Debian Codegraph @

t/smoke/operations/task_termination.py

raw
1 from __future__ import annotations
2
3 from enum import Enum, auto
4
5 from pytest_celery import CeleryTestWorker
6
7 from celery.canvas import Signature
8 from celery.result import AsyncResult
9 from t.smoke.tasks import (self_termination_delay_timeout, self_termination_exhaust_memory, self_termination_sigkill,
10 self_termination_system_exit)
11
12
13 class TaskTermination:
14 """Terminates a task in different ways."""
15 class Method(Enum):
16 SIGKILL = auto()
17 SYSTEM_EXIT = auto()
18 DELAY_TIMEOUT = auto()
19 EXHAUST_MEMORY = auto()
20
21 def apply_self_termination_task(
22 self,
23 worker: CeleryTestWorker,
24 method: TaskTermination.Method,
25 ) -> AsyncResult:
26 """Apply a task that will terminate itself.
27
28 Args:
29 worker (CeleryTestWorker): Take the queue of this worker.
30 method (TaskTermination.Method): The method to terminate the task.
31
32 Returns:
33 AsyncResult: The result of applying the task.
34 """
35 try:
36 self_termination_sig: Signature = {
37 TaskTermination.Method.SIGKILL: self_termination_sigkill.si(),
38 TaskTermination.Method.SYSTEM_EXIT: self_termination_system_exit.si(),
39 TaskTermination.Method.DELAY_TIMEOUT: self_termination_delay_timeout.si(),
40 TaskTermination.Method.EXHAUST_MEMORY: self_termination_exhaust_memory.si(),
41 }[method]
42
43 return self_termination_sig.apply_async(queue=worker.worker_queue)
44 finally:
45 # If there's an unexpected bug and the termination of the task caused the worker
46 # to crash, this will refresh the container object with the updated container status
47 # which can be asserted/checked during a test (for dev/debug)
48 worker.container.reload()