fix: skip stale cron jobs on gateway restart instead of firing immediately
When the gateway restarts after being down past a scheduled run time, recurring jobs (cron/interval) were firing immediately because their next_run_at was in the past. Now jobs more than 2 minutes late are fast-forwarded to the next future occurrence instead. - get_due_jobs() checks staleness for cron/interval jobs - Stale jobs get next_run_at recomputed and saved - Jobs within 2 minutes of their schedule still fire normally - One-shot (once) jobs are unaffected — they fire if missed Fixes the 'cron jobs run on every gateway restart' issue.
This commit is contained in:
parent
e3f9894caf
commit
4768ea624d
3 changed files with 64 additions and 7 deletions
|
|
@ -304,17 +304,34 @@ class TestMarkJobRun:
|
|||
|
||||
|
||||
class TestGetDueJobs:
|
||||
def test_past_due_returned(self, tmp_cron_dir):
|
||||
def test_past_due_within_window_returned(self, tmp_cron_dir):
|
||||
"""Jobs less than 2 minutes late are still considered due (not stale)."""
|
||||
job = create_job(prompt="Due now", schedule="every 1h")
|
||||
# Force next_run_at to the past
|
||||
# Force next_run_at to just 1 minute ago (within the 2-min window)
|
||||
jobs = load_jobs()
|
||||
jobs[0]["next_run_at"] = (datetime.now() - timedelta(minutes=5)).isoformat()
|
||||
jobs[0]["next_run_at"] = (datetime.now() - timedelta(seconds=60)).isoformat()
|
||||
save_jobs(jobs)
|
||||
|
||||
due = get_due_jobs()
|
||||
assert len(due) == 1
|
||||
assert due[0]["id"] == job["id"]
|
||||
|
||||
def test_stale_past_due_skipped(self, tmp_cron_dir):
|
||||
"""Recurring jobs more than 2 minutes late are fast-forwarded, not fired."""
|
||||
job = create_job(prompt="Stale", schedule="every 1h")
|
||||
# Force next_run_at to 5 minutes ago (beyond the 2-min window)
|
||||
jobs = load_jobs()
|
||||
jobs[0]["next_run_at"] = (datetime.now() - timedelta(minutes=5)).isoformat()
|
||||
save_jobs(jobs)
|
||||
|
||||
due = get_due_jobs()
|
||||
assert len(due) == 0
|
||||
# next_run_at should be fast-forwarded to the future
|
||||
updated = get_job(job["id"])
|
||||
from cron.jobs import _ensure_aware, _hermes_now
|
||||
next_dt = _ensure_aware(datetime.fromisoformat(updated["next_run_at"]))
|
||||
assert next_dt > _hermes_now()
|
||||
|
||||
def test_future_not_returned(self, tmp_cron_dir):
|
||||
create_job(prompt="Not yet", schedule="every 1h")
|
||||
due = get_due_jobs()
|
||||
|
|
|
|||
|
|
@ -241,7 +241,7 @@ class TestCronTimezone:
|
|||
job = create_job(prompt="Test job", schedule="every 1h")
|
||||
jobs = load_jobs()
|
||||
# Force a naive (no timezone) past timestamp
|
||||
naive_past = (datetime.now() - timedelta(minutes=5)).isoformat()
|
||||
naive_past = (datetime.now() - timedelta(seconds=30)).isoformat()
|
||||
jobs[0]["next_run_at"] = naive_past
|
||||
save_jobs(jobs)
|
||||
|
||||
|
|
@ -318,7 +318,7 @@ class TestCronTimezone:
|
|||
|
||||
# Simulate a naive timestamp that was written by datetime.now() on a
|
||||
# system running in UTC+5:30 — 5 minutes in the past (local time)
|
||||
naive_past = (datetime.now() - timedelta(minutes=5)).isoformat()
|
||||
naive_past = (datetime.now() - timedelta(seconds=30)).isoformat()
|
||||
jobs[0]["next_run_at"] = naive_past
|
||||
save_jobs(jobs)
|
||||
|
||||
|
|
@ -347,7 +347,7 @@ class TestCronTimezone:
|
|||
jobs = load_jobs()
|
||||
|
||||
# Force a naive past timestamp (system-local wall time, 10 min ago)
|
||||
naive_past = (datetime.now() - timedelta(minutes=10)).isoformat()
|
||||
naive_past = (datetime.now() - timedelta(seconds=30)).isoformat()
|
||||
jobs[0]["next_run_at"] = naive_past
|
||||
save_jobs(jobs)
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue