Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MUS-937] Porting fixes from upstream - fix jobs timeout #4

Merged
merged 1 commit into from
May 5, 2022
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
4 changes: 1 addition & 3 deletions arq/worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -525,10 +525,9 @@ async def job_failed(exc: BaseException) -> None:
logger.info('%6.2fs → %s(%s)%s', (start_ms - enqueue_time_ms) / 1000, ref, s, extra)
self.job_tasks[job_id] = task = self.loop.create_task(function.coroutine(ctx, *args, **kwargs))

cancel_handler = self.loop.call_at(self.loop.time() + timeout_s, task.cancel)
# run repr(result) and extra inside try/except as they can raise exceptions
try:
result = await task
result = await asyncio.wait_for(task, timeout_s)
except (Exception, asyncio.CancelledError) as e:
exc_extra = getattr(e, 'extra', None)
if callable(exc_extra):
Expand All @@ -538,7 +537,6 @@ async def job_failed(exc: BaseException) -> None:
result_str = '' if result is None else truncate(repr(result))
finally:
del self.job_tasks[job_id]
cancel_handler.cancel()

except (Exception, asyncio.CancelledError) as e:
finished_ms = timestamp_ms()
Expand Down
18 changes: 18 additions & 0 deletions tests/test_worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -837,3 +837,21 @@ async def wait_and_abort(job, delay=0.1):
assert worker.aborting_tasks == set()
assert worker.tasks == {}
assert worker.job_tasks == {}


async def test_job_timeout(arq_redis: ArqRedis, worker, caplog):
async def longfunc(ctx):
await asyncio.sleep(0.3)

caplog.set_level(logging.ERROR)
await arq_redis.enqueue_job('longfunc', _job_id='testing')
worker: Worker = worker(functions=[func(longfunc, name='longfunc')], job_timeout=0.2, poll_delay=0.1)
assert worker.jobs_complete == 0
assert worker.jobs_failed == 0
assert worker.jobs_retried == 0
await worker.main()
assert worker.jobs_complete == 0
assert worker.jobs_failed == 1
assert worker.jobs_retried == 0
log = re.sub(r'\d+.\d\ds', 'X.XXs', '\n'.join(r.message for r in caplog.records))
assert 'X.XXs ! testing:longfunc failed, TimeoutError:' in log