Skip to content

Commit 7a8516e

Browse files
committed
Replace polling with notification mechanism
1 parent 86ac4d5 commit 7a8516e

1 file changed

Lines changed: 21 additions & 11 deletions

File tree

src/main.rs

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use log::*;
66
// use procstar::fd::parse_fd;
77
use procstar::agent;
88
use procstar::http;
9-
use procstar::procs::{restrict_exe, start_procs, SharedProcs};
9+
use procstar::procs::{restrict_exe, start_procs, SharedProcs, Notification};
1010
use procstar::proto;
1111
use procstar::res;
1212
use procstar::shutdown;
@@ -103,27 +103,37 @@ async fn maybe_run_until_idle(args: &argv::Args, procs: &SharedProcs) {
103103
if !args.wait {
104104
return;
105105
}
106+
106107
if args.agent {
107108
// For --wait --agent: wait for exactly one run to be assigned, then disconnect
108-
// First, wait for at least one process to be assigned (work arrives)
109-
while procs.is_empty() {
110-
// Wait for work to be assigned or shutdown signal
111-
tokio::select! {
112-
_ = procs.wait_for_shutdown() => return,
113-
_ = tokio::time::sleep(tokio::time::Duration::from_millis(100)) => {},
109+
let mut sub = procs.subscribe();
110+
111+
// If already has work, skip waiting
112+
if procs.is_empty() {
113+
// Wait for first process assignment
114+
loop {
115+
tokio::select! {
116+
_ = procs.wait_for_shutdown() => return,
117+
notification = sub.recv() => {
118+
match notification {
119+
Some(Notification::Start(_)) => break,
120+
Some(_) => continue,
121+
None => return,
122+
}
123+
}
124+
}
114125
}
115126
}
116127

117-
// Work has been assigned! Set shutdown to Idling to prevent accepting more work
128+
// Process has been assigned! Set shutdown to Idling to prevent accepting other processes
118129
procs.set_shutdown(shutdown::State::Idling);
119130

120-
// Now wait for the single assigned process to complete and be deleted
131+
// Now wait for the single assigned process to complete
121132
tokio::select! {
122133
_ = procs.wait_idle() => {},
123134
_ = procs.wait_for_shutdown() => {},
124135
};
125-
126-
// Work completed, agent should disconnect (Done state will be set by check_idling())
136+
// Process completed and deleted, agent should disconnect
127137
} else {
128138
// Non-agent mode: just wait until idle then exit
129139
tokio::select! {

0 commit comments

Comments
 (0)