| 1 |
from time import sleep |
| 2 |
|
| 3 |
from config import app |
| 4 |
from visitors import FullVisitor, MonitoringIdStampingVisitor, MyStampingVisitor |
| 5 |
|
| 6 |
from celery import Task |
| 7 |
from celery.canvas import Signature, maybe_signature |
| 8 |
from celery.utils.log import get_task_logger |
| 9 |
|
| 10 |
logger = get_task_logger(__name__) |
| 11 |
|
| 12 |
|
| 13 |
def log_demo(running_task): |
| 14 |
request, name = running_task.request, running_task.name + running_task.request.argsrepr |
| 15 |
if hasattr(request, "stamps"): |
| 16 |
stamps = request.stamps or {} |
| 17 |
stamped_headers = request.stamped_headers or [] |
| 18 |
|
| 19 |
if stamps and stamped_headers: |
| 20 |
logger.critical(f"Found {name}.stamps: {stamps}") |
| 21 |
logger.critical(f"Found {name}.stamped_headers: {stamped_headers}") |
| 22 |
else: |
| 23 |
logger.critical(f"Running {name} without stamps") |
| 24 |
|
| 25 |
links = request.callbacks or [] |
| 26 |
for link in links: |
| 27 |
link = maybe_signature(link) |
| 28 |
logger.critical(f"Found {name}.link: {link}") |
| 29 |
stamped_headers = link.options.get("stamped_headers", []) |
| 30 |
stamps = {stamp: link.options[stamp] for stamp in stamped_headers} |
| 31 |
|
| 32 |
if stamps and stamped_headers: |
| 33 |
logger.critical(f"Found {name}.link stamps: {stamps}") |
| 34 |
logger.critical(f"Found {name}.link stamped_headers: {stamped_headers}") |
| 35 |
else: |
| 36 |
logger.critical(f"Running {name}.link without stamps") |
| 37 |
|
| 38 |
|
| 39 |
class StampOnReplace(Task): |
| 40 |
"""Custom task for stamping on replace""" |
| 41 |
|
| 42 |
def on_replace(self, sig: Signature): |
| 43 |
logger.warning(f"StampOnReplace: {sig}.stamp(FullVisitor())") |
| 44 |
sig.stamp(FullVisitor()) |
| 45 |
logger.warning(f"StampOnReplace: {sig}.stamp(MyStampingVisitor())") |
| 46 |
sig.stamp(MyStampingVisitor()) |
| 47 |
return super().on_replace(sig) |
| 48 |
|
| 49 |
|
| 50 |
class MonitoredTask(Task): |
| 51 |
def on_replace(self, sig: Signature): |
| 52 |
logger.warning(f"MonitoredTask: {sig}.stamp(MonitoringIdStampingVisitor())") |
| 53 |
sig.stamp(MonitoringIdStampingVisitor(), append_stamps=False) |
| 54 |
return super().on_replace(sig) |
| 55 |
|
| 56 |
|
| 57 |
@app.task(bind=True) |
| 58 |
def identity_task(self, x): |
| 59 |
"""Identity function""" |
| 60 |
log_demo(self) |
| 61 |
return x |
| 62 |
|
| 63 |
|
| 64 |
@app.task(bind=True, base=MonitoredTask) |
| 65 |
def replaced_identity(self: MonitoredTask, x): |
| 66 |
log_demo(self) |
| 67 |
logger.warning("Stamping identity_task with MonitoringIdStampingVisitor() before replace") |
| 68 |
replaced_task = identity_task.s(x) |
| 69 |
# These stamps should be overridden by the stamps from MonitoredTask.on_replace() |
| 70 |
replaced_task.stamp(MonitoringIdStampingVisitor()) |
| 71 |
return self.replace(replaced_task) |
| 72 |
|
| 73 |
|
| 74 |
@app.task(bind=True, base=StampOnReplace) |
| 75 |
def identity(self: Task, x): |
| 76 |
log_demo(self) |
| 77 |
return self.replace(replaced_identity.s(x)) |
| 78 |
|
| 79 |
|
| 80 |
@app.task |
| 81 |
def mul(x: int, y: int) -> int: |
| 82 |
"""Multiply two numbers""" |
| 83 |
return x * y |
| 84 |
|
| 85 |
|
| 86 |
@app.task |
| 87 |
def xsum(numbers: list) -> int: |
| 88 |
"""Sum a list of numbers""" |
| 89 |
return sum(numbers) |
| 90 |
|
| 91 |
|
| 92 |
@app.task |
| 93 |
def waitfor(seconds: int) -> None: |
| 94 |
"""Wait for "seconds" seconds, ticking every second.""" |
| 95 |
print(f"Waiting for {seconds} seconds...") |
| 96 |
for i in range(seconds): |
| 97 |
sleep(1) |
| 98 |
print(f"{i+1} seconds passed") |
| 99 |
|
| 100 |
|
| 101 |
@app.task(bind=True, base=StampOnReplace) |
| 102 |
def wait_for_revoke(self: StampOnReplace, seconds: int) -> None: |
| 103 |
"""Replace this task with a new task that waits for "seconds" seconds.""" |
| 104 |
self.replace(waitfor.s(seconds)) |