spark-debugger
Native runtime state inspector and execution debugger
Brought by drakestapleton
spark-debugger / src / daemon.rs
| 1 | use reqwest::Client; |
| 2 | use std::time::Duration; |
| 3 | use tokio::time::sleep; |
| 4 | use tracing::{info, warn}; |
| 5 | |
| 6 | use crate::audit::run_full_audit; |
| 7 | use crate::reporter::report_incident; |
| 8 | |
| 9 | pub async fn run_watchdog_daemon( |
| 10 | client: Client, |
| 11 | workspace_dir: String, |
| 12 | interval_secs: u64, |
| 13 | ) -> Result<(), Box<dyn std::error::Error>> { |
| 14 | info!("[Watchdog] Starting spark-debugger watchdog daemon (interval: {}s)...", interval_secs); |
| 15 | |
| 16 | let mut consecutive_failures = 0; |
| 17 | |
| 18 | loop { |
| 19 | let report = run_full_audit(&client, &workspace_dir).await; |
| 20 | |
| 21 | if !report.overall_healthy { |
| 22 | consecutive_failures += 1; |
| 23 | let mut issues = Vec::new(); |
| 24 | for s in &report.services { |
| 25 | if !s.reachable { |
| 26 | issues.push(format!("Service {} unreachable at {}", s.name, s.endpoint)); |
| 27 | } |
| 28 | } |
| 29 | if !report.invariants.passed { |
| 30 | issues.push("Vault or unslop invariants violated".to_string()); |
| 31 | } |
| 32 | if !report.build.passed { |
| 33 | issues.push(format!("Workspace build failed with {} errors", report.build.errors.len())); |
| 34 | } |
| 35 | |
| 36 | let summary = issues.join("; "); |
| 37 | warn!("[Watchdog] Audit failed (attempt {}): {}", consecutive_failures, summary); |
| 38 | |
| 39 | if consecutive_failures == 1 || consecutive_failures % 5 == 0 { |
| 40 | report_incident(&client, &report, &summary).await; |
| 41 | } |
| 42 | } else { |
| 43 | if consecutive_failures > 0 { |
| 44 | info!("[Watchdog] All systems recovered to healthy state."); |
| 45 | } |
| 46 | consecutive_failures = 0; |
| 47 | info!("[Watchdog] All 5 services and workspace invariants OK."); |
| 48 | } |
| 49 | |
| 50 | tokio::select! { |
| 51 | _ = tokio::signal::ctrl_c() => { |
| 52 | info!("[Watchdog] Received termination signal. Halting daemon."); |
| 53 | break; |
| 54 | } |
| 55 | _ = sleep(Duration::from_secs(interval_secs)) => {} |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | Ok(()) |
| 60 | } |