Skip to content

The agent that grows with you

fix(cron): rate-limit store-unwritable warnings per call site so skipped jobs are always reported

kshitijk4poor1c7348331210
diff --git a/cron/jobs.py b/cron/jobs.py
index 131936e9241..abae105bcdb 100644
--- a/cron/jobs.py
+++ b/cron/jobs.py
@@ -1247,16 +1247,17 @@ def _write_marker(name: str, text: str, tmp_prefix: str) -> None:
pass
-# One WARNING per (store, errno) per window: an unwritable store fails every 60s tick until fixed.
+# One WARNING per (store, errno, site) per window: an unwritable store fails every 60s tick until fixed;
+# the site keeps the scan's warning from silencing the tick's "skipped jobs" one.
_STORE_WARN_INTERVAL_SECONDS = 900.0
_last_store_warning: dict = {}
-def warn_store_unwritable(exc: OSError, consequence: str) -> None:
+def warn_store_unwritable(exc: OSError, consequence: str, site: str) -> None:
"""Rate-limited WARNING for a failed cron store write (ENOSPC/EROFS/EACCES). Callers skip the
dispatch that needed the write: no job runs without a durable advance/fire claim."""
cron_dir = str(_current_cron_store().cron_dir) # multi-profile gateway: one key per store
- now, key = time.monotonic(), (cron_dir, exc.errno)
+ now, key = time.monotonic(), (cron_dir, exc.errno, site)
last = _last_store_warning.get(key)
if last is not None and now - last < _STORE_WARN_INTERVAL_SECONDS:
return
@@ -3358,7 +3359,7 @@ def _get_due_jobs_locked() -> List[Dict[str, Any]]:
try:
save_jobs(raw_jobs, removed_ids=scan.removed or None)
except OSError as exc: # repairs live in memory; the next tick retries the persist
- warn_store_unwritable(exc, "due-scan repairs not persisted")
+ warn_store_unwritable(exc, "due-scan repairs not persisted", "scan")
return due
diff --git a/cron/scheduler.py b/cron/scheduler.py
index e982386d8b8..99669e60eaa 100644
--- a/cron/scheduler.py
+++ b/cron/scheduler.py
@@ -4265,7 +4265,7 @@ def _process_due_job(job: dict, adapters, loop, verbose: bool) -> bool:
try:
claimed = claim_job_for_fire(job["id"], return_job=True)
except OSError as exc:
- warn_store_unwritable(exc, f"skipped job '{job.get('name') or job['id']}'")
+ warn_store_unwritable(exc, f"skipped job '{job.get('name') or job['id']}'", "claim")
finish_execution(
job["execution_id"], success=False, error=f"Cron store unwritable; not started: {exc}")
return False
diff --git a/cron/scheduler_tick.py b/cron/scheduler_tick.py
index 36bf7850877..c9f6cd69427 100644
--- a/cron/scheduler_tick.py
+++ b/cron/scheduler_tick.py
@@ -112,7 +112,7 @@ def _tick_admitted(
# at-most-once side. One-shots still go through their own fire claim.
n = len(due_jobs)
due_jobs = [j for j in due_jobs if j.get("schedule", {}).get("kind") not in {"cron", "interval"}]
- warn_store_unwritable(exc, f"skipped {n - len(due_jobs)} recurring job(s)")
+ warn_store_unwritable(exc, f"skipped {n - len(due_jobs)} recurring job(s)", "advance")
if not due_jobs:
_sched._sweep_mcp_orphans()
return 0
diff --git a/tests/cron/test_due_scan_save_failure.py b/tests/cron/test_due_scan_save_failure.py
index b287ee4c75c..85babe9af7c 100644
--- a/tests/cron/test_due_scan_save_failure.py
+++ b/tests/cron/test_due_scan_save_failure.py
@@ -80,7 +80,7 @@ def test_due_jobs_are_returned_when_the_store_cannot_be_saved(cron_store, full_d
assert [d["id"] for d in get_due_jobs()] == ["due-job"]
with cronjobs.use_cron_store(cron_store / "other-profile"): # a second profile's store
- cronjobs.warn_store_unwritable(OSError(errno.ENOSPC, "No space left on device"), "x")
+ cronjobs.warn_store_unwritable(OSError(errno.ENOSPC, "No space left on device"), "x", "scan")
# Rate-limited per store: one WARNING per outage per profile, not one per 60s scan, and a
# sibling profile on the same errno is not silenced; each names its store.
@@ -108,11 +108,13 @@ def test_tick_on_unwritable_store_returns_cleanly_without_dispatch(cron_store, m
monkeypatch.setattr(scheduler, "_sweep_mcp_orphans", lambda: sweeps.append(1))
monkeypatch.setattr(cronjobs, "_last_store_warning", {})
monkeypatch.setattr(cronjobs, "_stage_jobs_payload", _enospc)
- with caplog.at_level(logging.WARNING, logger="cron.scheduler"):
+ with caplog.at_level(logging.WARNING):
assert scheduler.tick(verbose=False, sync=True) == 0
assert ran == [] and sweeps == [1]
assert f"Cron store {cron_store / 'cron'} is unwritable" in caplog.text
+ assert "skipped 1 recurring job(s)" in caplog.text # not silenced by the scan's earlier warning
+ assert executions.latest_execution("due-job") is None # skipped before its fire claim
assert load_jobs() == before
row = executions.latest_execution("once")
if with_once: