systemd Ready/Stopping notification

This commit is contained in:
Scott Lamb 2023-10-19 21:34:28 -07:00
parent ee98bf5236
commit 89ee2d0269
5 changed files with 45 additions and 1 deletions

View File

@ -8,6 +8,10 @@ upgrades, e.g. `v0.6.x` -> `v0.7.x`. The config file format and
[API](ref/api.md) currently have no stability guarantees, so they may change
even on minor releases, e.g. `v0.7.5` -> `v0.7.6`.
## unreleased
* On Linux, notify `systemd` of starting/stopping.
## v0.7.8 (2023-10-18)
* release as self-contained Linux binaries (for `x86_64`, `aarch64`, and

View File

@ -212,7 +212,8 @@ Environment=TZ=:/etc/localtime
Environment=MOONFIRE_FORMAT=systemd
Environment=MOONFIRE_LOG=info
Environment=RUST_BACKTRACE=1
Type=simple
Type=notify
TimeoutStartSec=300 # large installations take a while to scan the sample file dirs
User=moonfire-nvr
Restart=on-failure
CPUAccounting=true

19
server/Cargo.lock generated
View File

@ -965,6 +965,24 @@ dependencies = [
"vcpkg",
]
[[package]]
name = "libsystemd"
version = "0.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "88b9597a67aa1c81a6624603e6bd0bcefb9e0f94c9c54970ec53771082104b4e"
dependencies = [
"hmac",
"libc",
"log",
"nix",
"nom",
"once_cell",
"serde",
"sha2",
"thiserror",
"uuid",
]
[[package]]
name = "linux-raw-sys"
version = "0.4.3"
@ -1125,6 +1143,7 @@ dependencies = [
"hyper",
"itertools",
"libc",
"libsystemd",
"log",
"memchr",
"moonfire-base",

View File

@ -75,6 +75,9 @@ uuid = { version = "1.1.2", features = ["serde", "std", "v4"] }
flate2 = "1.0.26"
git-version = "0.3.5"
[target.'cfg(target_os = "linux")'.dependencies]
libsystemd = "0.6.0"
[build-dependencies]
blake3 = "1.0.0"
fnv = "1.0"

View File

@ -22,6 +22,9 @@ use tokio::signal::unix::{signal, SignalKind};
use tracing::error;
use tracing::{info, warn};
#[cfg(target_os = "linux")]
use libsystemd::daemon::{NotifyState, notify};
use self::config::ConfigFile;
pub mod config;
@ -402,9 +405,23 @@ async fn inner(
.collect();
let web_handles = web_handles?;
#[cfg(target_os = "linux")]
{
if let Err(err) = notify(false, &[NotifyState::Ready]) {
tracing::warn!(%err, "unable to notify systemd on ready");
}
}
info!("Ready to serve HTTP requests");
shutdown_rx.as_future().await;
#[cfg(target_os = "linux")]
{
if let Err(err) = notify(false, &[NotifyState::Stopping]) {
tracing::warn!(%err, "unable to notify systemd on stopping");
}
}
info!("Shutting down streamers and syncers.");
tokio::task::spawn_blocking({
let db = db.clone();