feat: add managed database capture and operator restore
PostgreSQL and MySQL backups run through paired outposts, with operator sign-in, off-site copies, and legacy import. Retention stays on each orbit, so the unused retention console is removed. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -1,46 +1,130 @@
|
||||
use crate::state::HubState;
|
||||
use anyhow::Result;
|
||||
use anyhow::{Context, Result};
|
||||
use chrono::{DateTime, NaiveTime, TimeZone, Utc};
|
||||
use chrono_tz::Tz;
|
||||
use cron::Schedule;
|
||||
use std::str::FromStr;
|
||||
use tessera_core::job::CaptureSchedule;
|
||||
use tokio::time::{sleep, Duration};
|
||||
use tracing::{error, info};
|
||||
|
||||
pub async fn start_orbit_scheduler(state: HubState) -> Result<()> {
|
||||
let db = state.db.clone();
|
||||
tokio::spawn(async move {
|
||||
loop {
|
||||
if let Err(e) = tick_schedules(state.clone()).await {
|
||||
if let Err(e) = tick_capture_schedules(&db, Utc::now()) {
|
||||
error!("capture scheduler tick: {e}");
|
||||
}
|
||||
sleep(Duration::from_secs(30)).await;
|
||||
}
|
||||
});
|
||||
tokio::spawn(async move {
|
||||
let mut previous = Utc::now();
|
||||
loop {
|
||||
let now = Utc::now();
|
||||
if let Err(e) = tick_schedules(state.clone(), previous, now).await {
|
||||
error!("scheduler tick: {e}");
|
||||
}
|
||||
previous = now;
|
||||
sleep(Duration::from_secs(30)).await;
|
||||
}
|
||||
});
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn tick_schedules(state: HubState) -> Result<()> {
|
||||
pub(crate) fn orbit_schedule(expression: &str) -> Result<Schedule, cron::error::Error> {
|
||||
if expression.split_whitespace().count() == 5 {
|
||||
Schedule::from_str(&format!("0 {expression}"))
|
||||
} else {
|
||||
Schedule::from_str(expression)
|
||||
}
|
||||
}
|
||||
|
||||
async fn tick_schedules(
|
||||
state: HubState,
|
||||
previous: DateTime<Utc>,
|
||||
now: DateTime<Utc>,
|
||||
) -> 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 {
|
||||
if orbit.provider == "wormhole" {
|
||||
continue;
|
||||
}
|
||||
let Some(cron_expr) = orbit
|
||||
.schedule
|
||||
.as_deref()
|
||||
.filter(|expr| !expr.trim().is_empty())
|
||||
else {
|
||||
continue;
|
||||
};
|
||||
let schedule = match Schedule::from_str(cron_expr) {
|
||||
let schedule = match orbit_schedule(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);
|
||||
}
|
||||
if schedule
|
||||
.after(&previous)
|
||||
.next()
|
||||
.is_some_and(|slot| slot <= now)
|
||||
{
|
||||
info!("scheduler firing orbit {}", orbit.name);
|
||||
let orbit_id = orbit.id;
|
||||
if let Err(e) = crate::api::run_orbit_internal(state.clone(), orbit).await {
|
||||
error!("orbit {orbit_id} failed: {e}");
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn capture_schedule(schedule: &CaptureSchedule) -> Option<(NaiveTime, Tz)> {
|
||||
let time = NaiveTime::from_hms_opt(u32::from(schedule.hour), u32::from(schedule.minute), 0)?;
|
||||
Some((time, schedule.timezone.parse().ok()?))
|
||||
}
|
||||
|
||||
pub(crate) fn tick_capture_schedules(db: &crate::db::Db, now: DateTime<Utc>) -> Result<()> {
|
||||
for (profile, cursor) in db.capture_schedule_cursors()? {
|
||||
let Some(schedule) = &profile.schedule else {
|
||||
continue;
|
||||
};
|
||||
let (time, tz) = capture_schedule(schedule).context("invalid stored capture schedule")?;
|
||||
let start = DateTime::from_timestamp(cursor, 0).context("invalid schedule cursor")?;
|
||||
let end = now.with_timezone(&tz).date_naive();
|
||||
let mut date = start.with_timezone(&tz).date_naive();
|
||||
for _ in 0..32 {
|
||||
if date > end {
|
||||
break;
|
||||
}
|
||||
if let Some(slot) = tz.from_local_datetime(&date.and_time(time)).earliest() {
|
||||
if slot.timestamp() > cursor && slot <= now {
|
||||
db.enqueue_scheduled_capture(
|
||||
profile.application_id,
|
||||
slot.timestamp(),
|
||||
now.timestamp(),
|
||||
)?;
|
||||
}
|
||||
}
|
||||
date = date.succ_opt().context("schedule date overflow")?;
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn orbit_cron_accepts_legacy_minutes_and_explicit_seconds() {
|
||||
let before = Utc.with_ymd_and_hms(2026, 11, 10, 1, 59, 55).unwrap();
|
||||
let slot = Utc.with_ymd_and_hms(2026, 11, 10, 2, 0, 0).unwrap();
|
||||
for expression in ["0 2 * * *", "0 0 2 * * *"] {
|
||||
let schedule = orbit_schedule(expression).unwrap();
|
||||
assert_eq!(schedule.after(&before).next(), Some(slot));
|
||||
assert!(schedule.after(&slot).next().unwrap() > slot);
|
||||
}
|
||||
assert!(orbit_schedule("60 2 * * *").is_err());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user