Files
tessera/crates/tessera-hub/src/scheduler.rs
Tessera Agent a5ffb2c1b4 Initial Tessera backup vault (M0–M2)
Scaffold Rust workspace, React canvas, vault engine, hub API,
providers, agent wormhole path, and Docker compose per SPEC.
2026-11-09 09:59:42 +00:00

47 lines
1.4 KiB
Rust

use crate::state::HubState;
use anyhow::Result;
use cron::Schedule;
use std::str::FromStr;
use tokio::time::{sleep, Duration};
use tracing::{error, info};
pub async fn start_orbit_scheduler(state: HubState) -> Result<()> {
tokio::spawn(async move {
loop {
if let Err(e) = tick_schedules(state.clone()).await {
error!("scheduler tick: {e}");
}
sleep(Duration::from_secs(30)).await;
}
});
Ok(())
}
async fn tick_schedules(state: HubState) -> Result<()> {
let orbits = state.db.list_orbits().await?;
let now = chrono::Utc::now();
for orbit in orbits {
let Some(cron_expr) = orbit.schedule.as_ref() else {
continue;
};
let schedule = match Schedule::from_str(cron_expr) {
Ok(s) => s,
Err(e) => {
error!("bad cron on orbit {}: {e}", orbit.name);
continue;
}
};
let upcoming = schedule.upcoming(chrono::Utc).take(1).collect::<Vec<_>>();
if let Some(next) = upcoming.first() {
let delta = (*next - now).num_seconds().abs();
if delta < 35 {
info!("scheduler firing orbit {}", orbit.name);
if let Err(e) = crate::api::run_orbit_internal(state.clone(), orbit.id).await {
error!("orbit {} failed: {e}", orbit.name);
}
}
}
}
Ok(())
}