aboutsummaryrefslogtreecommitdiffstats
path: root/crates/daemon/src/main.rs
blob: 174cf94b46cece78522d54e8561634ec5cf19974 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
#![expect(unused_crate_dependencies, reason = "Didn't remove them yet")]

use clap::Parser;
use eyre::{Result, WrapErr, bail, eyre};
use fs4::fs_std::FileExt;
use std::fs::{self, File, OpenOptions};
use std::io::Write;
use std::path::{Path, PathBuf};
use std::time::{Duration, Instant};
use tokio::time::sleep;
use turtle_daemon::{
    aclient::{database::ClientSqlite, record::sqlite_store::SqliteStore, settings::Settings},
    client::{DaemonClientErrorKind, HistoryClient, classify_error},
};

#[derive(Parser, Debug)]
#[command(infer_subcommands = true)]
pub(crate) enum Cmd {
    /// Start the daemon server
    Start {
        /// Also write daemon logs to the console (useful for debugging)
        #[arg(long)]
        show_logs: bool,
    },

    /// Show the daemon's current status
    Status,

    /// Stop the daemon gracefully
    Stop,
}

#[tokio::main]
async fn main() -> Result<()> {
    let settings = Settings::new().wrap_err("could not load client settings")?;
    let db_path = PathBuf::from(settings.db_path.as_str());
    let record_store_path = PathBuf::from(settings.record_store_path.as_str());

    let db = ClientSqlite::new(db_path, settings.local_timeout).await?;
    let sqlite_store = SqliteStore::new(record_store_path, settings.local_timeout).await?;

    match Cmd::parse() {
        Cmd::Start { show_logs, .. } => start_cmd(settings, store, history_db, show_logs).await,
        Cmd::Status => status_cmd(&settings).await,
        Cmd::Stop => stop_cmd(&settings).await,
    }
}

const STARTUP_POLL: Duration = Duration::from_millis(40);
const LEGACY_DAEMON_RESTART_MESSAGE: &str = "legacy daemon detected; restart daemon manually";

struct PidfileGuard {
    file: File,
}

impl PidfileGuard {
    fn acquire(path: &Path) -> Result<Self> {
        let mut file = open_lock_file(path)?;

        if !file.try_lock_exclusive()? {
            bail!(
                "daemon already running (pidfile lock busy at {})",
                path.display()
            );
        }

        file.set_len(0)
            .wrap_err_with(|| format!("could not truncate daemon pidfile {}", path.display()))?;
        writeln!(file, "{}", std::process::id())
            .and_then(|()| writeln!(file, "{DAEMON_VERSION}"))
            .wrap_err_with(|| format!("could not write daemon pidfile {}", path.display()))?;

        Ok(Self { file })
    }
}

impl Drop for PidfileGuard {
    fn drop(&mut self) {
        drop(self.file.unlock());
    }
}

enum Probe {
    Ready(HistoryClient),
    NeedsRestart(String),
    Unreachable(eyre::Report),
}

fn is_legacy_daemon_error(err: &eyre::Report) -> bool {
    matches!(classify_error(err), DaemonClientErrorKind::Unimplemented)
}

fn open_lock_file(path: &Path) -> Result<File> {
    if let Some(parent) = path.parent() {
        fs::create_dir_all(parent)
            .wrap_err_with(|| format!("could not create lock directory {}", parent.display()))?;
    }

    OpenOptions::new()
        .read(true)
        .write(true)
        .create(true)
        .truncate(false)
        .open(path)
        .wrap_err_with(|| format!("could not open lock file {}", path.display()))
}

async fn wait_for_lock(path: &Path, timeout: Duration) -> Result<File> {
    const LOCK_POLL: Duration = Duration::from_millis(20);

    let file = open_lock_file(path)?;
    let start = Instant::now();

    loop {
        match file.try_lock_exclusive() {
            Ok(true) => return Ok(file),
            Ok(false) => {
                if start.elapsed() >= timeout {
                    bail!("timed out waiting for lock at {}", path.display());
                }

                sleep(LOCK_POLL).await;
            }
            Err(err) => {
                return Err(eyre!("could not lock {}: {err}", path.display()));
            }
        }
    }
}

