Skip to content

The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.

chore: files changed crates/openhuman-core/src/cron/ops.rs,crates/openhuman-core/src/cron/ops_tests.

Steven Enamakelca597a926717
diff --git a/crates/openhuman-core/src/cron/ops.rs b/crates/openhuman-core/src/cron/ops.rs
index b49fa2afda..b979131b7d 100644
--- a/crates/openhuman-core/src/cron/ops.rs
+++ b/crates/openhuman-core/src/cron/ops.rs
@@ -236,6 +236,13 @@ pub async fn cron_run(config: &Config, job_id: &str) -> Result<Outcome<serde_jso
}
};
+ if let Err(refused) = refuse_if_single_flight_running(config, &job.id) {
+ if let Ok(mut active) = ACTIVE_RUNS.lock() {
+ active.remove(job_id);
+ }
+ return Err(refused);
+ }
+
// Insert a "queued" placeholder run record immediately so the frontend
// poller can observe the run as soon as the RPC returns — otherwise the
// run list stays unchanged until execute_job_now finishes.
@@ -257,38 +264,7 @@ pub async fn cron_run(config: &Config, job_id: &str) -> Result<Outcome<serde_jso
};
tracing::debug!(job_id = %job_id_owned, "[cron_run] background task started");
-
- let started_at = chrono::Utc::now();
- let (success, output) = cron::scheduler::execute_job_now(&config_owned, &job).await;
- let finished_at = chrono::Utc::now();
- let duration_ms = (finished_at - started_at).num_milliseconds();
- let status = if success { "ok" } else { "error" };
-
- tracing::debug!(
- job_id = %job_id_owned,
- status,
- duration_ms,
- "[cron_run] background task finished"
- );
-
- // Remove the "queued" placeholder before inserting the real result
- // so we don't leave orphaned rows in the run history.
- let _ = cron::delete_queued_runs(&config_owned, &job.id);
-
- let _ = cron::record_run(
- &config_owned,
- &job.id,
- started_at,
- finished_at,
- status,
- Some(&output),
- duration_ms,
- );
- 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;
+ run_and_record(&config_owned, &job).await;
});
Ok(Outcome::new(
@@ -300,6 +276,55 @@ pub async fn cron_run(config: &Config, job_id: &str) -> Result<Outcome<serde_jso
))
}
+/// Run job `job_id` now and wait for it: the same execution, retry budget,
+/// run record and delivery as a scheduled run. Refused while a single-flight
+/// job ([`crate::cron::policy::JobPolicy::single_flight`]) is already running.
+pub async fn run_job_now(config: &Config, job_id: &str) -> Result<(bool, String), String> {
+ let job = cron::get_job(config, job_id.trim()).map_err(|e| e.to_string())?;
+ refuse_if_single_flight_running(config, &job.id)?;
+ Ok(run_and_record(config, &job).await)
+}
+
+/// `Err` when `job_id` is single-flight and a run of it is in flight.
+fn refuse_if_single_flight_running(config: &Config, job_id: &str) -> Result<(), String> {
+ if crate::cron::scheduler::in_flight::is_running(job_id)
+ && crate::cron::policy::policy_or_default(config, job_id).single_flight
+ {
+ tracing::debug!(job_id, "[cron_run] single-flight job already running; refused");
+ return Err(format!("cron job '{job_id}' is already running"));
+ }
+ Ok(())
+}
+
+/// Execute `job` once (with its retry budget) and persist the run, its
+/// last-run state and its delivery, holding the job in flight meanwhile.
+async fn run_and_record(config: &Config, job: &CronJob) -> (bool, String) {
+ let _in_flight = crate::cron::scheduler::in_flight::enter(&job.id);
+ let started_at = chrono::Utc::now();
+ let (success, output) = cron::scheduler::execute_job_now(config, job).await;
+ let finished_at = chrono::Utc::now();
+ let duration_ms = (finished_at - started_at).num_milliseconds();
+ let status = if success { "ok" } else { "error" };
+ tracing::debug!(job_id = %job.id, status, duration_ms, "[cron_run] run finished");
+ // Remove a "queued" placeholder before inserting the real result so no
+ // orphaned rows are left in the run history.
+ let _ = cron::delete_queued_runs(config, &job.id);
+ let _ = cron::record_run(
+ config,
+ &job.id,
+ started_at,
+ finished_at,
+ status,
+ Some(&output),
+ duration_ms,
+ );
+ let _ = cron::record_last_run(config, &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, job, &output).await;
+ (success, output)
+}
+
pub async fn cron_runs(
config: &Config,
job_id: &str,
diff --git a/crates/openhuman-core/src/cron/ops_tests.rs b/crates/openhuman-core/src/cron/ops_tests.rs
index a7ff5dce0a..6a051b24cf 100644
--- a/crates/openhuman-core/src/cron/ops_tests.rs
+++ b/crates/openhuman-core/src/cron/ops_tests.rs
@@ -545,3 +545,64 @@ async fn cron_run_rejects_duplicate_concurrent_execution() {
"expected 'already running', got: {err}"
);
}
+
+// ── run_job_now / single-flight ─────────────────────────────────
+
+fn single_flight_system_job(config: &Config, name: &str) -> CronJob {
+ let job = crate::cron::system_jobs::ensure_system_job(
+ config,
+ name,
+ Schedule::Every { every_ms: 3_600_000 },
+ )
+ .unwrap();
+ crate::cron::policy::set_policy(
+ config,
+ &job.id,
+ crate::cron::policy::JobPolicy {
+ retries: Some(0),
+ single_flight: true,
+ },
+ )
+ .unwrap();
+ job
+}
+
+#[tokio::test]
+async fn run_job_now_runs_and_records_the_handler_result() {
+ let tmp = TempDir::new().unwrap();
+ let config = test_config(&tmp);
+ let _handler = crate::cron::system_job_handlers::register(
+ "ops-run-now",
+ std::sync::Arc::new(|_ctx| Box::pin(async { Err("nope".to_string()) })),
+ );
+ let job = single_flight_system_job(&config, "ops-run-now");
+ let (success, output) = run_job_now(&config, &job.id).await.unwrap();
+ assert!(!success);
+ assert!(output.contains("nope"), "{output}");
+ let runs = cron::list_runs(&config, &job.id, 10).unwrap();
+ assert_eq!(runs.len(), 1);
+ assert_eq!(runs[0].status, "error");
+ assert_eq!(
+ cron::get_job(&config, &job.id).unwrap().last_status.as_deref(),
+ Some("error")
+ );
+}
+
+#[tokio::test]
+async fn run_job_now_refuses_a_single_flight_job_that_is_running() {
+ let tmp = TempDir::new().unwrap();
+ let config = test_config(&tmp);
+ let job = single_flight_system_job(&config, "ops-run-now-busy");
+ let _running = crate::cron::scheduler::in_flight::enter(&job.id);
+ let err = run_job_now(&config, &job.id).await.unwrap_err();
+ assert!(err.contains("already running"), "{err}");
+ let err = cron_run(&config, &job.id).await.unwrap_err();
+ assert!(err.contains("already running"), "{err}");
+}
+
+#[tokio::test]
+async fn run_job_now_reports_an_unknown_job() {
+ let tmp = TempDir::new().unwrap();
+ let config = test_config(&tmp);
+ assert!(run_job_now(&config, "no-such-job").await.is_err());
+}