use super::*;
pub(super) fn send_reply(reply: ReplyKind, outcome: Result<Vec<ProcessInfo>, SupervisorError>) {
match reply {
ReplyKind::Info(tx) => {
let _ = tx.send(outcome);
}
ReplyKind::Ids(tx) => {
let ids = outcome.map(|infos| infos.into_iter().map(|info| info.id).collect());
let _ = tx.send(ids);
}
ReplyKind::Shutdown(tx) => {
let _ = tx.send(());
}
}
}
pub(super) fn to_info(entry: &ProcessEntry, smits: &Smits) -> ProcessInfo {
let uptime_ms = entry.started_at.map_or(0, |started_at| {
tokio::time::Instant::now()
.saturating_duration_since(started_at)
.as_millis() as u64
});
ProcessInfo::builder(entry.id, entry.spec.config().name.clone(), entry.status)
.pid(entry.pid)
.restarts(entry.restarts)
.uptime_ms(uptime_ms)
.fold(entry.spec.config().fold.clone())
.depends_on(entry.spec.config().depends_on.clone())
.out_file(Some(entry.out_file.to_string_lossy().into_owned()))
.err_file(Some(entry.err_file.to_string_lossy().into_owned()))
.cpu_percent(None)
.memory_bytes(None)
.dog(entry.dog.clone())
.last_exit(entry.last_exit)
.spawn_failure(entry.spawn_failure.clone())
.smit(
smits
.get(&entry.spec.config().name)
.map(|(_, smit)| smit.clone()),
)
.instance(Some(entry.instance))
.pending({
let fields = pending_fields(entry);
(!fields.is_empty()).then_some(fields)
})
.overridden((!entry.overridden.is_empty()).then(|| entry.overridden.clone()))
.max_memory(entry.spec.config().max_memory.map(MemSize::bytes))
.level_rules(entry.spec.config().level_rules.clone())
.log_timestamps((!entry.spec.config().log_timestamps).then_some(false))
.sandbox_refused(sandbox_refused(entry))
.build()
}
fn sandbox_refused(entry: &ProcessEntry) -> Option<bool> {
(entry.status == ProcStatus::Errored).then(|| {
entry.spawn_failure.is_none()
&& is_sandbox_refusal(
entry.spec.config(),
entry.last_exit.and_then(|exit| exit.code),
)
})
}
impl From<ExitOutcome> for ExitInfo {
fn from(outcome: ExitOutcome) -> Self {
Self {
code: outcome.code,
signal: outcome.signal,
}
}
}
pub(super) fn swap_budget(config: &AppConfig) -> Duration {
config.listen_timeout.as_duration()
+ config.graceful_timeout.as_duration()
+ RELOAD_DEADLINE_SLACK
}
pub(super) fn reload_eligible(slot: &SheepSlot) -> bool {
slot.entry.status == ProcStatus::Online || slot.ready_failed
}
pub(super) fn restored_status(slot: &SheepSlot) -> ProcStatus {
if slot.ready_failed {
ProcStatus::Starting
} else {
ProcStatus::Online
}
}