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 Enamakel5e05fb5920e3
diff --git a/crates/openhuman-core/src/cron/ops.rs b/crates/openhuman-core/src/cron/ops.rs
index 2e51d81943..17e5fddef6 100644
--- a/crates/openhuman-core/src/cron/ops.rs
+++ b/crates/openhuman-core/src/cron/ops.rs
@@ -10,14 +10,6 @@ use {
tinyflows_bus::schedule::types::Schedule,
};
-/// Claim `job_id` as running. `None` when a Run Now of it already holds it. The
-/// claim is released when the guard drops, including on panic or future
-/// cancellation, so an aborted task can never lock a job id. A *scheduled* run
-/// of the same job is refused by the module's own claim when the run starts.
-pub(crate) fn try_acquire_run(job_id: &str) -> Option<crate::cron::scheduler::RunGuard> {
- crate::cron::scheduler::try_acquire_run(job_id)
-}
-
/// Loads `job_id` for a run, failing closed. The job document carries the
/// run policy (retry budget, single-flight), so a store that cannot produce it
/// refuses the run. A missing job reads as such; any other error is reduced to
@@ -72,66 +64,6 @@ pub fn resume_job(config: &Config, id: &str) -> Result<CronJob> {
)
}
-/// Update an existing cron job using the same rules as the legacy CLI, but without CLI wiring.
-///
-/// `expression` and `tz` are merged with the existing [`Schedule::Cron`] fields; the
-/// existing `active_hours` is always preserved as-is. To set or clear `active_hours`
-/// directly, use the RPC path (`cron.update` with a full [`CronJobPatch`]).
-#[cfg(test)]
-pub fn update_cron_job(
- config: &Config,
- id: &str,
- expression: Option<String>,
- tz: Option<String>,
- command: Option<String>,
- name: Option<String>,
-) -> Result<CronJob> {
- if expression.is_none() && tz.is_none() && command.is_none() && name.is_none() {
- anyhow::bail!("At least one of --expression, --tz, --command, or --name must be provided");
- }
-
- // Merge expression/tz with the existing schedule so that
- // tz alone updates the timezone and expression alone preserves the timezone.
- let schedule = if expression.is_some() || tz.is_some() {
- let existing = cron::get_job(config, id)?;
- let (existing_expr, existing_tz, existing_active) = match existing.schedule {
- Schedule::Cron {
- expr,
- tz: existing_tz,
- active_hours: existing_active,
- } => (expr, existing_tz, existing_active),
- _ => anyhow::bail!("Cannot update expression/tz on a non-cron schedule"),
- };
- Some(Schedule::Cron {
- expr: expression.unwrap_or(existing_expr),
- tz: tz.or(existing_tz),
- active_hours: existing_active,
- })
- } else {
- None
- };
-
- if let Some(ref cmd) = command {
- let security = SecurityPolicy::from_config(
- &config.autonomy,
- &config.workspace_dir,
- &config.action_dir,
- );
- if !security.is_command_allowed(cmd) {
- anyhow::bail!("Command blocked by security policy: {cmd}");
- }
- }
-
- let patch = CronJobPatch {
- schedule,
- command,
- name,
- ..CronJobPatch::default()
- };
-
- update_job(config, id, patch)
-}
-
pub async fn cron_list(config: &Config) -> Result<Outcome<Vec<CronJob>>, String> {
if !config.cron.enabled {
return Err("cron is disabled by config (cron.enabled=false)".to_string());
@@ -213,7 +145,7 @@ pub async fn cron_run(
// Guard against duplicate concurrent executions for the same job. The
// claim is taken before the job is loaded and moves into the task.
- let Some(run_guard) = try_acquire_run(job_id) else {
+ let Some(run_guard) = crate::cron::scheduler::try_acquire_run(job_id) else {
tracing::debug!(job_id, "[cron_run] rejected duplicate concurrent execution");
return Err(format!("cron job '{job_id}' is already running"));
};
@@ -260,7 +192,7 @@ pub async fn run_job_now(
) -> Result<(bool, String), String> {
let job = load_job_for_run(config, job_id.trim()).map_err(|e| e.to_string())?;
cron::limits::check_run(config, &job).map_err(|e| e.to_string())?;
- let Some(_guard) = try_acquire_run(&job.id) else {
+ let Some(_guard) = crate::cron::scheduler::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/ops_tests.rs b/crates/openhuman-core/src/cron/ops_tests.rs
index 9e89cd0c45..0ce7e5f529 100644
--- a/crates/openhuman-core/src/cron/ops_tests.rs
+++ b/crates/openhuman-core/src/cron/ops_tests.rs
@@ -1,7 +1,7 @@
use super::*;
use crate::cron::get_job;
use tempfile::TempDir;
-use tinyflows_bus::schedule::types::{ActiveHours, DeliveryConfig};
+use tinyflows_bus::schedule::types::DeliveryConfig;
pub(super) fn test_config(tmp: &TempDir) -> Config {
let config = Config {
@@ -28,174 +28,6 @@ fn make_job(config: &Config, expr: &str, tz: Option<&str>, cmd: &str) -> CronJob
.unwrap()
}
-fn run_update(
- config: &Config,
- id: &str,
- expression: Option<&str>,
- tz: Option<&str>,
- command: Option<&str>,
- name: Option<&str>,
-) -> Result<()> {
- update_cron_job(
- config,
- id,
- expression.map(Into::into),
- tz.map(Into::into),
- command.map(Into::into),
- name.map(Into::into),
- )
- .map(|_| ())
-}
-
-#[test]
-fn update_changes_command_via_handler() {
- let tmp = TempDir::new().unwrap();
- let config = test_config(&tmp);
- let job = make_job(&config, "*/5 * * * *", None, "echo original");
-
- run_update(&config, &job.id, None, None, Some("echo updated"), None).unwrap();
-
- let updated = get_job(&config, &job.id).unwrap();
- assert_eq!(updated.command, "echo updated");
- assert_eq!(updated.id, job.id);
-}
-
-#[test]
-fn update_changes_expression_via_handler() {
- let tmp = TempDir::new().unwrap();
- let config = test_config(&tmp);
- let job = make_job(&config, "*/5 * * * *", None, "echo test");
-
- run_update(&config, &job.id, Some("0 9 * * *"), None, None, None).unwrap();
-
- let updated = get_job(&config, &job.id).unwrap();
- assert_eq!(updated.expression, "0 9 * * *");
-}
-
-#[test]
-fn update_changes_name_via_handler() {
- let tmp = TempDir::new().unwrap();
- let config = test_config(&tmp);
- let job = make_job(&config, "*/5 * * * *", None, "echo test");
-
- run_update(&config, &job.id, None, None, None, Some("new-name")).unwrap();
-
- let updated = get_job(&config, &job.id).unwrap();
- assert_eq!(updated.name.as_deref(), Some("new-name"));
-}
-
-#[test]
-fn update_tz_alone_sets_timezone() {
- let tmp = TempDir::new().unwrap();
- let config = test_config(&tmp);
- let job = make_job(&config, "*/5 * * * *", None, "echo test");
-
- run_update(
- &config,
- &job.id,
- None,
- Some("America/Los_Angeles"),
- None,
- None,
- )
- .unwrap();
-
- let updated = get_job(&config, &job.id).unwrap();
- assert_eq!(
- updated.schedule,
- Schedule::Cron {
- expr: "*/5 * * * *".into(),
- tz: Some("America/Los_Angeles".into()),
- active_hours: None,
- }
- );
-}
-
-#[test]
-fn update_expr_alone_preserves_timezone() {
- let tmp = TempDir::new().unwrap();
- let config = test_config(&tmp);
- let job = make_job(&config, "*/5 * * * *", Some("UTC"), "echo test");
-
- run_update(&config, &job.id, Some("0 10 * * *"), None, None, None).unwrap();
-
- let updated = get_job(&config, &job.id).unwrap();
- assert_eq!(
- updated.schedule,
- Schedule::Cron {
- expr: "0 10 * * *".into(),
- tz: Some("UTC".into()),
- active_hours: None,
- }
- );
-}
-
-#[test]
-fn update_expr_and_tz_preserve_active_hours() {
- let tmp = TempDir::new().unwrap();
- let config = test_config(&tmp);
- let active_hours = ActiveHours {
- start: "09:00".into(),
- end: "17:00".into(),
- };
- let job = add_shell_job(
- &config,
- None,
- Schedule::Cron {
- expr: "*/5 * * * *".into(),
- tz: Some("UTC".into()),
- active_hours: Some(active_hours.clone()),
- },
- "echo test",
- )
- .unwrap();
-
- run_update(
- &config,
- &job.id,
- Some("0 10 * * *"),
- Some("America/Los_Angeles"),
- None,
- None,
- )
- .unwrap();
-
- let updated = get_job(&config, &job.id).unwrap();
- assert_eq!(
- updated.schedule,
- Schedule::Cron {
- expr: "0 10 * * *".into(),
- tz: Some("America/Los_Angeles".into()),
- active_hours: Some(active_hours),
- }
- );
-}
-
-#[test]
-fn update_fails_when_no_fields_provided() {
- let tmp = TempDir::new().unwrap();
- let config = test_config(&tmp);
- let job = make_job(&config, "*/5 * * * *", None, "echo test");
-
- let err = run_update(&config, &job.id, None, None, None, None).unwrap_err();
- assert!(err
- .to_string()
- .contains("At least one of --expression, --tz, --command, or --name"));
-}
-
-#[test]
-fn update_rejects_expression_for_non_cron_schedule() {
- let tmp = TempDir::new().unwrap();
- let config = test_config(&tmp);
- let at = chrono::Utc::now() + chrono::Duration::minutes(5);
- let job = add_shell_job(&config, None, Schedule::At { at }, "echo test").unwrap();
-
- let err = run_update(&config, &job.id, Some("0 9 * * *"), None, None, None).unwrap_err();
- assert!(err
- .to_string()
- .contains("Cannot update expression/tz on a non-cron schedule"));
-}
-
// ── add_once ────────────────────────────────────────────────────
#[test]
diff --git a/crates/openhuman-core/src/cron/tools/run.rs b/crates/openhuman-core/src/cron/tools/run.rs
index a1dfba31f3..f232050cfe 100644
--- a/crates/openhuman-core/src/cron/tools/run.rs
+++ b/crates/openhuman-core/src/cron/tools/run.rs
@@ -95,7 +95,7 @@ impl Tool for CronRunTool {
}
// 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 {
+ let Some(_run_guard) = cron::scheduler::try_acquire_run(&job.id) else {
return Ok(ToolResult::error(format!(
"cron job '{}' is already running",
job.id