use anyhow::{Context, Result};
use sd_notify::NotifyState;
use tokio::task::JoinSet;
use tokio::time::MissedTickBehavior;
pub fn ready() -> Result<()> {
sd_notify::notify(&[NotifyState::Ready]).context("Failed to send ready notification")
}
pub fn stopping() -> Result<()> {
sd_notify::notify(&[NotifyState::Stopping]).context("Failed to send stopping notification")
}
pub fn status(status: &str) -> Result<()> {
sd_notify::notify(&[NotifyState::Status(status)]).context("Failed to send status notification")
}
pub fn start_watchdog(set: &mut JoinSet<()>) {
let watchdog = sd_notify::watchdog_enabled();
if let Some(period) = watchdog {
set.spawn(async move {
let mut interval = tokio::time::interval(period / 2);
interval.set_missed_tick_behavior(MissedTickBehavior::Delay);
loop {
interval.tick().await;
sd_notify::notify(&[NotifyState::Watchdog])
.context("Failed to send watchdog notification")
.unwrap();
}
});
}
}