use tracing::Instrument as _;
use super::*;
use crate::sweep::{LambSnapshot, LambSweep, sweep_lambs};
#[derive(Debug)]
pub(super) struct SignalRequest {
pub(super) sig: OperatorSignal,
pub(super) done: oneshot::Sender<Result<(), RunnerError>>,
}
pub(super) struct SheepHandles {
pub(super) ctl: mpsc::Sender<SheepCtl>,
pub(super) signals: mpsc::Sender<SignalRequest>,
}
impl<R: ProcessRunner> Actor<R> {
pub(super) fn lamb_sweep(&self) -> Option<Arc<dyn LambSweep>> {
self.extras
.as_ref()
.map(|extras| Arc::clone(&extras.lamb_sweep))
}
}
pub(super) fn spawn_sheep_task<P: RunningProcess>(
id: u32,
proc: P,
io: ProcIo,
app: ResolvedApp,
events: Bus,
actor_tx: mpsc::Sender<Msg>,
sweep: Option<Arc<dyn LambSweep>>,
) -> SheepHandles {
let (ctl_tx, ctl_rx) = mpsc::channel(SHEEP_CTL_CAPACITY);
let (signal_tx, signal_rx) = mpsc::channel(SIGNAL_CAPACITY);
tokio::spawn(run_sheep(
id, proc, io, app, ctl_rx, signal_rx, events, actor_tx, sweep,
));
SheepHandles {
ctl: ctl_tx,
signals: signal_tx,
}
}
#[allow(clippy::too_many_arguments)]
pub(super) async fn run_sheep<P: RunningProcess>(
id: u32,
mut proc: P,
io: ProcIo,
app: ResolvedApp,
mut ctl_rx: mpsc::Receiver<SheepCtl>,
mut signal_rx: mpsc::Receiver<SignalRequest>,
events: Bus,
actor_tx: mpsc::Sender<Msg>,
sweep: Option<Arc<dyn LambSweep>>,
) {
let ProcIo {
mut logs,
mut from_child,
to_child,
log_ctl: _log_ctl,
to_stdin: _to_stdin,
pty: _pty,
} = io;
let mut ctl_open = true;
let mut logs_open = true;
let mut from_child_open = true;
let mut signals_open = true;
loop {
tokio::select! {
outcome = proc.wait() => {
if let Some(sweep) = &sweep
&& let Some(snapshot) = sweep.last_snapshot(proc.pid())
{
sweep_after_exit(sweep.as_ref(), &snapshot, proc.pid(), id, &app).await;
}
let _ = actor_tx.send(Msg::Exited { id, outcome }).await;
break;
}
maybe_ctl = ctl_rx.recv(), if ctl_open => {
match maybe_ctl {
Some(SheepCtl::Kill { grace }) => {
let snapshot = match &sweep {
Some(sweep) => Some(stop_snapshot(sweep, proc.pid()).await),
None => None,
};
let outcome =
kill_process(&mut proc, app.config(), Some(&to_child), grace).await;
if let Some(sweep) = &sweep
&& let Some(snapshot) = &snapshot
{
sweep_after_exit(sweep.as_ref(), snapshot, proc.pid(), id, &app)
.await;
}
let _ = actor_tx.send(Msg::Exited { id, outcome }).await;
break;
}
None => ctl_open = false,
}
}
maybe_signal = signal_rx.recv(), if signals_open => {
match maybe_signal {
Some(SignalRequest { sig, done }) => {
let _ = done.send(proc.signal_process(sig));
}
None => signals_open = false,
}
}
maybe_line = logs.recv(), if logs_open => {
match maybe_line {
Some(line) => {
events.publish_log(if line.err {
BusEvent::LogErr { id, line: line.line }
} else {
BusEvent::LogOut { id, line: line.line }
});
}
None => logs_open = false,
}
}
maybe_msg = from_child.recv(), if from_child_open => {
match maybe_msg {
Some(message) => {
let _ = events.send(SharedEvent::new(BusEvent::Channel {
id,
message: message.clone(),
}));
match message {
ChildMessage::Ready => {
let _ = actor_tx.send(Msg::Ready { id }).await;
}
ChildMessage::Metric { name, value, labels, .. } => {
tracing::debug!(
id,
name,
value,
?labels,
"child metric forwarded to the bus as channel.metric"
);
}
ChildMessage::ActionReply {
action,
body,
id: stamp,
} => {
let _ = actor_tx
.send(Msg::ActionReply { id, action, body, stamp })
.await;
}
ChildMessage::LambLabel { pid, label } => {
let root_pid = proc.pid();
let _ = actor_tx
.send(Msg::LambLabel { root_pid, pid, label })
.await;
}
ChildMessage::Note { name, value } => {
tracing::debug!(
id,
name,
value,
"child note forwarded to the bus as channel.note"
);
}
}
}
None => from_child_open = false,
}
}
}
}
}
async fn stop_snapshot(sweep: &Arc<dyn LambSweep>, root_pid: u32) -> LambSnapshot {
let walker = Arc::clone(sweep);
let fresh = tokio::task::spawn_blocking(move || walker.snapshot(root_pid))
.await
.unwrap_or_else(|error| {
tracing::warn!(root_pid, %error, "lamb walk failed; sweeping the last tick's lambs only");
LambSnapshot::default()
});
match sweep.last_snapshot(root_pid) {
Some(last) => fresh.merge(last),
None => fresh,
}
}
async fn sweep_after_exit(
sweep: &dyn LambSweep,
snapshot: &LambSnapshot,
root_pid: u32,
id: u32,
app: &ResolvedApp,
) {
let config = app.config();
let span = tracing::info_span!("lamb_sweep", id, name = %config.name);
if leader_still_runs(sweep, snapshot, root_pid) {
tracing::warn!(
parent: &span,
root_pid,
"the sheep's own process still runs after its exit was seen; leaving its lambs"
);
return;
}
sweep_lambs(sweep, snapshot, config.kill_timeout.as_duration())
.instrument(span)
.await;
}
fn leader_still_runs(sweep: &dyn LambSweep, snapshot: &LambSnapshot, root_pid: u32) -> bool {
snapshot.latest().is_some_and(|latest| {
!sweep
.survivors(&LambSnapshot::new([root_pid], latest))
.is_empty()
})
}