use crate::domain::{
process::{ExitIntent, ProcessState},
pty::ExitOutcome,
};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ExitDecision {
Retire,
Stop,
RestartNow,
RestartLater,
Settle(ProcessState),
}
pub struct ProcessLifecycle;
impl ProcessLifecycle {
pub fn after_exit(
intent: ExitIntent,
retired_by_config: bool,
should_restart: bool,
outcome: ExitOutcome,
) -> ExitDecision {
if retired_by_config {
return ExitDecision::Retire;
}
match intent {
ExitIntent::StopRetryable | ExitIntent::StopInFlight => ExitDecision::Stop,
ExitIntent::RestartRetryable | ExitIntent::RestartInFlight => ExitDecision::RestartNow,
ExitIntent::FollowPolicy if should_restart => ExitDecision::RestartLater,
ExitIntent::FollowPolicy => ExitDecision::Settle(exit_state(outcome)),
}
}
}
fn exit_state(outcome: ExitOutcome) -> ProcessState {
match outcome {
ExitOutcome::Succeeded => ProcessState::Exited,
ExitOutcome::Failed => ProcessState::Crashed,
}
}