async fn wait_for_pidfile_available(path: &Path, timeout: Duration) -> Result<()> {
    let file = wait_for_lock(path, timeout).await?;
    file.unlock()
        .wrap_err_with(|| format!("failed to unlock {}", path.display()))?;
    Ok(())
}

async fn request_shutdown(settings: &Settings) {
    if let Ok(mut client) = connect_client(settings).await {
        drop(client.shutdown().await);
    }
}

fn startup_timeout(settings: &Settings) -> Duration {
    Duration::from_secs_f64(settings.local_timeout.max(0.5) + 2.0)
}

async fn status_cmd(settings: &Settings) -> Result<()> {
    match probe(settings).await {
        Probe::Ready(mut client) => {
            let status = client.status().await?;
            println!("Daemon running");
            println!("  PID:      {}", status.pid);
            println!("  Version:  {}", status.version);
            println!("  Protocol: {}", status.protocol);
            println!("  Healthy:  {}", status.healthy);
            println!("  Socket:   {}", settings.daemon.socket_path);
        }
        Probe::NeedsRestart(reason) => {
            println!("Daemon running (needs restart)");
            println!("  Reason: {reason}");
        }
        Probe::Unreachable(_) => {
            println!("Daemon is not running");
        }
    }

    Ok(())
}

async fn stop_cmd(settings: &Settings) -> Result<()> {
    let Ok(mut client) = connect_client(settings).await else {
        println!("Daemon is not running");
        return Ok(());
    };

    match client.shutdown().await {
        Ok(true) => {
            println!("Shutdown requested");

            let pidfile_path = PathBuf::from(&settings.daemon.pidfile_path);
            let timeout = Duration::from_secs(5);
            match wait_for_pidfile_available(&pidfile_path, timeout).await {
                Ok(()) => println!("Daemon stopped"),
                Err(_) => println!("Daemon may still be shutting down"),
            }

            Ok(())
        }
        Ok(false) => bail!("Daemon rejected shutdown request"),
        Err(err) => Err(err.wrap_err("Failed to send shutdown request")),
    }
}

async fn start_cmd(
    settings: Settings,
    store: SqliteStore,
    history_db: ClientSqlite,
    show_logs: bool,
) -> Result<()> {
    let pidfile_path = PathBuf::from(&settings.daemon.pidfile_path);
    let _pidfile_guard = PidfileGuard::acquire(&pidfile_path)?;

    turtle_daemon::boot(settings, store, history_db).await?;

    Ok(())
}

#[cfg(test)]
mod tests {
    use super::{
        DAEMON_PROTOCOL_VERSION, DAEMON_VERSION, daemon_matches_expected, daemon_mismatch_message,
    };

    #[test]
    fn test_version_matches() {
        assert!(daemon_matches_expected(
            DAEMON_VERSION,
            DAEMON_PROTOCOL_VERSION
        ));
    }

    #[test]
    fn test_version_mismatch() {
        assert!(!daemon_matches_expected("0.0.0", DAEMON_PROTOCOL_VERSION));
        assert!(!daemon_matches_expected(DAEMON_VERSION, 999));
        assert!(!daemon_matches_expected("0.0.0", 999));
    }

    #[test]
    fn test_mismatch_message_version() {
        let msg = daemon_mismatch_message("0.0.0", DAEMON_PROTOCOL_VERSION);
        assert!(msg.contains("out of date"), "got: {msg}");
        assert!(msg.contains("0.0.0"));
        assert!(msg.contains(DAEMON_VERSION));
    }

    #[test]
    fn test_mismatch_message_protocol() {
        let msg = daemon_mismatch_message(DAEMON_VERSION, 999);
        assert!(msg.contains("protocol mismatch"), "got: {msg}");
    }
}