use super::*;
pub(super) fn spec_prober(spec: &SpawnSpec) -> Arc<dyn Prober> {
Arc::new(OsProber::new(spec.cwd.clone(), spec.env.clone()))
}
pub(super) fn spawn_readiness_task(
id: u32,
epoch: u64,
manually: bool,
source: ReadinessSource,
deadline: Duration,
prober: Arc<dyn Prober>,
actor_tx: mpsc::Sender<Msg>,
) -> oneshot::Sender<()> {
let (ready_tx, ready_rx) = oneshot::channel();
tokio::spawn(async move {
let readiness = await_ready(&source, deadline, ready_rx, prober).await;
let _ = actor_tx
.send(Msg::ReadyResult {
id,
epoch,
manually,
readiness,
})
.await;
});
ready_tx
}
pub(super) fn spawn_action_task(
id: u32,
stamp: u64,
message: ShepherdMessage,
to_child: mpsc::Sender<ShepherdMessage>,
deadline: Duration,
actor_tx: mpsc::Sender<Msg>,
) -> oneshot::Sender<String> {
let (reply_tx, reply_rx) = oneshot::channel();
tokio::spawn(async move {
let delivered = tokio::time::timeout(deadline, async move {
if to_child.send(message).await.is_err() {
return None;
}
reply_rx.await.ok()
})
.await;
let outcome = match delivered {
Ok(Some(body)) => ActionOutcome::Replied { body },
Ok(None) => ActionOutcome::NoChannel,
Err(_elapsed) => ActionOutcome::TimedOut,
};
let _ = actor_tx
.send(Msg::ActionResult { id, stamp, outcome })
.await;
});
reply_tx
}
pub(super) fn spawn_trigger_task(
mut rows: Vec<ActionReply>,
waits: Vec<(u32, String, oneshot::Receiver<ActionOutcome>)>,
reply: oneshot::Sender<Result<Vec<ActionReply>, SupervisorError>>,
) {
tokio::spawn(async move {
for (id, name, answer) in waits {
let outcome = answer.await.unwrap_or(ActionOutcome::NoChannel);
rows.push(ActionReply { id, name, outcome });
}
rows.sort_unstable_by(|a, b| (a.name.as_str(), a.id).cmp(&(b.name.as_str(), b.id)));
let _ = reply.send(Ok(rows));
});
}
type SignalWait = (u32, String, oneshot::Receiver<Result<(), RunnerError>>);
pub(super) fn spawn_signal_task(
mut rows: Vec<SignalReply>,
waits: Vec<SignalWait>,
reply: oneshot::Sender<Result<Vec<SignalReply>, SupervisorError>>,
) {
tokio::spawn(async move {
for (id, name, answer) in waits {
let outcome = match answer.await {
Ok(Ok(())) => SignalOutcome::Delivered,
Ok(Err(err)) => SignalOutcome::Failed {
reason: err.to_string(),
},
Err(_dropped) => SignalOutcome::NotRunning,
};
rows.push(SignalReply { id, name, outcome });
}
rows.sort_unstable_by(|a, b| (a.name.as_str(), a.id).cmp(&(b.name.as_str(), b.id)));
let _ = reply.send(Ok(rows));
});
}
type LineWait = (u32, String, oneshot::Receiver<Result<(), RunnerError>>);
pub(super) fn spawn_send_line_task(
settled: Vec<LineReply>,
waits: Vec<LineWait>,
reply: oneshot::Sender<Result<Vec<LineReply>, SupervisorError>>,
) {
tokio::spawn(async move {
let mut rows = settled;
rows.extend(
futures_util::future::join_all(waits.into_iter().map(
|(id, name, answer)| async move {
let outcome = match tokio::time::timeout(STDIN_WRITE_TIMEOUT, answer).await {
Ok(Ok(Ok(()))) => LineOutcome::Sent,
Ok(Ok(Err(err))) => LineOutcome::NotWritten {
reason: err.to_string(),
},
Ok(Err(_recv)) => LineOutcome::NoStdin,
Err(_elapsed) => LineOutcome::NotWritten {
reason: format!(
"the app did not read its stdin within {}s; this line \
may still land if it drains",
STDIN_WRITE_TIMEOUT.as_secs()
),
},
};
LineReply { id, name, outcome }
},
))
.await,
);
rows.sort_unstable_by(|a, b| (a.name.as_str(), a.id).cmp(&(b.name.as_str(), b.id)));
let _ = reply.send(Ok(rows));
});
}