From 8d7ce89527a27ea7244a940e9b4c21e35db75489 Mon Sep 17 00:00:00 2001 From: wojnilowicz Date: Sat, 24 Aug 2024 14:21:56 +0200 Subject: [PATCH] fix(linux): add sd-notify for better systemd interop (#489) Currently, services dependent on aw-server.service (e.g. aw-awatcher) start just after its launch. This patch causes systemd to launch the dependent services only after the rocket server is up an running. It's aimed to help slow systems to not close the dependent service prematurely due to not being able to connect to the server. --- aw-server.service | 4 ++-- aw-server/Cargo.toml | 3 +++ aw-server/src/main.rs | 9 +++++++-- 3 files changed, 12 insertions(+), 4 deletions(-) diff --git a/aw-server.service b/aw-server.service index 75b53021..24ab545b 100644 --- a/aw-server.service +++ b/aw-server.service @@ -14,12 +14,12 @@ # [Service] -Type=simple +Type=notify ExecStart=aw-server [Unit] Description=ActivityWatch Server (Rust implementation) -Wants=network.target +After=network.target [Install] WantedBy=default.target diff --git a/aw-server/Cargo.toml b/aw-server/Cargo.toml index ea70b851..56c407e4 100644 --- a/aw-server/Cargo.toml +++ b/aw-server/Cargo.toml @@ -35,6 +35,9 @@ aw-models = { path = "../aw-models" } aw-transform = { path = "../aw-transform" } aw-query = { path = "../aw-query" } +[target.'cfg(target_os="linux")'.dependencies] +sd-notify = "0.4.2" + [target.'cfg(all(target_os="linux", target_arch="x86"))'.dependencies] jemallocator = "0.5.0" diff --git a/aw-server/src/main.rs b/aw-server/src/main.rs index 933366e6..2cbf39e7 100644 --- a/aw-server/src/main.rs +++ b/aw-server/src/main.rs @@ -9,6 +9,8 @@ use clap::Parser; use aw_server::*; +#[cfg(target_os = "linux")] +use sd_notify::NotifyState; #[cfg(all(target_os = "linux", target_arch = "x86"))] extern crate jemallocator; #[cfg(all(target_os = "linux", target_arch = "x86"))] @@ -147,9 +149,12 @@ async fn main() -> Result<(), rocket::Error> { device_id, }; - let _ = endpoints::build_rocket(server_state, config) - .launch() + let _rocket = endpoints::build_rocket(server_state, config) + .ignite() .await?; + #[cfg(target_os = "linux")] + let _ = sd_notify::notify(true, &[NotifyState::Ready]); + _rocket.launch().await?; Ok(()) }