second-brain-api 0.13.1

HTTP API server for second-brain: REST endpoints for recall, remember, and ingest
Documentation
//! Posts each health state change to `[alert] webhook_url`, retrying a
//! failed post until it lands or a newer state replaces it.

use std::time::Duration;

use reqwest::StatusCode;

use second_brain_core::config::AlertConfig;
use tokio::sync::mpsc::{UnboundedReceiver, UnboundedSender, unbounded_channel};

use crate::health::Transition;

const FIRST_RETRY: Duration = Duration::from_secs(1);
const LAST_RETRY: Duration = Duration::from_secs(5 * 60);

fn payload(transition: &Transition, machine: &str) -> serde_json::Value {
    serde_json::json!({
        "state": transition.state,
        "reason": transition.reason,
        "machine": machine,
        "at": transition.at,
    })
}

/// The sender to hand `Health::new`, or None when no webhook is set, so
/// nothing is ever queued or sent.
pub fn start(config: &AlertConfig, machine: String) -> Option<UnboundedSender<Transition>> {
    let url = config.webhook_url.clone()?;
    let (tx, rx) = unbounded_channel();
    tokio::spawn(deliver(url, machine, rx));
    Some(tx)
}

async fn deliver(url: String, machine: String, mut transitions: UnboundedReceiver<Transition>) {
    let client = match reqwest::Client::builder()
        .timeout(Duration::from_secs(10))
        .build()
    {
        Ok(c) => c,
        Err(e) => {
            tracing::error!("alert webhook disabled, cannot build an HTTP client: {e}");
            return;
        }
    };
    while let Some(mut transition) = transitions.recv().await {
        let mut wait = FIRST_RETRY;
        while post(&client, &url, &payload(&transition, &machine)).await == Posted::Retry {
            tokio::select! {
                newer = transitions.recv() => match newer {
                    Some(newer) => {
                        transition = newer;
                        while let Ok(newest) = transitions.try_recv() {
                            transition = newest;
                        }
                    }
                    None => return,
                },
                _ = tokio::time::sleep(wait) => wait = (wait * 2).min(LAST_RETRY),
            }
        }
    }
}

#[derive(PartialEq)]
enum Posted {
    Done,
    Retry,
}

/// A 4xx other than 429 will not change on retry, so it is logged once.
async fn post(client: &reqwest::Client, url: &str, body: &serde_json::Value) -> Posted {
    match client.post(url).json(body).send().await {
        Ok(r) if r.status().is_success() => Posted::Done,
        Ok(r) if r.status().is_client_error() && r.status() != StatusCode::TOO_MANY_REQUESTS => {
            tracing::warn!("alert webhook refused the post with {}", r.status());
            Posted::Done
        }
        Ok(r) => {
            tracing::warn!("alert webhook answered {}; retrying", r.status());
            Posted::Retry
        }
        Err(e) => {
            tracing::warn!("alert webhook failed: {e}; retrying");
            Posted::Retry
        }
    }
}