diff --git a/Cargo.lock b/Cargo.lock index 8765f99..69ce87b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1383,6 +1383,7 @@ dependencies = [ "rand", "redis", "reqwest", + "sd-notify", "serde", "serde_json", "smallvec", @@ -2039,6 +2040,12 @@ dependencies = [ "untrusted", ] +[[package]] +name = "sd-notify" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "621e3680f3e07db4c9c2c3fb07c6223ab2fab2e54bd3c04c3ae037990f428c32" + [[package]] name = "semver" version = "0.10.0" diff --git a/Cargo.toml b/Cargo.toml index af2d696..a1ad8ff 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -32,6 +32,7 @@ derivative = "2.2.0" nextcloud-config-parser = { version = "0.10.0", features = ["redis-connect"] } url = "2.5.0" clap = { version = "4.5.4", features = ["derive"] } +sd-notify = { version = "0.4.1", optional = true } [dev-dependencies] mini-redis = "0.4.1" @@ -49,3 +50,6 @@ opt-level = 3 lto = true [workspace] + +[features] +systemd = ["dep:sd-notify"] diff --git a/README.md b/README.md index d8f93d8..0623fbc 100644 --- a/README.md +++ b/README.md @@ -60,12 +60,15 @@ Documentation=https://github.com/nextcloud/notify_push [Service] Environment = PORT=7867 # Change if you already have something running on this port ExecStart = /path/to/push/binary/notify_push /path/to/nextcloud/config/config.php +Type=notify # requires the push server to have been build with the systemd feature User=www-data [Install] WantedBy = multi-user.target ``` +If the push server has not been compiled with the optional systemd feature the `Type=notify` line has to be removed. + #### OpenRC For OpenRC based setups, you can create an OpenRC service by creating a file named `/etc/init.d/notify_push` with the following content. diff --git a/src/error.rs b/src/error.rs index 0a17010..68913e0 100644 --- a/src/error.rs +++ b/src/error.rs @@ -26,6 +26,9 @@ pub enum Error { Authentication(#[from] AuthenticationError), #[error("Error while communicating with Nextcloud: {0}")] NextCloud(#[from] NextCloudError), + #[cfg(feature = "systemd")] + #[error("Failed to notify SystemD: {0}")] + SystemD(#[from] std::io::Error), } #[derive(Debug, Error, Diagnostic)] diff --git a/src/main.rs b/src/main.rs index 20de109..7f24c90 100644 --- a/src/main.rs +++ b/src/main.rs @@ -6,6 +6,8 @@ use notify_push::error::ConfigError; use notify_push::message::DEBOUNCE_ENABLE; use notify_push::metrics::serve_metrics; use notify_push::{listen_loop, serve, App, Error}; +#[cfg(feature = "systemd")] +use sd_notify; use std::sync::atomic::Ordering; use std::sync::Arc; use tokio::select; @@ -97,6 +99,10 @@ async fn run(config: Config, log_handle: LoggerHandle) -> Result<()> { )?); } + // tell SystemD that sockets have been bound to their addresses + #[cfg(feature = "systemd")] + sd_notify::notify(true, &[sd_notify::NotifyState::Ready]).map_err(Error::SystemD)?; + spawn(listen_loop(app, listen_cancel_handle)); // wait for either a sigint or sigterm