use std::path::Path;
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum StartupAction {
Proceed,
ExitAlreadyRunning,
Fail(String),
}
pub fn startup_action_from_probe_result(probe_ok: bool) -> StartupAction {
if probe_ok {
StartupAction::ExitAlreadyRunning
} else {
StartupAction::Proceed
}
}
pub async fn single_instance_check(addr_file: Option<&Path>) -> StartupAction {
let Some(path) = addr_file else {
return StartupAction::Proceed;
};
let probe_ok = trusty_common::check_already_running(path, "/health")
.await
.is_some();
startup_action_from_probe_result(probe_ok)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn startup_action_from_probe_result_when_alive() {
assert_eq!(
startup_action_from_probe_result(true),
StartupAction::ExitAlreadyRunning,
"alive probe → ExitAlreadyRunning"
);
}
#[test]
fn startup_action_from_probe_result_when_dead() {
assert_eq!(
startup_action_from_probe_result(false),
StartupAction::Proceed,
"dead/absent probe → Proceed"
);
}
#[tokio::test]
async fn single_instance_check_proceeds_when_no_path() {
let action = single_instance_check(None).await;
assert_eq!(
action,
StartupAction::Proceed,
"no addr path → Proceed (cold start must not be blocked)"
);
}
#[tokio::test]
async fn single_instance_check_proceeds_when_addr_file_missing() {
let tmp = tempfile::tempdir().expect("tempdir");
let missing = tmp.path().join("http_addr");
let action = single_instance_check(Some(&missing)).await;
assert_eq!(
action,
StartupAction::Proceed,
"missing addr file → Proceed"
);
}
#[tokio::test]
async fn single_instance_check_proceeds_when_addr_file_stale() {
let tmp = tempfile::tempdir().expect("tempdir");
let addr_file = tmp.path().join("http_addr");
std::fs::write(&addr_file, "127.0.0.1:19999\n").expect("write");
let action = single_instance_check(Some(&addr_file)).await;
assert_eq!(
action,
StartupAction::Proceed,
"stale addr file (no listener) → Proceed"
);
}
}