Skip to content

Commit e015d3d

Browse files
authored
fix error when cron jobs were terminanted (#273)
fixes #253
1 parent 7cc5cf2 commit e015d3d

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

arq/worker.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -620,7 +620,8 @@ async def finish_job(
620620
tr.zrem(self.queue_name, job_id)
621621
elif incr_score:
622622
tr.zincrby(self.queue_name, incr_score, job_id)
623-
tr.delete(*delete_keys)
623+
if delete_keys:
624+
tr.delete(*delete_keys)
624625
await tr.execute()
625626

626627
async def finish_failed_job(self, job_id: str, result_data: Optional[bytes]) -> None:

tests/test_cron.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1+
import asyncio
12
import logging
23
import re
34
from datetime import datetime, timedelta
45
from random import random
56

67
import pytest
78

9+
import arq
810
from arq import Worker
911
from arq.constants import in_progress_key_prefix
1012
from arq.cron import cron, next_cron
@@ -142,3 +144,19 @@ async def test_repr():
142144
async def test_str_function():
143145
cj = cron('asyncio.sleep', hour=1, run_at_startup=True)
144146
assert str(cj).startswith('<CronJob name=cron:asyncio.sleep coroutine=<function sleep at')
147+
148+
149+
async def test_cron_cancelled(worker, mocker):
150+
mocker.patch.object(arq.worker, 'keep_cronjob_progress', 0.1)
151+
152+
async def try_sleep(ctx):
153+
if ctx['job_try'] == 1:
154+
raise asyncio.CancelledError
155+
156+
worker: Worker = worker(
157+
cron_jobs=[cron(try_sleep, microsecond=20, run_at_startup=True, max_tries=2)], poll_delay=0.01,
158+
)
159+
await worker.main()
160+
assert worker.jobs_complete == 1
161+
assert worker.jobs_retried == 1
162+
assert worker.jobs_failed == 0

0 commit comments

Comments
 (0)