Skip to content

The agent that grows with you

fix(cron): reset a re-armed recurring job from error to scheduled

kshitijk4poor8efccd65902d
diff --git a/cron/jobs.py b/cron/jobs.py
index 50f507a5733..f7aa9c64fb5 100644
--- a/cron/jobs.py
+++ b/cron/jobs.py
@@ -2950,8 +2950,9 @@ def _self_disable_half_paused(job: Dict[str, Any], scan: _DueScan) -> None:
def _recover_missing_next_run(job: Dict[str, Any], scan: _DueScan) -> Optional[str]:
"""Recompute and persist a missing ``next_run_at``; None when unrecoverable. One-shots use the
- grace window; recurring jobs only get here after a direct jobs.json edit bypassed add_job(),
- and would otherwise be silently skipped forever."""
+ grace window; recurring jobs get here after a direct jobs.json edit bypassed add_job() or a
+ transient croniter ImportError left them in state 'error' (#127182) — re-armed ones go back
+ to 'scheduled', otherwise they would be silently skipped forever."""
schedule = job.get("schedule", {})
kind = schedule.get("kind")
recovered_next = _recoverable_oneshot_run_at(
@@ -2967,7 +2968,11 @@ def _recover_missing_next_run(job: Dict[str, Any], scan: _DueScan) -> Optional[s
logger.info(
"Job '%s' had no next_run_at; recovering %s run at %s",
job.get("name", job.get("id", "?")), recovery_kind, recovered_next)
- scan.persist(job["id"], next_run_at=recovered_next)
+ fields: Dict[str, Any] = {"next_run_at": recovered_next}
+ if recovery_kind in {"cron", "interval"} and job.get("state") == "error":
+ fields["state"] = "scheduled"
+ job.update(fields)
+ scan.persist(job["id"], **fields)
return recovered_next
diff --git a/tests/cron/test_jobs.py b/tests/cron/test_jobs.py
index e464d07b8ce..10380cc8ef6 100644
--- a/tests/cron/test_jobs.py
+++ b/tests/cron/test_jobs.py
@@ -906,6 +906,7 @@ class TestMarkJobRun:
monkeypatch.setattr(jobs_mod, "HAS_CRONITER", None)
assert jobs_mod._ensure_croniter() is False
assert jobs_mod.compute_next_run(job["schedule"]) is None
+ mark_job_run(job["id"], success=True) # leaves state=error, next_run_at=None
# Window over (import works again): WITHOUT resetting HAS_CRONITER — the probe's
# cached outcome must be re-evaluated, not latched.
@@ -915,6 +916,8 @@ class TestMarkJobRun:
"next_run_at=None until a gateway restart"
)
assert jobs_mod.compute_next_run(job["schedule"]) is not None
+ get_due_jobs() # due-scan recovery re-arms the job
+ assert get_job(job["id"])["state"] == "scheduled"
class TestAdvanceNextRun: