| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
from enum import Enum, auto |
| 4 |
|
| 5 |
from pytest_celery import CeleryTestWorker |
| 6 |
|
| 7 |
|
| 8 |
class WorkerRestart: |
| 9 |
"""Restarts a worker in different ways.""" |
| 10 |
class Method(Enum): |
| 11 |
POOL_RESTART = auto() |
| 12 |
DOCKER_RESTART_GRACEFULLY = auto() |
| 13 |
DOCKER_RESTART_FORCE = auto() |
| 14 |
|
| 15 |
def restart_worker( |
| 16 |
self, |
| 17 |
worker: CeleryTestWorker, |
| 18 |
method: WorkerRestart.Method, |
| 19 |
assertion: bool = True, |
| 20 |
) -> None: |
| 21 |
"""Restart a Celery worker. |
| 22 |
|
| 23 |
Args: |
| 24 |
worker (CeleryTestWorker): Worker to restart. |
| 25 |
method (WorkerRestart.Method): The method to restart the worker. |
| 26 |
assertion (bool, optional): Whether to assert the worker state after restart. Defaults to True. |
| 27 |
""" |
| 28 |
if method == WorkerRestart.Method.POOL_RESTART: |
| 29 |
worker.app.control.pool_restart() |
| 30 |
worker.container.reload() |
| 31 |
|
| 32 |
if method == WorkerRestart.Method.DOCKER_RESTART_GRACEFULLY: |
| 33 |
worker.restart() |
| 34 |
|
| 35 |
if method == WorkerRestart.Method.DOCKER_RESTART_FORCE: |
| 36 |
worker.restart(force=True) |
| 37 |
|
| 38 |
if assertion: |
| 39 |
assert worker.container.status == "running", ( |
| 40 |
f"Worker container should be in 'running' state after restart, " |
| 41 |
f"but is in '{worker.container.status}' state instead." |
| 42 |
) |