use std::future::Future;
use std::pin::Pin;
use wabot_core::injection::Container;
use wabot_core::lifecycle::{
install_panic_reporter, systemd, Cancel, ShutdownManager, ShutdownOutcome, ShutdownTask,
};
type BoxedService = Pin<Box<dyn Future<Output = ServiceResult> + Send>>;
type ServiceBuilder = Box<dyn FnOnce(Cancel) -> BoxedService + Send>;
pub type ServiceResult = Result<(), String>;
struct Service {
name: String,
build: ServiceBuilder,
}
pub struct ProjectRunner {
container: Container,
shutdown: std::sync::Arc<ShutdownManager>,
services: Vec<Service>,
install_signals: bool,
}
impl ProjectRunner {
pub fn new(container: Container) -> Self {
Self {
container,
shutdown: std::sync::Arc::new(ShutdownManager::new()),
services: Vec::new(),
install_signals: true,
}
}
pub fn with_shutdown(mut self, shutdown: std::sync::Arc<ShutdownManager>) -> Self {
self.shutdown = shutdown;
self
}
pub fn container(&self) -> &Container {
&self.container
}
pub fn shutdown_manager(&self) -> &std::sync::Arc<ShutdownManager> {
&self.shutdown
}
pub fn service<F, E>(mut self, name: impl Into<String>, future: F) -> Self
where
F: Future<Output = Result<(), E>> + Send + 'static,
E: std::fmt::Display + Send + 'static,
{
self.services.push(Service {
name: name.into(),
build: Box::new(move |_cancel| {
Box::pin(async move { future.await.map_err(|error| error.to_string()) })
}),
});
self
}
pub fn service_with_cancel<F, Fut, E>(mut self, name: impl Into<String>, build: F) -> Self
where
F: FnOnce(Cancel) -> Fut + Send + 'static,
Fut: Future<Output = Result<(), E>> + Send + 'static,
E: std::fmt::Display + Send + 'static,
{
self.services.push(Service {
name: name.into(),
build: Box::new(move |cancel| {
Box::pin(async move { build(cancel).await.map_err(|error| error.to_string()) })
}),
});
self
}
pub fn on_shutdown(self, task: ShutdownTask) -> Self {
self.shutdown.register(task);
self
}
pub fn without_signal_handlers(mut self) -> Self {
self.install_signals = false;
self
}
pub async fn run(self) -> ShutdownOutcome {
install_panic_reporter();
let Self {
shutdown,
services,
install_signals,
..
} = self;
if services.is_empty() {
tracing::warn!("no services registered — nothing to run");
return ShutdownOutcome::Clean;
}
tracing::info!(
services = services
.iter()
.map(|service| service.name.as_str())
.collect::<Vec<_>>()
.join(", "),
"starting"
);
let mut running = tokio::task::JoinSet::new();
for service in services {
let Service { name, build } = service;
let future = build(shutdown.cancel_signal());
running.spawn(async move { (name, future.await) });
}
start_watchdog(&shutdown);
systemd::notify_ready();
let reason = if install_signals {
tokio::select! {
Some(joined) = running.join_next() => ended(joined),
signal = shutdown.wait_for_signal_name() => format!("received {signal}"),
}
} else {
match running.join_next().await {
Some(joined) => ended(joined),
None => "every service ended".to_string(),
}
};
drain_services(&mut running, &shutdown, install_signals).await;
shutdown.shutdown(&reason).await
}
}
fn start_watchdog(shutdown: &std::sync::Arc<ShutdownManager>) {
let Some(interval) = systemd::watchdog_interval() else {
return;
};
let cancel = shutdown.cancel_signal();
tracing::debug!(?interval, "systemd watchdog enabled");
tokio::spawn(async move {
let mut ticker = tokio::time::interval(interval);
loop {
tokio::select! {
_ = cancel.cancelled() => break,
_ = ticker.tick() => { systemd::notify_watchdog(); }
}
}
});
}
async fn drain_services(
running: &mut tokio::task::JoinSet<(String, ServiceResult)>,
shutdown: &std::sync::Arc<ShutdownManager>,
install_signals: bool,
) {
shutdown.cancel();
let deadline = shutdown.timeout();
let drain = async {
while let Some(joined) = running.join_next().await {
let _ = ended(joined);
}
};
tokio::select! {
outcome = tokio::time::timeout(deadline, drain) => {
if outcome.is_err() {
tracing::warn!(
timeout_seconds = deadline.as_secs(),
remaining = running.len(),
"services did not stop within the drain window — aborting them"
);
}
}
signal = shutdown.wait_for_signal_name(), if install_signals => {
tracing::warn!(signal, "second signal during drain — not waiting for services");
}
}
running.shutdown().await;
}
fn ended(joined: Result<(String, ServiceResult), tokio::task::JoinError>) -> String {
match joined {
Ok((name, Ok(()))) => {
tracing::warn!(service = %name, "service stopped");
format!("{name} stopped")
}
Ok((name, Err(error))) => {
tracing::error!(service = %name, %error, "service failed");
format!("{name} failed: {error}")
}
Err(error) => {
tracing::error!(%error, "service task did not complete");
format!("a service task did not complete: {error}")
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::Arc;
use std::time::Duration;
use wabot_core::lifecycle::ShutdownPhase;
fn runner() -> ProjectRunner {
ProjectRunner::new(Container::new()).without_signal_handlers()
}
fn brisk_runner() -> ProjectRunner {
runner().with_shutdown(Arc::new(ShutdownManager::with_timeout(
Duration::from_millis(50),
)))
}
#[tokio::test]
async fn one_service_stopping_shuts_everything_down() {
let drained = Arc::new(AtomicUsize::new(0));
let counter = drained.clone();
let outcome = brisk_runner()
.service("rest", async {
Err::<(), String>("port already in use".into())
})
.service("jobs", async {
std::future::pending::<Result<(), String>>().await
})
.on_shutdown(ShutdownTask::new("pool", ShutdownPhase::Close, move || {
let counter = counter.clone();
async move {
counter.fetch_add(1, Ordering::SeqCst);
}
}))
.run()
.await;
assert_eq!(outcome, ShutdownOutcome::Clean);
assert_eq!(drained.load(Ordering::SeqCst), 1, "the drain still ran");
}
#[tokio::test]
async fn a_service_that_returns_ok_also_ends_the_run() {
let outcome = brisk_runner()
.service("one-shot", async { Ok::<(), String>(()) })
.service("forever", async {
std::future::pending::<Result<(), String>>().await
})
.run()
.await;
assert_eq!(outcome, ShutdownOutcome::Clean);
}
#[tokio::test]
async fn a_cancel_aware_service_gets_to_finish() {
let cleaned = Arc::new(AtomicUsize::new(0));
let counter = cleaned.clone();
let outcome = runner()
.service("trigger", async { Ok::<(), String>(()) })
.service_with_cancel("edge", move |cancel| async move {
cancel.cancelled().await;
tokio::time::sleep(Duration::from_millis(20)).await;
counter.fetch_add(1, Ordering::SeqCst);
Ok::<(), String>(())
})
.run()
.await;
assert_eq!(outcome, ShutdownOutcome::Clean);
assert_eq!(
cleaned.load(Ordering::SeqCst),
1,
"the runner waited for the service instead of dropping it"
);
}
#[tokio::test]
async fn a_service_that_ignores_cancellation_is_aborted() {
let started = std::time::Instant::now();
let outcome = brisk_runner()
.service("trigger", async { Ok::<(), String>(()) })
.service_with_cancel("stubborn", |_cancel| async {
std::future::pending::<Result<(), String>>().await
})
.run()
.await;
assert_eq!(outcome, ShutdownOutcome::Clean);
assert!(
started.elapsed() < Duration::from_secs(2),
"the drain window bounded the wait, it took {:?}",
started.elapsed()
);
}
#[tokio::test]
async fn a_replaced_manager_still_cancels_services_registered_before_it() {
let cancelled = Arc::new(AtomicUsize::new(0));
let counter = cancelled.clone();
let outcome = runner()
.service_with_cancel("early", move |cancel| async move {
cancel.cancelled().await;
counter.fetch_add(1, Ordering::SeqCst);
Ok::<(), String>(())
})
.with_shutdown(Arc::new(ShutdownManager::with_timeout(
Duration::from_millis(50),
)))
.service("trigger", async { Ok::<(), String>(()) })
.run()
.await;
assert_eq!(outcome, ShutdownOutcome::Clean);
assert_eq!(cancelled.load(Ordering::SeqCst), 1);
}
#[tokio::test]
async fn shutdown_tasks_run_in_phase_order() {
let log = Arc::new(parking_lot::Mutex::new(Vec::<&'static str>::new()));
let mut runner = runner().service("brief", async { Ok::<(), String>(()) });
for (name, phase, mark) in [
("pool", ShutdownPhase::Close, "close"),
("http", ShutdownPhase::Intake, "intake"),
] {
let log = log.clone();
runner = runner.on_shutdown(ShutdownTask::new(name, phase, move || {
let log = log.clone();
async move { log.lock().push(mark) }
}));
}
runner.run().await;
assert_eq!(*log.lock(), vec!["intake", "close"]);
}
#[tokio::test]
async fn a_stuck_drain_reports_a_failure_code() {
let shutdown = Arc::new(ShutdownManager::with_timeout(Duration::from_millis(50)));
shutdown.register(ShutdownTask::new(
"never-finishes",
ShutdownPhase::Drain,
|| async {
tokio::time::sleep(Duration::from_secs(30)).await;
},
));
let outcome = runner()
.with_shutdown(shutdown)
.service("brief", async { Ok::<(), String>(()) })
.run()
.await;
assert_eq!(outcome.exit_code(), 1);
match outcome {
ShutdownOutcome::TimedOut { still_running } => {
assert_eq!(still_running, vec!["never-finishes".to_string()]);
}
other => panic!("expected a timeout, got {other:?}"),
}
}
#[tokio::test]
async fn with_no_services_it_does_nothing() {
assert_eq!(runner().run().await, ShutdownOutcome::Clean);
}
}