agentmux_srv/server/
drone_resume.rs1use std::collections::HashSet;
15use std::sync::Arc;
16
17use crate::backend::jobs::RunLedger;
18use crate::backend::storage::drone_store::DroneStore;
19use crate::drone::checkpoint::{Checkpoint, GraphRef};
20use crate::drone::executor::resume_drone;
21use crate::drone::recorder::{load, Persist};
22use crate::drone::types::{DroneGraph, DroneRun};
23use crate::drone::versions::{graph_hash, DroneVersionStore};
24use crate::server::AppState;
25
26pub(crate) async fn resume_runs(state: &AppState, ledger: Option<&Arc<RunLedger>>) -> HashSet<String> {
31 let mut resumed = HashSet::new();
32 let running = match state.mstore.drone_runs_running() {
33 Ok(rows) => rows,
34 Err(e) => {
35 tracing::warn!(error = %e, "drone: couldn't read the runs left running");
36 return resumed;
37 }
38 };
39 for run in running {
40 match checkpoint_and_graph(state, &run) {
41 Ok(Some(_)) if ledger.is_some_and(|l| l.close_if_past_timeout(&run.id)) => {
42 tracing::info!(run_id = %run.id, "drone: a run is not taken up again: its job's timeout ran out while AgentMux was stopped");
43 }
44 Ok(Some((checkpoint, graph))) => {
45 let persist = Persist { sink: state.mstore.clone(), graph: checkpoint.graph.clone() };
46 let step = checkpoint.step;
47 let replay = match crate::server::drone_replay::replay_for(state, &checkpoint) {
49 Ok(replay) => replay,
50 Err(why) => {
51 tracing::warn!(run_id = %run.id, "drone: a replay is not taken up again: {why}");
52 continue;
53 }
54 };
55 match resume_drone(graph, checkpoint, persist, replay).await {
56 Ok(handle) => {
57 let ledger_run = ledger.and_then(|l| l.open_run_of_drone(&run.id).map(|id| (l.clone(), id)));
58 tracing::info!(run_id = %run.id, drone_id = %run.drone_id, step, "drone: taking a run up again after the restart");
59 crate::server::drone_handlers::spawn_run_drain(state, handle, run.drone_id.clone(), run.started_at, ledger_run);
60 resumed.insert(run.id);
61 }
62 Err(e) => tracing::warn!(run_id = %run.id, error = %e, "drone: a run could not be taken up again"),
63 }
64 }
65 Ok(None) => {}
66 Err(why) => tracing::warn!(run_id = %run.id, drone_id = %run.drone_id, "drone: a run is not taken up again: {why}"),
67 }
68 }
69 resumed
70}
71
72fn checkpoint_and_graph(state: &AppState, run: &DroneRun) -> Result<Option<(Checkpoint, DroneGraph)>, String> {
75 let rows = state.mstore.drone_checkpoints_for(&run.id).map_err(|e| format!("its checkpoints cannot be read: {e}"))?;
76 let Some(checkpoint) = load(&rows).map_err(|e| format!("its checkpoints are unusable: {e}"))? else { return Ok(None) };
77 let graph = graph_of(state, &run.drone_id, &checkpoint.graph)?;
78 Ok(Some((checkpoint, graph)))
79}
80
81pub(crate) fn graph_of(state: &AppState, drone_id: &str, graph: &GraphRef) -> Result<DroneGraph, String> {
86 if graph.version > 0 {
87 if let Ok(Some(v)) = state.id_store.version_get(drone_id, graph.version) {
88 if v.hash == graph.hash {
89 return Ok(v.graph);
90 }
91 }
92 }
93 match state.id_store.drone_get(drone_id) {
94 Ok(Some(d)) if graph_hash(&d.graph) == graph.hash => Ok(d.graph),
95 Ok(Some(_)) => Err("the drone was edited since, and the graph the run started on was not kept".to_string()),
96 Ok(None) => Err("the drone no longer exists".to_string()),
97 Err(e) => Err(format!("the drone cannot be read: {e}")),
98 }
99}
100
101#[cfg(test)]
102#[path = "drone_resume_tests.rs"]
103mod tests;