Skip to content

Commit 52d40b1

Browse files
gakerclaudechrisguidry
authored
Reset a Perpetual's cancel and next-run time on each run (#626)
Since #619, `Perpetual.__aenter__` returns `copy.copy()` of the declared instance, and the copy keeps `cancelled` and `_next_when`. A task function called directly, outside a worker, receives the declared default itself. A `cancel()`, `after()`, or `at()` from that call then carried into every later worker run of the task in the same process. After a direct call that cancelled, each worker run stopped the perpetual instead of rescheduling it. `__aenter__` now resets `cancelled` to `False` and `_next_when` to `None` on the copy. Cron's `__aenter__` already does the same for its `_iterator`. This matches 0.29, where each run built a new `Perpetual(every=..., automatic=...)`. A changed `every` still carries over, as it did in 0.29, because 0.29 also read `self.every`. The other dependencies that #619 moved to `copy.copy()` don't have this problem: - `Retry` and `ExponentialRetry` set `attempt` and `delay` from the execution on each copy. `Retry.after()` writes `delay`, but 0.29 also copied `self.delay`. - `Timeout.run()` calls `start()` on the copy, which sets `_deadline`. - `ConcurrencyLimit` sets every per-run field on its copy. The new test calls a perpetual task directly with `after(1h)` and `cancel()`, then runs it on a worker and checks that it runs three times. Without the fix, it runs once. 🤖 Generated with [Claude Code](https://claude.com/claude-code) --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com> Co-authored-by: Chris Guidry <chris@theguidrys.us>
1 parent 5e911e1 commit 52d40b1

2 files changed

Lines changed: 42 additions & 1 deletion

File tree

‎python/src/docket/dependencies/_perpetual.py‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,13 @@ async def __aenter__(self) -> Self:
9090
# Each attempt gets a copy of the declared instance, so what the task
9191
# asks of it (cancel, perpetuate, after, at) applies to that attempt
9292
# alone. Copying keeps a subclass and its state without calling its
93-
# constructor, whose arguments may differ from this class's.
93+
# constructor, whose arguments may differ from this class's. A task
94+
# called directly, outside a worker, receives the declared instance
95+
# itself, so the copy resets what cancel(), after(), and at() set.
9496
execution = current_execution.get()
9597
perpetual = copy.copy(self)
98+
perpetual.cancelled = False
99+
perpetual._next_when = None
96100
perpetual.args = execution.args
97101
perpetual.kwargs = execution.kwargs
98102
return perpetual
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
"""Each run of a task works with its own copy of the perpetual the task
2+
declared."""
3+
4+
import asyncio
5+
from datetime import timedelta
6+
7+
from docket import Docket, Perpetual, Worker
8+
9+
10+
async def test_calling_a_perpetual_task_directly_does_not_affect_worker_runs(
11+
docket: Docket, worker: Worker
12+
):
13+
"""A direct call passes the declared default itself, so a cancel() or
14+
after() it makes must not carry into later runs on a worker."""
15+
worker_calls = 0
16+
17+
async def perpetual_task(
18+
direct: bool = False,
19+
perpetual: Perpetual = Perpetual(every=timedelta(milliseconds=10)),
20+
):
21+
nonlocal worker_calls
22+
if direct:
23+
perpetual.after(timedelta(hours=1))
24+
perpetual.cancel()
25+
return
26+
worker_calls += 1
27+
if worker_calls == 3:
28+
perpetual.cancel()
29+
30+
await perpetual_task(direct=True)
31+
32+
await docket.add(perpetual_task)()
33+
34+
async with asyncio.timeout(5):
35+
await worker.run_until_finished()
36+
37+
assert worker_calls == 3

0 commit comments

Comments
 (0)