| 1 |
"""myapp.py |
| 2 |
|
| 3 |
This is a simple example of how to use the stamping feature. |
| 4 |
It uses a custom stamping visitor to stamp a workflow with a unique |
| 5 |
monitoring id stamp (per task), and a different visitor to stamp the last |
| 6 |
task in the workflow. The last task is stamped with a consistent stamp, which |
| 7 |
is used to revoke the task by its stamped header using two different approaches: |
| 8 |
1. Run the workflow, then revoke the last task by its stamped header. |
| 9 |
2. Revoke the last task by its stamped header before running the workflow. |
| 10 |
|
| 11 |
Usage:: |
| 12 |
|
| 13 |
# The worker service reacts to messages by executing tasks. |
| 14 |
(window1)$ celery -A myapp worker -l INFO |
| 15 |
|
| 16 |
# The shell service is used to run the example. |
| 17 |
(window2)$ celery -A myapp shell |
| 18 |
|
| 19 |
# Use (copy) the content of the examples modules to run the workflow via the |
| 20 |
# shell service. |
| 21 |
|
| 22 |
# Use one of demo runs via the shell service: |
| 23 |
# 1) run_then_revoke(): Run the workflow and revoke the last task |
| 24 |
# by its stamped header during its run. |
| 25 |
# 2) revoke_then_run(): Revoke the last task by its stamped header |
| 26 |
# before its run, then run the workflow. |
| 27 |
# 3) Any of the examples in examples.py |
| 28 |
# |
| 29 |
# See worker logs for output per defined in task_received_handler(). |
| 30 |
""" |
| 31 |
import json |
| 32 |
|
| 33 |
# Import tasks in worker context |
| 34 |
import tasks # noqa |
| 35 |
from config import app |
| 36 |
|
| 37 |
from celery.signals import task_received |
| 38 |
|
| 39 |
|
| 40 |
@task_received.connect |
| 41 |
def task_received_handler(sender=None, request=None, signal=None, **kwargs): |
| 42 |
print(f"In {signal.name} for: {repr(request)}") |
| 43 |
if hasattr(request, "stamped_headers") and request.stamped_headers: |
| 44 |
print(f"Found stamps: {request.stamped_headers}") |
| 45 |
print(json.dumps(request.stamps, indent=4, sort_keys=True)) |
| 46 |
else: |
| 47 |
print("No stamps found") |
| 48 |
|
| 49 |
|
| 50 |
if __name__ == "__main__": |
| 51 |
app.start() |