use std::io::ErrorKind;
use std::path::{Path, PathBuf};
use shep_core::paths::ShepPaths;
use shep_core::protocol::BusEvent;
use shep_core::transport::Listener;
use tokio::sync::watch;
use tokio::task::JoinHandle;
use super::BootError;
use super::pidfile::{PidfileLock, pidfile};
use super::signals::SignalTasks;
use crate::bus::SharedEvent;
use crate::rpc::RpcContext;
use crate::server::RpcServer;
use crate::snapshot::SnapshotWriter;
#[derive(Debug)]
pub struct RunningDaemon {
pub(super) ctx: RpcContext,
pub(super) listener: Listener,
pub(super) writer: SnapshotWriter,
pub(super) dog_watch: JoinHandle<()>,
pub(super) silent_dog_watch: JoinHandle<()>,
pub(super) paths: ShepPaths,
pub(super) socket: PathBuf,
pub(super) shutdown_rx: watch::Receiver<bool>,
pub(super) signals: SignalTasks,
pub(super) pidfile_lock: PidfileLock,
pub(super) delete_flock_on_shutdown: bool,
}
impl RunningDaemon {
#[must_use]
pub fn context(&self) -> RpcContext {
self.ctx.clone()
}
#[must_use]
pub fn socket(&self) -> &Path {
&self.socket
}
pub async fn run(self) -> Result<(), BootError> {
let RunningDaemon {
ctx,
listener,
writer,
dog_watch,
silent_dog_watch,
paths,
socket,
shutdown_rx,
signals: _signals,
pidfile_lock: _pidfile_lock,
delete_flock_on_shutdown,
} = self;
RpcServer::new(listener, ctx.clone())
.serve(shutdown_rx)
.await;
writer.stop().await;
dog_watch.abort();
silent_dog_watch.abort();
let edges = ctx.registry.depends_on_by_name();
if delete_flock_on_shutdown {
ctx.registry.clear();
}
if let Err(err) = ctx.snapshot_now().await {
tracing::warn!(%err, "final muster roll write failed");
}
let _ = ctx.events.send(SharedEvent::new(BusEvent::DaemonShutdown));
crate::boot_order::stop_edges_in_reverse(edges, &ctx.dog_names, &ctx.supervisor).await;
ctx.supervisor.shutdown().await;
#[cfg(unix)]
let unlink_socket = unlink_if_present(&socket);
#[cfg(windows)]
let unlink_socket = {
let _ = &socket;
Ok(())
};
let unlink_pidfile = unlink_if_present(&pidfile(&paths));
unlink_socket.and(unlink_pidfile)
}
}
fn unlink_if_present(path: &Path) -> Result<(), BootError> {
match std::fs::remove_file(path) {
Ok(()) => Ok(()),
Err(err) if err.kind() == ErrorKind::NotFound => Ok(()),
Err(source) => Err(BootError::Io {
path: path.to_path_buf(),
source,
}),
}
}
#[cfg(all(test, unix))]
mod tests {
use super::*;
use crate::boot::pidfile::read_pidfile;
use crate::boot::{BootOptions, SIGNAL_TEST_LOCK, boot_with_idle_sweep, init_dirs};
use crate::dogs::DogSpec;
use crate::fake::{ProcScript, ScriptedRunner};
use crate::snapshot::{FlockSnapshot, SavedApp};
use crate::testing::test_paths;
use shep_core::config::AppConfig;
use shep_core::protocol::{DogSource, ProcessEventKind};
use std::collections::BTreeMap;
use std::time::Duration;
#[tokio::test]
async fn boot_restores_a_saved_flock_and_tears_down_in_order() {
let _guard = SIGNAL_TEST_LOCK.lock().await;
let dir = tempfile::tempdir().unwrap();
let paths = test_paths(&dir);
init_dirs(&paths).unwrap();
let roll = FlockSnapshot::with_apps(vec![SavedApp {
app: AppConfig::minimal("web", "./srv"),
instances_running: 99,
disabled: false,
}]);
crate::snapshot::write_atomic(&paths.snapshot, &roll).unwrap();
let daemon = boot_with_idle_sweep(
ScriptedRunner::new(vec![ProcScript::never_exits()]),
paths.clone(),
BootOptions {
restore: true,
..BootOptions::default()
},
)
.await
.unwrap();
let ctx = daemon.context();
let flock = ctx.supervisor.list_checked().await.unwrap();
assert_eq!(flock.len(), 1, "the muster roll must be back on its feet");
assert_eq!(flock[0].name, "web");
let run = tokio::spawn(daemon.run());
ctx.shutdown();
tokio::time::timeout(Duration::from_secs(5), run)
.await
.unwrap()
.unwrap()
.unwrap();
let final_roll = crate::snapshot::read(&paths.snapshot).unwrap();
assert_eq!(
final_roll.apps[0].instances_running, 1,
"the roll must be written before the flock is killed, or muster restores nothing"
);
assert!(
!paths.socket.exists(),
"the socket is unlinked on a clean exit"
);
assert_eq!(read_pidfile(&paths).unwrap(), None);
}
#[tokio::test]
async fn delete_flock_on_shutdown_clears_the_roll_even_on_a_signalled_exit() {
let _guard = SIGNAL_TEST_LOCK.lock().await;
let dir = tempfile::tempdir().unwrap();
let paths = test_paths(&dir);
init_dirs(&paths).unwrap();
let daemon = boot_with_idle_sweep(
ScriptedRunner::new(vec![ProcScript::never_exits()]),
paths.clone(),
BootOptions {
delete_flock_on_shutdown: true,
..BootOptions::default()
},
)
.await
.unwrap();
let ctx = daemon.context();
let held = shep_core::config::normalize(AppConfig::minimal("held", "./held")).unwrap();
ctx.registry.record(std::slice::from_ref(&held));
ctx.supervisor.start(vec![held]).await.unwrap();
let flock = ctx.supervisor.list_checked().await.unwrap();
assert_eq!(flock.len(), 1, "the held app must actually be up");
let run = tokio::spawn(daemon.run());
ctx.shutdown();
tokio::time::timeout(Duration::from_secs(5), run)
.await
.unwrap()
.unwrap()
.unwrap();
let final_roll = crate::snapshot::read(&paths.snapshot).unwrap();
assert!(
final_roll.apps.is_empty(),
"delete_flock_on_shutdown must leave the roll empty, not {:?}",
final_roll.apps
);
}
#[tokio::test]
async fn a_dev_session_still_stops_its_flock_in_dependency_order() {
let _guard = SIGNAL_TEST_LOCK.lock().await;
let dir = tempfile::tempdir().unwrap();
let paths = test_paths(&dir);
init_dirs(&paths).unwrap();
let daemon = boot_with_idle_sweep(
ScriptedRunner::new(vec![
ProcScript::never_exits(),
ProcScript::ignores_signals(),
]),
paths.clone(),
BootOptions {
delete_flock_on_shutdown: true,
..BootOptions::default()
},
)
.await
.unwrap();
let ctx = daemon.context();
let db = AppConfig::minimal("db", "./db");
let mut worker = AppConfig::minimal("worker", "./worker");
worker.depends_on = vec!["db".to_string()];
worker.kill_timeout = shep_core::values::UpDuration::from_millis(100);
let apps = shep_core::config::normalize_all(vec![db, worker]).unwrap();
ctx.registry.record(&apps);
ctx.supervisor.start(apps).await.unwrap();
let mut rx = ctx.events.subscribe();
let run = tokio::spawn(daemon.run());
ctx.shutdown();
tokio::time::timeout(Duration::from_secs(5), run)
.await
.unwrap()
.unwrap()
.unwrap();
let mut stops = Vec::new();
while let Ok(event) = rx.try_recv() {
if let shep_core::protocol::BusEvent::Process {
event: shep_core::protocol::ProcessEventKind::Stop,
info,
..
} = &*event
{
stops.push(info.name.clone());
}
}
assert_eq!(
stops,
vec!["worker".to_string(), "db".to_string()],
"the dependant stops before what it waits on"
);
}
#[tokio::test]
async fn a_dog_is_stopped_by_the_time_run_returns() {
let _guard = SIGNAL_TEST_LOCK.lock().await;
let dir = tempfile::tempdir().unwrap();
let paths = test_paths(&dir);
init_dirs(&paths).unwrap();
let daemon = boot_with_idle_sweep(
ScriptedRunner::new(vec![ProcScript::never_exits()]),
paths.clone(),
BootOptions {
dogs: vec![DogSpec {
name: "metrics".to_string(),
source: DogSource::BuiltIn {
grants: None,
events: Vec::new(),
},
env: BTreeMap::new(),
}],
..BootOptions::default()
},
)
.await
.unwrap();
let ctx = daemon.context();
let flock = ctx.supervisor.list_checked().await.unwrap();
assert_eq!(
flock.len(),
1,
"the dog has to be up for its stop to mean anything: {flock:?}"
);
let mut rx = ctx.events.subscribe();
let run = tokio::spawn(daemon.run());
ctx.shutdown();
tokio::time::timeout(Duration::from_secs(5), run)
.await
.unwrap()
.unwrap()
.unwrap();
let mut stops = Vec::new();
while let Ok(event) = rx.try_recv() {
if let BusEvent::Process {
event: ProcessEventKind::Stop,
info,
..
} = &*event
{
stops.push(info.name.clone());
}
}
assert!(
stops.iter().any(|name| name == "metrics"),
"the dog must be stopped before run() returns; stops were {stops:?}"
);
}
}