Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/docket/docket.py
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,8 @@ def replace(
"""
if isinstance(function, str):
function = self.tasks[function]
else:
self.register(function)

async def scheduler(*args: P.args, **kwargs: P.kwargs) -> Execution:
execution = Execution(self, function, args, kwargs, key, when, attempt=1)
Expand Down
18 changes: 18 additions & 0 deletions tests/test_fundamentals.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,24 @@ async def test_rescheduling_by_name(
assert later <= now()


async def test_replace_without_existing_task_acts_like_add(
docket: Docket, worker: Worker, the_task: AsyncMock, now: Callable[[], datetime]
):
"""docket.replace() on a non-existent key should schedule the task like add()"""

key = f"my-cool-task:{uuid4()}"

# Replace without prior add - should just schedule the task
later = now() + timedelta(milliseconds=100)
await docket.replace(the_task, later, key=key)("b", "c", c="d")

await worker.run_until_finished()

the_task.assert_awaited_once_with("b", "c", c="d")

assert later <= now()


async def test_task_keys_are_idempotent_in_the_future(
docket: Docket, worker: Worker, the_task: AsyncMock, now: Callable[[], datetime]
):
Expand Down