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

Prevent worker getting stuck in terminating state #370

Merged
merged 2 commits into from
Dec 6, 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
16 changes: 8 additions & 8 deletions arq/worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -382,14 +382,14 @@ async def _poll_iteration(self) -> None:

await self.start_jobs(job_ids)

if self.allow_abort_jobs:
await self._cancel_aborted_jobs()

for job_id, t in list(self.tasks.items()):
if t.done():
del self.tasks[job_id]
# required to make sure errors in run_job get propagated
t.result()
if self.allow_abort_jobs:
await self._cancel_aborted_jobs()

for job_id, t in list(self.tasks.items()):
if t.done():
del self.tasks[job_id]
# required to make sure errors in run_job get propagated
t.result()

await self.heart_beat()

Expand Down
10 changes: 7 additions & 3 deletions tests/test_worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,19 +116,23 @@ async def test_worker_signal_completes_job_before_shutting_down(caplog, arq_redi
async def sleep_job(ctx, time):
await asyncio.sleep(time)

await arq_redis.enqueue_job('sleep_job', 0.2, _job_id='short_sleep') # should be cancelled
await arq_redis.enqueue_job('sleep_job', 0.2, _job_id='short_sleep') # should be completed
await arq_redis.enqueue_job('sleep_job', 5, _job_id='long_sleep') # should be cancelled
worker = worker(
functions=[func(sleep_job, name='sleep_job', max_tries=1)],
job_completion_wait=0.4,
job_completion_wait=0.5,
job_timeout=10,
)
assert worker.jobs_complete == 0
asyncio.create_task(worker.main())
await asyncio.sleep(0.1)
worker.handle_sig_wait_for_completion(signal.SIGINT)
assert len(worker.tasks) == 2 # should be two tasks when sigint is sent
assert worker.allow_pick_jobs is False
await asyncio.sleep(0.5)
await asyncio.sleep(0.3)
assert len(worker.tasks) == 1 # slept a bit, first job should now be complete and self.tasks should be updated
await asyncio.sleep(0.3)
assert len(worker.tasks) == 0 # slept longer than `job_completion_wait`, task should be cancelled and updated
logs = [rec.message for rec in caplog.records]
assert 'shutdown on SIGINT ◆ 0 jobs complete ◆ 0 failed ◆ 0 retries ◆ 2 to be completed' in logs
assert 'shutdown on SIGINT, wait complete ◆ 1 jobs complete ◆ 0 failed ◆ 0 retries ◆ 1 ongoing to cancel' in logs
Expand Down