Debian Codegraph @

t/smoke/tasks.py

raw
1 """Smoke tests tasks."""
2
3 from __future__ import annotations
4
5 import os
6 import sys
7 from signal import SIGKILL
8 from time import sleep
9
10 import celery.utils
11 from celery import Task, shared_task, signature
12 from celery.canvas import Signature
13 from t.integration.tasks import * # noqa
14 from t.integration.tasks import replaced_with_me
15
16
17 @shared_task
18 def noop(*args, **kwargs) -> None:
19 return celery.utils.noop(*args, **kwargs)
20
21
22 @shared_task
23 def long_running_task(seconds: float = 1, verbose: bool = False) -> bool:
24 from celery import current_task
25 from celery.utils.log import get_task_logger
26
27 logger = get_task_logger(current_task.name)
28
29 logger.info("Starting long running task")
30
31 for i in range(0, int(seconds)):
32 sleep(1)
33 if verbose:
34 logger.info(f"Sleeping: {i}")
35
36 logger.info("Finished long running task")
37
38 return True
39
40
41 @shared_task(soft_time_limit=3, time_limit=5)
42 def soft_time_limit_lower_than_time_limit():
43 sleep(4)
44
45
46 @shared_task(soft_time_limit=5, time_limit=3)
47 def soft_time_limit_must_exceed_time_limit():
48 pass
49
50
51 @shared_task(bind=True)
52 def replace_with_task(self: Task, replace_with: Signature = None):
53 if replace_with is None:
54 replace_with = replaced_with_me.s()
55 return self.replace(signature(replace_with))
56
57
58 @shared_task
59 def self_termination_sigkill():
60 """Forceful termination."""
61 os.kill(os.getpid(), SIGKILL)
62
63
64 @shared_task
65 def self_termination_system_exit():
66 """Triggers a system exit to simulate a critical stop of the Celery worker."""
67 sys.exit(1)
68
69
70 @shared_task(time_limit=2)
71 def self_termination_delay_timeout():
72 """Delays the execution to simulate a task timeout."""
73 sleep(4)
74
75
76 @shared_task
77 def self_termination_exhaust_memory():
78 """Continuously allocates memory to simulate memory exhaustion."""
79 mem = []
80 while True:
81 mem.append(" " * 10**6)