Skip to content

Commit

Permalink
add support for notifying SystemD when the push server is ready
Browse files Browse the repository at this point in the history
This requires the optional feature `systemd` to be enabled and allows
the startup of units depending on e.g. Unix domain sockets to be delayed
until these are created.

Signed-off-by: Eric Wolf <[email protected]>
  • Loading branch information
Deric-W committed May 25, 2024
1 parent 590e91e commit 3cb19c8
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 0 deletions.
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -49,3 +50,6 @@ opt-level = 3
lto = true

[workspace]

[features]
systemd = ["dep:sd-notify"]
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
3 changes: 3 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down
6 changes: 6 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 3cb19c8

Please sign in to comment.