exp: simplify shutdown behavior

This commit is contained in:
Peter Rowlands
2022-07-12 21:39:49 +09:00
committed by Peter Rowlands (변기호)
parent 8b663f2719
commit 8ab46c1549
2 changed files with 8 additions and 38 deletions
+6 -17
View File
@@ -244,25 +244,14 @@ class LocalCeleryQueue(BaseStashQueue):
for queue_entry in to_kill:
self.proc.kill(queue_entry.stash_rev)
def _shutdown_handler(self, task_id: str = None, **kwargs):
if task_id in self._shutdown_task_ids:
self._shutdown_task_ids.remove(task_id)
if not self._shutdown_task_ids:
self.celery.control.shutdown()
def shutdown(self, kill: bool = False):
from celery.signals import task_postrun
tasks = list(self._iter_active_tasks())
if tasks:
for task_id, _ in tasks:
self._shutdown_task_ids.add(task_id)
task_postrun.connect()(self._shutdown_handler)
if kill:
for _, task_entry in tasks:
self.celery.control.shutdown()
if kill:
for _, task_entry in self._iter_active_tasks():
try:
self.proc.kill(task_entry.stash_rev)
else:
self.celery.control.shutdown()
except ProcessLookupError:
continue
def attach(
self,
@@ -17,28 +17,9 @@ def _foo(arg=None): # pylint: disable=unused-argument
return "foo"
def test_shutdown_active_tasks(test_queue, mocker):
shutdown_spy = mocker.spy(test_queue.celery.control, "shutdown")
# use frozen signature to get an assigned task ID and AsyncResult object
# before running the task
sig = _foo.s()
result = sig.freeze()
mocker.patch.object(
test_queue, "_iter_active_tasks", return_value=[(result.id, None)]
)
# shutdown signal handler should be registered but not executed since task
# has not completed
def test_shutdown(test_queue, mocker):
shutdown_spy = mocker.patch("celery.app.control.Control.shutdown")
test_queue.shutdown()
assert result.id in test_queue._shutdown_task_ids
assert not result.ready()
shutdown_spy.assert_not_called()
sig.delay()
# block until task completes
assert "foo" == result.get()
assert result.id not in test_queue._shutdown_task_ids
shutdown_spy.assert_called_once()