The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.
Rust agent-orchestrationai-agentsai-assistantdesktopllmlocal-firstmcppersonal-aiprivacyrustsecond-braintauri
fix(cron): prevent duplicate job execution on scheduler restart
Steven Enamakelf8eb6b7e80d8
diff --git a/crates/openhuman-core/src/cron/ops.rs b/crates/openhuman-core/src/cron/ops.rsindex b49fa2afda..9119843b0f 100644--- a/crates/openhuman-core/src/cron/ops.rs+++ b/crates/openhuman-core/src/cron/ops.rs@@ -18,7 +18,7 @@ static ACTIVE_RUNS: Lazy<Mutex<HashSet<String>>> = Lazy::new(|| Mutex::new(HashS////// Ensures cleanup runs on normal completion, panic, or future cancellation —/// so a hung or aborted background task can never permanently lock a job_id.-struct ActiveRunGuard {+pub(crate) struct ActiveRunGuard {job_id: String,}@@ -30,6 +30,18 @@ impl Drop for ActiveRunGuard {}}+/// Claim `job_id` as running. `None` when a run (scheduled or Run Now) already+/// holds it; the claim is released when the guard drops.+pub(crate) fn try_acquire_run(job_id: &str) -> Option<ActiveRunGuard> {+ let mut active = ACTIVE_RUNS.lock().ok()?;+ if !active.insert(job_id.to_string()) {+ return None;+ }+ Some(ActiveRunGuard {+ job_id: job_id.to_string(),+ })+}+pub fn add_once(config: &Config, delay: &str, command: &str) -> Result<CronJob> {let duration = parse_human_delay(delay)?;let at = chrono::Utc::now() + duration;@@ -258,8 +270,10 @@ pub async fn cron_run(config: &Config, job_id: &str) -> Result<Outcome<serde_jsotracing::debug!(job_id = %job_id_owned, "[cron_run] background task started");+ let run_id = uuid::Uuid::new_v4().to_string();let started_at = chrono::Utc::now();- let (success, output) = cron::scheduler::execute_job_now(&config_owned, &job).await;+ let (success, output) =+ cron::scheduler::execute_job_now(&config_owned, &job, &run_id).await;let finished_at = chrono::Utc::now();let duration_ms = (finished_at - started_at).num_milliseconds();let status = if success { "ok" } else { "error" };@@ -275,7 +289,13 @@ pub async fn cron_run(config: &Config, job_id: &str) -> Result<Outcome<serde_jso// so we don't leave orphaned rows in the run history.let _ = cron::delete_queued_runs(&config_owned, &job.id);- let _ = cron::record_run(+ // Deliver via the same path as the scheduler loop so proactive+ // messages, origin replies and alerts are sent on "Run Now" too, then+ // record the run with how the delivery went.+ let delivery_status =+ cron::scheduler::deliver_job(&config_owned, &job, &run_id, &output).await;++ let _ = cron::record_run_with_delivery(&config_owned,&job.id,started_at,@@ -283,12 +303,9 @@ pub async fn cron_run(config: &Config, job_id: &str) -> Result<Outcome<serde_jsostatus,Some(&output),duration_ms,+ Some(delivery_status),);let _ = cron::record_last_run(&config_owned, &job.id, finished_at, success, &output);-- // Deliver via the same path as the scheduler loop so proactive- // messages and alerts are sent on "Run Now" too.- cron::scheduler::deliver_job(&config_owned, &job, &output).await;});Ok(Outcome::new(diff --git a/crates/openhuman-core/src/cron/scheduler.rs b/crates/openhuman-core/src/cron/scheduler.rsindex 9a4ad999cc..8e87f3fa2f 100644--- a/crates/openhuman-core/src/cron/scheduler.rs+++ b/crates/openhuman-core/src/cron/scheduler.rs@@ -165,10 +165,25 @@ pub(crate) async fn tick_once(async fn process_due_jobs(config: &Config, security: &Arc<SecurityPolicy>, jobs: Vec<CronJob>) {let max_concurrent = config.scheduler.max_concurrent.max(1);- let mut in_flight = stream::iter(jobs.into_iter().map(|job| {+ // A job already running (a long previous tick, or Run Now) is skipped: the+ // next tick picks it up once the claim is released.+ let claimed: Vec<_> = jobs+ .into_iter()+ .filter_map(|job| match crate::cron::ops::try_acquire_run(&job.id) {+ Some(guard) => Some((job, guard)),+ None => {+ tracing::debug!(job_id = %job.id, "[cron:scheduler] skipping job: a run is already active");+ None+ }+ })+ .collect();+ let mut in_flight = stream::iter(claimed.into_iter().map(|(job, guard)| {let config = config.clone();let security = Arc::clone(security);- async move { execute_and_persist_job(&config, security.as_ref(), &job).await }+ async move {+ let _guard = guard;+ execute_and_persist_job(&config, security.as_ref(), &job).await+ }})).buffer_unordered(max_concurrent);@@ -204,11 +219,14 @@ async fn execute_and_persist_job(job_type: format!("{:?}", job.job_type),});- let (execution_success, output) = execute_job_with_retry(config, security, job).await;+ let run_id = uuid::Uuid::new_v4().to_string();+ let (execution_success, output) =+ execute_job_with_retry_for_run(config, security, job, &run_id).await;let finished_at = Utc::now();- let success = persist_job_result(+ let success = persist_job_result_for_run(config,job,+ &run_id,execution_success,&output,started_at,diff --git a/crates/openhuman-core/src/cron/tools/run.rs b/crates/openhuman-core/src/cron/tools/run.rsindex 468155ca3f..fdf7669650 100644--- a/crates/openhuman-core/src/cron/tools/run.rs+++ b/crates/openhuman-core/src/cron/tools/run.rs@@ -89,7 +89,9 @@ impl Tool for CronRunTool {};let started_at = Utc::now();- let (success, output) = cron::scheduler::execute_job_now(&self.config, &job).await;+ let run_id = uuid::Uuid::new_v4().to_string();+ let (success, output) =+ cron::scheduler::execute_job_now(&self.config, &job, &run_id).await;let finished_at = Utc::now();let duration_ms = (finished_at - started_at).num_milliseconds();let status = if success { "ok" } else { "error" };diff --git a/vendor/tinyagents b/vendor/tinyagentsindex 15f64c647c..986eee5753 160000--- a/vendor/tinyagents+++ b/vendor/tinyagents@@ -1 +1 @@-Subproject commit 15f64c647c64eb17f7b7e7334d992611d14563dc+Subproject commit 986eee5753ec5eeb3d62d37d8eb8a9bf45827843