| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
from enum import Enum, auto |
| 4 |
|
| 5 |
from pytest_celery import CeleryTestWorker |
| 6 |
|
| 7 |
from celery.app.control import Control |
| 8 |
|
| 9 |
|
| 10 |
class WorkerKill: |
| 11 |
"""Kills a worker in different ways.""" |
| 12 |
|
| 13 |
class Method(Enum): |
| 14 |
DOCKER_KILL = auto() |
| 15 |
CONTROL_SHUTDOWN = auto() |
| 16 |
SIGTERM = auto() |
| 17 |
SIGQUIT = auto() |
| 18 |
|
| 19 |
def kill_worker( |
| 20 |
self, |
| 21 |
worker: CeleryTestWorker, |
| 22 |
method: WorkerKill.Method, |
| 23 |
) -> None: |
| 24 |
"""Kill a Celery worker. |
| 25 |
|
| 26 |
Args: |
| 27 |
worker (CeleryTestWorker): Worker to kill. |
| 28 |
method (WorkerKill.Method): The method to kill the worker. |
| 29 |
""" |
| 30 |
if method == WorkerKill.Method.DOCKER_KILL: |
| 31 |
worker.kill() |
| 32 |
|
| 33 |
assert worker.container.status == "exited", ( |
| 34 |
f"Worker container should be in 'exited' state after kill, " |
| 35 |
f"but is in '{worker.container.status}' state instead." |
| 36 |
) |
| 37 |
|
| 38 |
if method == WorkerKill.Method.CONTROL_SHUTDOWN: |
| 39 |
control: Control = worker.app.control |
| 40 |
control.shutdown(destination=[worker.hostname()]) |
| 41 |
worker.container.reload() |
| 42 |
|
| 43 |
if method == WorkerKill.Method.SIGTERM: |
| 44 |
worker.kill(signal="SIGTERM") |
| 45 |
|
| 46 |
if method == WorkerKill.Method.SIGQUIT: |
| 47 |
worker.kill(signal="SIGQUIT") |