kayak 0.3.0

A virtual IP manager using etcd
// SPDX-License-Identifier: MPL-2.0

use anyhow::{Context, Result};
use sd_notify::NotifyState;
use tokio::task::JoinSet;
use tokio::time::MissedTickBehavior;

// These are no-ops if systemd isn't used

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 {
            // Recommended notification period
            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();
            }
        });
    }
}