Abstract over sd_notify

This commit is contained in:
Lambda 2024-09-26 16:27:58 -07:00 committed by Charles Hall
parent 6ab87f97dd
commit 39880cc6ac
No known key found for this signature in database
GPG key ID: 7B8E0645816E07CF
2 changed files with 30 additions and 9 deletions

View file

@ -42,12 +42,12 @@ use crate::{
}, },
config::{self, Config, ListenComponent, ListenTransport}, config::{self, Config, ListenComponent, ListenTransport},
database::KeyValueDatabase, database::KeyValueDatabase,
error, observability, services, error, observability, services, set_application_state,
utils::{ utils::{
self, self,
error::{Error, Result}, error::{Error, Result},
}, },
Services, ApplicationState, Services,
}; };
pub(crate) async fn run(args: ServeArgs) -> Result<(), error::ServeCommand> { pub(crate) async fn run(args: ServeArgs) -> Result<(), error::ServeCommand> {
@ -226,9 +226,7 @@ async fn run_server() -> Result<(), error::Serve> {
} }
} }
#[cfg(feature = "systemd")] set_application_state(ApplicationState::Ready);
sd_notify::notify(true, &[sd_notify::NotifyState::Ready])
.expect("should be able to notify systemd");
tokio::spawn(shutdown_signal(handles)); tokio::spawn(shutdown_signal(handles));
@ -573,9 +571,7 @@ async fn shutdown_signal(handles: Vec<ServerHandle>) {
handle.graceful_shutdown(Some(Duration::from_secs(30))); handle.graceful_shutdown(Some(Duration::from_secs(30)));
} }
#[cfg(feature = "systemd")] set_application_state(ApplicationState::Stopping);
sd_notify::notify(true, &[sd_notify::NotifyState::Stopping])
.expect("should be able to notify systemd");
} }
async fn federation_disabled(_: Uri) -> impl IntoResponse { async fn federation_disabled(_: Uri) -> impl IntoResponse {

View file

@ -5,7 +5,7 @@
use std::process::ExitCode; use std::process::ExitCode;
use clap::Parser; use clap::Parser;
use tracing::error; use tracing::{error, info};
mod api; mod api;
mod cli; mod cli;
@ -41,6 +41,31 @@ fn version() -> String {
} }
} }
#[derive(Debug, Clone, Copy)]
enum ApplicationState {
Ready,
Stopping,
}
fn set_application_state(state: ApplicationState) {
info!(?state, "Application state changed");
#[cfg(feature = "systemd")]
{
use sd_notify::NotifyState;
fn notify(states: &[NotifyState<'_>]) {
sd_notify::notify(false, states)
.expect("should be able to notify systemd");
}
match state {
ApplicationState::Ready => notify(&[NotifyState::Ready]),
ApplicationState::Stopping => notify(&[NotifyState::Stopping]),
};
}
}
#[tokio::main] #[tokio::main]
async fn main() -> ExitCode { async fn main() -> ExitCode {
let args = cli::Args::parse(); let args = cli::Args::parse();