Skip to content

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

fix(cron): measure the gap between consecutive runs, not between now and now+1s

shaurya703e45a8d35f4c4
diff --git a/src/openhuman/cron/scheduler_part_03.rs b/src/openhuman/cron/scheduler_part_03.rs
index dc54d097a1..15df84f18e 100644
--- a/src/openhuman/cron/scheduler_part_03.rs
+++ b/src/openhuman/cron/scheduler_part_03.rs
@@ -1,4 +1,3 @@
-
async fn persist_job_result(
config: &Config,
job: &CronJob,
@@ -68,26 +67,43 @@ fn is_one_shot_auto_delete(job: &CronJob) -> bool {
job.delete_after_run && matches!(job.schedule, Schedule::At { .. })
}
-fn warn_if_high_frequency_agent_job(job: &CronJob) {
+/// Whether an agent job runs oftener than every 5 minutes.
+///
+/// Returns the verdict rather than only logging it, because the verdict is the
+/// part worth testing: the `Schedule::Cron` arm below was wrong, and no test
+/// could see it, because a function that only warns has nothing to assert.
+///
+/// The gap is measured between two *consecutive* runs. It used to be measured
+/// between the next run after now and the next run after now plus one second,
+/// which are the same instant unless a run happens to fall inside that second:
+/// `num_minutes()` of nothing is 0, 0 is under 5, so **every** cron-scheduled
+/// agent job warned, whatever its expression.
+fn is_high_frequency_agent_job(job: &CronJob) -> bool {
if !matches!(job.job_type, JobType::Agent) {
- return;
+ return false;
}
- let too_frequent = match &job.schedule {
+ match &job.schedule {
Schedule::Every { every_ms } => *every_ms < 5 * 60 * 1000,
Schedule::Cron { .. } => {
let now = Utc::now();
- match (
- next_run_for_schedule(&job.schedule, now),
- next_run_for_schedule(&job.schedule, now + chrono::Duration::seconds(1)),
- ) {
- (Ok(a), Ok(b)) => (b - a).num_minutes() < 5,
- _ => false,
+ match next_run_for_schedule(&job.schedule, now) {
+ Ok(first) => match next_run_for_schedule(&job.schedule, first) {
+ // Truncating division is what makes the boundary read the
+ // way the message does: a gap of exactly 5 minutes is 5,
+ // and not "more frequently than every 5 minutes".
+ Ok(second) => (second - first).num_minutes() < 5,
+ // An expression with no second occurrence is not frequent.
+ Err(_) => false,
+ },
+ Err(_) => false,
}
}
Schedule::At { .. } => false,
- };
+ }
+}
- if too_frequent {
+fn warn_if_high_frequency_agent_job(job: &CronJob) {
+ if is_high_frequency_agent_job(job) {
tracing::warn!(
"Cron agent job '{}' is scheduled more frequently than every 5 minutes",
job.id
diff --git a/src/openhuman/cron/scheduler_tests_part_03_tests.rs b/src/openhuman/cron/scheduler_tests_part_03_tests.rs
index 07a3af4f86..26e9bae97f 100644
--- a/src/openhuman/cron/scheduler_tests_part_03_tests.rs
+++ b/src/openhuman/cron/scheduler_tests_part_03_tests.rs
@@ -7,27 +7,90 @@ fn forbidden_path_argument_skips_flags_and_urls() {
assert!(forbidden_path_argument(&policy, "ls -la").is_none());
}
+fn agent_job_on(schedule: Schedule) -> CronJob {
+ let mut job = test_job("echo hi");
+ job.job_type = JobType::Agent;
+ job.schedule = schedule;
+ job
+}
+
+fn cron_agent_job(expr: &str) -> CronJob {
+ agent_job_on(Schedule::Cron {
+ expr: expr.into(),
+ tz: None,
+ active_hours: None,
+ })
+}
+
#[test]
fn warn_if_high_frequency_agent_job_does_not_panic_on_non_agent() {
let mut job = test_job("echo hi");
job.job_type = JobType::Shell;
warn_if_high_frequency_agent_job(&job); // should not panic
+ assert!(!is_high_frequency_agent_job(&job));
}
#[test]
fn warn_if_high_frequency_agent_job_does_not_panic_on_at_schedule() {
- let mut job = test_job("echo hi");
- job.job_type = JobType::Agent;
- job.schedule = Schedule::At { at: Utc::now() };
+ let job = agent_job_on(Schedule::At { at: Utc::now() });
warn_if_high_frequency_agent_job(&job); // should not panic
+ assert!(!is_high_frequency_agent_job(&job));
}
#[test]
fn warn_if_high_frequency_agent_job_handles_every_ms() {
- let mut job = test_job("echo hi");
- job.job_type = JobType::Agent;
- job.schedule = Schedule::Every { every_ms: 60_000 }; // 1 minute — too frequent
+ let job = agent_job_on(Schedule::Every { every_ms: 60_000 }); // 1 minute — too frequent
warn_if_high_frequency_agent_job(&job); // should warn but not panic
+ assert!(is_high_frequency_agent_job(&job));
+}
+
+/// **The warning fired for every cron-scheduled agent job.**
+///
+/// The gap was measured between the next run after now and the next run after
+/// now plus one second, which are the same instant unless a run falls inside
+/// that second. Zero minutes is under five, so an hourly job was reported as
+/// running oftener than every five minutes, and the operator had no way to tell
+/// a real misconfiguration from the noise.
+#[test]
+fn an_ordinary_cron_agent_job_is_not_called_high_frequency() {
+ for expr in ["*/10 * * * *", "0 * * * *", "0 9 * * *", "*/6 * * * *"] {
+ assert!(
+ !is_high_frequency_agent_job(&cron_agent_job(expr)),
+ "{expr} runs at most every 6 minutes and must not warn"
+ );
+ }
+}
+
+#[test]
+fn a_cron_agent_job_under_five_minutes_is_still_caught() {
+ for expr in ["*/3 * * * *", "*/1 * * * *", "*/4 * * * *"] {
+ assert!(
+ is_high_frequency_agent_job(&cron_agent_job(expr)),
+ "{expr} runs oftener than every 5 minutes and must warn"
+ );
+ }
+}
+
+/// Exactly five minutes is the boundary the message names, and it is not over
+/// it. `*/5` is also the expression the codebase's own tests reach for, so
+/// warning on it would train an operator to ignore the warning.
+#[test]
+fn a_cron_agent_job_at_exactly_five_minutes_is_not_high_frequency() {
+ assert!(!is_high_frequency_agent_job(&cron_agent_job("*/5 * * * *")));
+ assert!(!is_high_frequency_agent_job(&agent_job_on(
+ Schedule::Every {
+ every_ms: 5 * 60 * 1000
+ }
+ )));
+}
+
+/// A schedule the arm cannot read is not evidence of anything, least of all of
+/// running too often.
+#[test]
+fn an_unparseable_cron_expression_is_not_called_high_frequency() {
+ assert!(!is_high_frequency_agent_job(&cron_agent_job(
+ "not a cron expression"
+ )));
}
#[tokio::test]