Skip to content

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

feat(cron): add retry handling for failed scheduled jobs

Steven Enamakel049713a792d9
diff --git a/crates/openhuman-core/src/cron/ops.rs b/crates/openhuman-core/src/cron/ops.rs
index 2d1cdb94b3..e084a45af4 100644
--- a/crates/openhuman-core/src/cron/ops.rs
+++ b/crates/openhuman-core/src/cron/ops.rs
@@ -226,6 +226,9 @@ pub async fn cron_run(config: &Config, job_id: &str) -> Result<Outcome<serde_jso
return Err("cron is disabled by config (cron.enabled=false)".to_string());
}
+ // Fail closed before queueing anything: an unreadable policy refuses the run.
+ cron::policy::get_policy(config, job_id).map_err(|e| e.to_string())?;
+
// Guard against duplicate concurrent executions for the same job.
{
let mut active = ACTIVE_RUNS
@@ -286,6 +289,7 @@ pub async fn cron_run(config: &Config, job_id: &str) -> Result<Outcome<serde_jso
/// (scheduled or another Run Now) is already active.
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())?;
+ cron::policy::get_policy(config, &job.id).map_err(|e| e.to_string())?;
let Some(_guard) = try_acquire_run(&job.id) else {
tracing::debug!(job_id = %job.id, "[cron_run] job already running; refused");
return Err(format!("cron job '{}' is already running", job.id));
diff --git a/crates/openhuman-core/src/cron/scheduler/dispatch.rs b/crates/openhuman-core/src/cron/scheduler/dispatch.rs
index ba46851255..ed2e141dc8 100644
--- a/crates/openhuman-core/src/cron/scheduler/dispatch.rs
+++ b/crates/openhuman-core/src/cron/scheduler/dispatch.rs
@@ -32,7 +32,7 @@ use crate::config::Config;
use crate::core::bus::BUS;
use crate::core::events::DomainEvent;
use crate::core::runtime::CoreContext;
-use crate::cron::policy::policy_or_default;
+use crate::cron::policy::get_policy;
use crate::cron::{next_run_for_schedule, record_run, update_job, CronJob, CronJobPatch, Schedule};
use crate::security::SecurityPolicy;
@@ -68,6 +68,11 @@ impl JobDispatcher {
skip_in_flight(config, &job);
continue;
};
+ // Fail closed: an unreadable policy refuses the job before its slot
+ // is advanced, so the occurrence is retried once the read recovers.
+ if get_policy(config, &job.id).is_err() {
+ continue;
+ }
claim_slot(config, &job);
let config = config.clone();
let security = Arc::clone(security);
@@ -143,8 +148,8 @@ fn claim_slot(config: &Config, job: &CronJob) {
/// A due job whose previous run is still going.
fn skip_in_flight(config: &Config, job: &CronJob) {
- let policy = policy_or_default(config, &job.id);
- if !policy.single_flight || matches!(job.schedule, Schedule::At { .. }) {
+ let single_flight = get_policy(config, &job.id).is_ok_and(|policy| policy.single_flight);
+ if !single_flight || matches!(job.schedule, Schedule::At { .. }) {
tracing::trace!(job_id = %job.id, "[cron:dispatch] still running; not dispatched again");
return;
}
diff --git a/crates/openhuman-core/src/cron/scheduler/retry.rs b/crates/openhuman-core/src/cron/scheduler/retry.rs
index d26d53a607..0f5b5f68a5 100644
--- a/crates/openhuman-core/src/cron/scheduler/retry.rs
+++ b/crates/openhuman-core/src/cron/scheduler/retry.rs
@@ -48,11 +48,13 @@ pub(super) async fn execute_job_with_retry_for_run(
) -> (bool, String) {
let mut last_output = String::new();
let mut last_agent_error: Option<String> = None;
- // A per-job override (`cron::policy`) wins; `Some(0)` is one attempt.
- let retries = crate::cron::policy::effective_retries(
- config,
- &crate::cron::policy::policy_or_default(config, &job.id),
- );
+ // A per-job override (`cron::policy`) wins; `Some(0)` is one attempt. An
+ // unreadable policy refuses the run (and any later attempt) outright.
+ let policy = match crate::cron::policy::get_policy(config, &job.id) {
+ Ok(policy) => policy,
+ Err(error) => return (false, error.to_string()),
+ };
+ let retries = crate::cron::policy::effective_retries(config, &policy);
let mut backoff_ms = config.reliability.provider_backoff_ms.max(200);
let mut session_expired = false;
let mut credits_exhausted = false;
@@ -61,6 +63,11 @@ pub(super) async fn execute_job_with_retry_for_run(
let mut local_unreachable = false;
for attempt in 0..=retries {
+ if attempt > 0 {
+ if let Err(error) = crate::cron::policy::get_policy(config, &job.id) {
+ return (false, error.to_string());
+ }
+ }
let (success, output, agent_error) = match job.job_type {
JobType::Shell => {
let (success, output) = run_job_command(config, security, job).await;
diff --git a/crates/openhuman-core/src/cron/tools/run.rs b/crates/openhuman-core/src/cron/tools/run.rs
index 3a1ce85418..1b2da23abc 100644
--- a/crates/openhuman-core/src/cron/tools/run.rs
+++ b/crates/openhuman-core/src/cron/tools/run.rs
@@ -88,6 +88,10 @@ impl Tool for CronRunTool {
}
};
+ if let Err(e) = cron::policy::get_policy(&self.config, &job.id) {
+ return Ok(ToolResult::error(e.to_string()));
+ }
+
// Claim the job so this run cannot overlap a scheduled tick or a Run Now.
let Some(_run_guard) = cron::ops::try_acquire_run(&job.id) else {
return Ok(ToolResult::error(format!(