Skip to main content

kaizen/daemon/
lifecycle.rs

1// SPDX-License-Identifier: AGPL-3.0-or-later
2//! Daemon process lifecycle.
3
4use crate::ipc::{DaemonRequest, DaemonResponse, DaemonStatus};
5use anyhow::{Result, anyhow};
6use std::path::{Path, PathBuf};
7
8const PID_FILE: &str = "daemon.pid";
9const SOCK_FILE: &str = "daemon.sock";
10const LOG_FILE: &str = "daemon.log";
11const TOKEN_FILE: &str = "web_token.hex";
12
13#[derive(Debug, Clone)]
14pub struct RuntimePaths {
15    pub dir: PathBuf,
16    pub pid: PathBuf,
17    pub sock: PathBuf,
18    pub log: PathBuf,
19    pub token: PathBuf,
20}
21
22#[derive(Debug, Clone)]
23pub enum DaemonStatusOutcome {
24    Running(DaemonStatus),
25    Stopped { socket: PathBuf },
26}
27
28pub fn enabled() -> bool {
29    if let Ok(value) = std::env::var("KAIZEN_DAEMON") {
30        return value != "0";
31    }
32    executable_name().is_some_and(|name| name == "kaizen")
33}
34
35fn executable_name() -> Option<String> {
36    std::env::args()
37        .next()
38        .and_then(|path| PathBuf::from(path).file_stem().map(|stem| stem.to_owned()))
39        .and_then(|stem| stem.to_str().map(str::to_string))
40}
41
42pub fn runtime_paths() -> Result<RuntimePaths> {
43    runtime_paths_for(&std::env::current_dir()?)
44}
45
46pub fn runtime_paths_for(workspace: &Path) -> Result<RuntimePaths> {
47    let dir = crate::core::home_paths::root(workspace)?;
48    let child = |name| crate::core::paths::descendant_path(&dir, Path::new(name));
49    Ok(RuntimePaths {
50        pid: child(PID_FILE)?,
51        sock: child(SOCK_FILE)?,
52        log: child(LOG_FILE)?,
53        token: child(TOKEN_FILE)?,
54        dir,
55    })
56}
57
58pub fn ensure_running() -> Result<()> {
59    ensure_running_for(&std::env::current_dir()?)
60}
61
62pub fn ensure_running_for(workspace: &Path) -> Result<()> {
63    runtime_paths_for(workspace)?;
64    if !enabled() || try_status().is_ok() {
65        return Ok(());
66    }
67    super::start_background_for(workspace).map(|_| ())
68}
69
70pub fn try_status() -> Result<DaemonStatus> {
71    let response = tokio::runtime::Runtime::new()?
72        .block_on(super::client::request_async(DaemonRequest::Status))?;
73    match response {
74        DaemonResponse::Status(status) => Ok(status),
75        DaemonResponse::Error { message, .. } => Err(anyhow!(message)),
76        _ => Err(anyhow!("unexpected daemon status response")),
77    }
78}
79
80pub fn status_outcome() -> Result<DaemonStatusOutcome> {
81    match try_status() {
82        Ok(status) => Ok(DaemonStatusOutcome::Running(status)),
83        Err(err) if is_daemon_unavailable(&err) => Ok(DaemonStatusOutcome::Stopped {
84            socket: runtime_paths()?.sock,
85        }),
86        Err(err) => Err(err),
87    }
88}
89
90fn is_daemon_unavailable(err: &anyhow::Error) -> bool {
91    err.chain().any(is_unavailable_cause)
92}
93
94fn is_unavailable_cause(cause: &(dyn std::error::Error + 'static)) -> bool {
95    if cause.to_string().starts_with("connect daemon socket:") {
96        return true;
97    }
98    cause.downcast_ref::<std::io::Error>().is_some_and(|error| {
99        matches!(
100            error.kind(),
101            std::io::ErrorKind::UnexpectedEof
102                | std::io::ErrorKind::ConnectionReset
103                | std::io::ErrorKind::BrokenPipe
104        )
105    })
106}
107
108pub fn start_foreground() -> Result<()> {
109    tokio::runtime::Builder::new_multi_thread()
110        .enable_all()
111        .build()?
112        .block_on(super::server::run_server())
113}
114
115pub fn stop() -> Result<String> {
116    let response = tokio::runtime::Runtime::new()?
117        .block_on(super::client::request_async(DaemonRequest::Stop))?;
118    match response {
119        DaemonResponse::Ack { message } => Ok(message),
120        DaemonResponse::Error { message, .. } => Err(anyhow!(message)),
121        _ => Err(anyhow!("unexpected daemon stop response")),
122    }
123}
124
125#[cfg(test)]
126mod tests {
127    use super::*;
128
129    #[test]
130    fn eof_after_shutdown_is_unavailable() {
131        let error = anyhow::Error::new(std::io::Error::from(std::io::ErrorKind::UnexpectedEof));
132        assert!(is_daemon_unavailable(&error));
133    }
134}