orch-cli 0.118.1

Orch, the agent orchestration engine: runs coding agents on tasks and cron jobs
Documentation
//! This node's entry in the cluster's node registry, written on start, on config reload and on a
//! heartbeat, and the leases on the runs it holds

use std::collections::HashSet;
use std::sync::Arc;

use chrono::Utc;
use tokio::sync::RwLock;

use crate::config;
use crate::engine::router::Router;
use crate::store::TaskStore;
use crate::store::cluster::lease::Lease;
use crate::store::cluster::registry::{self, Entry};

/// Write the entry in the background, so a write that waits for a leader never stalls the tick
pub(crate) async fn publish(
    store: &Arc<TaskStore>,
    repos: &HashSet<String>,
    router: &Arc<RwLock<Router>>,
    max_parallel: usize,
) {
    let Some(node) = store.node() else {
        return;
    };
    let mut projects: Vec<String> = repos.iter().cloned().collect();
    projects.sort();
    let entry = Entry {
        node: node.name().to_string(),
        projects,
        agents: router.read().await.available_agents.clone(),
        max_parallel,
        version: node.version(),
        witness: config::get("cluster.witness").is_ok_and(|w| w == "true"),
        seen: Utc::now().to_rfc3339(),
    };
    let store = Arc::clone(store);
    tokio::spawn(async move {
        if let Err(e) = registry::write(&store, &entry).await {
            tracing::warn!(err = %e, "failed to write this node's registry entry");
        }
    });
}

/// Renew this node's lease on the runs it holds, in the background like the registry entry
pub(crate) fn renew_leases(store: &Arc<TaskStore>, lease: Lease) {
    let store = Arc::clone(store);
    tokio::spawn(async move {
        match store.renew_leases(lease.ttl_secs()).await {
            Ok(n) if n > 0 => tracing::debug!(renewed = n, "renewed this node's leases"),
            Ok(_) => {}
            Err(e) => tracing::warn!(err = %e, "failed to renew this node's leases"),
        }
    });
}