-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Expand file tree
/
Copy pathsystemd.rs
More file actions
48 lines (42 loc) · 1.52 KB
/
systemd.rs
File metadata and controls
48 lines (42 loc) · 1.52 KB
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
//! Systemd integration via `sd_notify`
//! See <https://www.freedesktop.org/software/systemd/man/latest/sd_notify.html>
use tokio::time::interval;
/// Sends `READY=1` to systemd via sd_notify. No-op if not Type=notify.
pub fn sd_notify_ready() {
if let Err(error) = sd_notify::notify(&[sd_notify::NotifyState::Ready]) {
warn!(message = "Failed to notify systemd of ready state.", %error);
}
}
/// Sends `STOPPING=1` to systemd via sd_notify. No-op if not Type=notify.
pub fn sd_notify_stopping() {
if let Err(error) = sd_notify::notify(&[sd_notify::NotifyState::Stopping]) {
warn!(message = "Failed to notify systemd of stopping state.", %error);
}
}
/// Sends `WATCHDOG=1` to systemd via sd_notify. No-op if not Type=notify.
pub fn sd_notify_watchdog() {
if let Err(error) = sd_notify::notify(&[sd_notify::NotifyState::Watchdog]) {
warn!(message = "Failed to send systemd watchdog ping.", %error);
}
}
/// Sends `WATCHDOG=1` pings at half the `WatchdogSec` interval. No-op if not set.
pub async fn watchdog() {
let Some(duration) = sd_notify::watchdog_enabled() else {
return;
};
let mut ticker = interval(duration / 2);
loop {
ticker.tick().await;
sd_notify_watchdog();
}
}
#[cfg(test)]
mod tests {
#[test]
fn sd_notify_no_socket_does_not_panic() {
// NOTIFY_SOCKET is not set in test environments - these must be no-ops.
super::sd_notify_ready();
super::sd_notify_stopping();
super::sd_notify_watchdog();
}
}