Skip to content

The agent that grows with you

fix(cron): a run-now queued during a manual run survives its completion

Tekniumaf0d245b3a74
diff --git a/cron/extra_fire.py b/cron/extra_fire.py
index f573d7ee94d..97ae3c1ddf0 100644
--- a/cron/extra_fire.py
+++ b/cron/extra_fire.py
@@ -58,13 +58,26 @@ def is_extra(job: dict[str, Any]) -> bool:
return isinstance(claim, dict) and bool(claim.get(_CLAIM_KEY))
+_MARKERS = ("manual_run_at", RESTORE_KEY, "manual_run_prompt")
+
+
+def held_markers(job: dict[str, Any]) -> dict[str, Any]:
+ """The run-now markers of a ``trigger_job`` that landed while this extra fire was in flight: the
+ claim popped RESTORE_KEY in its own save, so a ``queued`` job at completion was triggered
+ after the claim (``fire_claim.at`` cannot tell: the heartbeat refreshes it). That trigger is
+ its own run-now, so the caller puts these back after the outcome clears the markers."""
+ return {key: job[key] for key in _MARKERS if key in job} if queued(job) else {}
+
+
def settle(job: dict[str, Any], now: str) -> None:
- """After an extra fire: ``repeat.completed`` untouched, the job keeps its pending slot. A slot
- that passed while the run was in flight (the tick lost the claim race) is not dropped: a
- one-shot fires at once, a recurring job takes its next natural occurrence."""
+ """After an extra fire: ``repeat.completed`` untouched, the job keeps its pending slot, and a
+ run-now queued during the run (``queued``) is left for its own claim. A slot that passed while
+ the run was in flight: a one-shot fires at once; a recurring job DROPS it and takes its next
+ natural occurrence, as on a scheduled run (the tick's pre-run advance has usually moved it
+ already, and keeping it due would make every tick during the run log a lost-claim fire)."""
from cron.jobs import compute_next_run
- if _future_slot(job.get("next_run_at"), datetime.fromisoformat(now)):
+ if queued(job) or _future_slot(job.get("next_run_at"), datetime.fromisoformat(now)):
return
if job.get("schedule", {}).get("kind") == "once":
job["next_run_at"] = now
diff --git a/cron/jobs.py b/cron/jobs.py
index 1b0ed356d22..d650cee1a98 100644
--- a/cron/jobs.py
+++ b/cron/jobs.py
@@ -2453,7 +2453,9 @@ def mark_job_run(
return False
now = _hermes_now().isoformat()
extra = extra_fire.is_extra(job) # read before the outcome clears the claim
+ held = extra_fire.held_markers(job) if extra else {}
_record_run_outcome(job, success, error, delivery_error, status, now)
+ job.update(held) # a run-now queued mid-run is its own fire, not this one
if execution_id is not None: # retire ids recovery can no longer replay; latest stays last
from cron.scheduler_authority import replayable_completions as live
job["canonical_completions"] = [*live(job_id, job.get("canonical_completions")), execution_id]
diff --git a/tests/cron/test_manual_run_extra_fire.py b/tests/cron/test_manual_run_extra_fire.py
index baf97dc0d64..4e69c91cb3c 100644
--- a/tests/cron/test_manual_run_extra_fire.py
+++ b/tests/cron/test_manual_run_extra_fire.py
@@ -15,6 +15,7 @@ from cron.jobs import (
create_job,
get_due_jobs,
get_job,
+ heartbeat_fire_claim,
load_jobs,
mark_job_run,
save_jobs,
@@ -150,3 +151,25 @@ def test_run_at_or_after_the_due_slot_is_the_scheduled_fire(clock):
done = get_job(job["id"])
assert (done["state"], done["repeat"]["completed"]) == ("completed", 1)
+
+
+def test_trigger_landing_during_a_manual_run_survives_its_completion(clock):
+ """A run-now queued while a manual run is in flight is its own extra fire: the in-flight run's
+ completion must not clear it, and firing it must not consume the one-shot."""
+ job = create_job("remind me", "in 30m", name="reminder")
+ slot = job["next_run_at"]
+ claimed = claim_job_for_fire(job["id"], manual=True, return_job=True)
+ assert isinstance(claimed, dict) and claim_dispatch(job["id"])
+
+ clock["now"] += timedelta(seconds=10)
+ trigger_job(job["id"]) # gateway-forwarded run-now while the first run is executing
+ clock["now"] += timedelta(seconds=10)
+ assert heartbeat_fire_claim(job["id"], expected_owner=claimed["fire_claim"]["by"]) # still running
+ assert mark_job_run(job["id"], success=True, expected_fire_owner=claimed["fire_claim"]["by"])
+
+ assert [d["id"] for d in get_due_jobs()] == [job["id"]] # the queued run-now is still due
+ _fire(job["id"]) # the ticker picks it up
+ after = get_job(job["id"])
+ assert (after["state"], after["next_run_at"]) == ("scheduled", slot)
+ assert after["repeat"] == {"times": 1, "completed": 0}
+