| 1 |
.. _guide-monitoring: |
| 2 |
|
| 3 |
================================= |
| 4 |
Monitoring and Management Guide |
| 5 |
================================= |
| 6 |
|
| 7 |
.. contents:: |
| 8 |
:local: |
| 9 |
|
| 10 |
Introduction |
| 11 |
============ |
| 12 |
|
| 13 |
There are several tools available to monitor and inspect Celery clusters. |
| 14 |
|
| 15 |
This document describes some of these, as well as |
| 16 |
features related to monitoring, like events and broadcast commands. |
| 17 |
|
| 18 |
.. _monitoring-workers: |
| 19 |
|
| 20 |
Workers |
| 21 |
======= |
| 22 |
|
| 23 |
.. _monitoring-control: |
| 24 |
|
| 25 |
Management Command-line Utilities (``inspect``/``control``) |
| 26 |
----------------------------------------------------------- |
| 27 |
|
| 28 |
|
| 29 |
:program:`celery` can also be used to inspect |
| 30 |
and manage worker nodes (and to some degree tasks). |
| 31 |
|
| 32 |
To list all the commands available do: |
| 33 |
|
| 34 |
.. code-block:: console |
| 35 |
|
| 36 |
$ celery --help |
| 37 |
|
| 38 |
or to get help for a specific command do: |
| 39 |
|
| 40 |
.. code-block:: console |
| 41 |
|
| 42 |
$ celery <command> --help |
| 43 |
|
| 44 |
Commands |
| 45 |
~~~~~~~~ |
| 46 |
|
| 47 |
* **shell**: Drop into a Python shell. |
| 48 |
|
| 49 |
The locals will include the ``celery`` variable: this is the current app. |
| 50 |
Also all known tasks will be automatically added to locals (unless the |
| 51 |
:option:`--without-tasks <celery shell --without-tasks>` flag is set). |
| 52 |
|
| 53 |
Uses :pypi:`Ipython`, :pypi:`bpython`, or regular :program:`python` in that |
| 54 |
order if installed. You can force an implementation using |
| 55 |
:option:`--ipython <celery shell --ipython>`, |
| 56 |
:option:`--bpython <celery shell --bpython>`, or |
| 57 |
:option:`--python <celery shell --python>`. |
| 58 |
|
| 59 |
* **status**: List active nodes in this cluster |
| 60 |
|
| 61 |
.. code-block:: console |
| 62 |
|
| 63 |
$ celery -A proj status |
| 64 |
|
| 65 |
* **result**: Show the result of a task |
| 66 |
|
| 67 |
.. code-block:: console |
| 68 |
|
| 69 |
$ celery -A proj result -t tasks.add 4e196aa4-0141-4601-8138-7aa33db0f577 |
| 70 |
|
| 71 |
Note that you can omit the name of the task as long as the |
| 72 |
task doesn't use a custom result backend. |
| 73 |
|
| 74 |
* **purge**: Purge messages from all configured task queues. |
| 75 |
|
| 76 |
This command will remove all messages from queues configured in |
| 77 |
the :setting:`CELERY_QUEUES` setting: |
| 78 |
|
| 79 |
.. warning:: |
| 80 |
|
| 81 |
There's no undo for this operation, and messages will |
| 82 |
be permanently deleted! |
| 83 |
|
| 84 |
.. code-block:: console |
| 85 |
|
| 86 |
$ celery -A proj purge |
| 87 |
|
| 88 |
|
| 89 |
You can also specify the queues to purge using the `-Q` option: |
| 90 |
|
| 91 |
.. code-block:: console |
| 92 |
|
| 93 |
$ celery -A proj purge -Q celery,foo,bar |
| 94 |
|
| 95 |
and exclude queues from being purged using the `-X` option: |
| 96 |
|
| 97 |
.. code-block:: console |
| 98 |
|
| 99 |
$ celery -A proj purge -X celery |
| 100 |
|
| 101 |
* **inspect active**: List active tasks |
| 102 |
|
| 103 |
.. code-block:: console |
| 104 |
|
| 105 |
$ celery -A proj inspect active |
| 106 |
|
| 107 |
These are all the tasks that are currently being executed. |
| 108 |
|
| 109 |
* **inspect scheduled**: List scheduled ETA tasks |
| 110 |
|
| 111 |
.. code-block:: console |
| 112 |
|
| 113 |
$ celery -A proj inspect scheduled |
| 114 |
|
| 115 |
These are tasks reserved by the worker when they have an |
| 116 |
`eta` or `countdown` argument set. |
| 117 |
|
| 118 |
* **inspect reserved**: List reserved tasks |
| 119 |
|
| 120 |
.. code-block:: console |
| 121 |
|
| 122 |
$ celery -A proj inspect reserved |
| 123 |
|
| 124 |
This will list all tasks that have been prefetched by the worker, |
| 125 |
and is currently waiting to be executed (doesn't include tasks |
| 126 |
with an ETA value set). |
| 127 |
|
| 128 |
* **inspect revoked**: List history of revoked tasks |
| 129 |
|
| 130 |
.. code-block:: console |
| 131 |
|
| 132 |
$ celery -A proj inspect revoked |
| 133 |
|
| 134 |
* **inspect registered**: List registered tasks |
| 135 |
|
| 136 |
.. code-block:: console |
| 137 |
|
| 138 |
$ celery -A proj inspect registered |
| 139 |
|
| 140 |
* **inspect stats**: Show worker statistics (see :ref:`worker-statistics`) |
| 141 |
|
| 142 |
.. code-block:: console |
| 143 |
|
| 144 |
$ celery -A proj inspect stats |
| 145 |
|
| 146 |
* **inspect query_task**: Show information about task(s) by id. |
| 147 |
|
| 148 |
Any worker having a task in this set of ids reserved/active will respond |
| 149 |
with status and information. |
| 150 |
|
| 151 |
.. code-block:: console |
| 152 |
|
| 153 |
$ celery -A proj inspect query_task e9f6c8f0-fec9-4ae8-a8c6-cf8c8451d4f8 |
| 154 |
|
| 155 |
You can also query for information about multiple tasks: |
| 156 |
|
| 157 |
.. code-block:: console |
| 158 |
|
| 159 |
$ celery -A proj inspect query_task id1 id2 ... idN |
| 160 |
|
| 161 |
* **control enable_events**: Enable events |
| 162 |
|
| 163 |
.. code-block:: console |
| 164 |
|
| 165 |
$ celery -A proj control enable_events |
| 166 |
|
| 167 |
* **control disable_events**: Disable events |
| 168 |
|
| 169 |
.. code-block:: console |
| 170 |
|
| 171 |
$ celery -A proj control disable_events |
| 172 |
|
| 173 |
* **migrate**: Migrate tasks from one broker to another (**EXPERIMENTAL**). |
| 174 |
|
| 175 |
.. code-block:: console |
| 176 |
|
| 177 |
$ celery -A proj migrate redis://localhost amqp://localhost |
| 178 |
|
| 179 |
This command will migrate all the tasks on one broker to another. |
| 180 |
As this command is new and experimental you should be sure to have |
| 181 |
a backup of the data before proceeding. |
| 182 |
|
| 183 |
.. note:: |
| 184 |
|
| 185 |
All ``inspect`` and ``control`` commands supports a |
| 186 |
:option:`--timeout <celery inspect --timeout>` argument, |
| 187 |
This is the number of seconds to wait for responses. |
| 188 |
You may have to increase this timeout if you're not getting a response |
| 189 |
due to latency. |
| 190 |
|
| 191 |
.. _inspect-destination: |
| 192 |
|
| 193 |
Specifying destination nodes |
| 194 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 195 |
|
| 196 |
By default the inspect and control commands operates on all workers. |
| 197 |
You can specify a single, or a list of workers by using the |
| 198 |
:option:`--destination <celery inspect --destination>` argument: |
| 199 |
|
| 200 |
.. code-block:: console |
| 201 |
|
| 202 |
$ celery -A proj inspect -d [email protected],[email protected] reserved |
| 203 |
|
| 204 |
$ celery -A proj control -d [email protected],[email protected] enable_events |
| 205 |
|
| 206 |
|
| 207 |
.. _monitoring-flower: |
| 208 |
|
| 209 |
Flower: Real-time Celery web-monitor |
| 210 |
------------------------------------ |
| 211 |
|
| 212 |
Flower is a real-time web based monitor and administration tool for Celery. |
| 213 |
It's under active development, but is already an essential tool. |
| 214 |
Being the recommended monitor for Celery, it obsoletes the Django-Admin |
| 215 |
monitor, ``celerymon`` and the ``ncurses`` based monitor. |
| 216 |
|
| 217 |
Flower is pronounced like "flow", but you can also use the botanical version |
| 218 |
if you prefer. |
| 219 |
|
| 220 |
Features |
| 221 |
~~~~~~~~ |
| 222 |
|
| 223 |
- Real-time monitoring using Celery Events |
| 224 |
|
| 225 |
- Task progress and history |
| 226 |
- Ability to show task details (arguments, start time, run-time, and more) |
| 227 |
- Graphs and statistics |
| 228 |
|
| 229 |
- Remote Control |
| 230 |
|
| 231 |
- View worker status and statistics |
| 232 |
- Shutdown and restart worker instances |
| 233 |
- Control worker pool size and autoscale settings |
| 234 |
- View and modify the queues a worker instance consumes from |
| 235 |
- View currently running tasks |
| 236 |
- View scheduled tasks (ETA/countdown) |
| 237 |
- View reserved and revoked tasks |
| 238 |
- Apply time and rate limits |
| 239 |
- Configuration viewer |
| 240 |
- Revoke or terminate tasks |
| 241 |
|
| 242 |
- HTTP API |
| 243 |
|
| 244 |
- List workers |
| 245 |
- Shut down a worker |
| 246 |
- Restart worker’s pool |
| 247 |
- Grow worker’s pool |
| 248 |
- Shrink worker’s pool |
| 249 |
- Autoscale worker pool |
| 250 |
- Start consuming from a queue |
| 251 |
- Stop consuming from a queue |
| 252 |
- List tasks |
| 253 |
- List (seen) task types |
| 254 |
- Get a task info |
| 255 |
- Execute a task |
| 256 |
- Execute a task by name |
| 257 |
- Get a task result |
| 258 |
- Change soft and hard time limits for a task |
| 259 |
- Change rate limit for a task |
| 260 |
- Revoke a task |
| 261 |
|
| 262 |
- OpenID authentication |
| 263 |
|
| 264 |
**Screenshots** |
| 265 |
|
| 266 |
.. figure:: ../images/dashboard.png |
| 267 |
:width: 700px |
| 268 |
|
| 269 |
More screenshots_: |
| 270 |
|
| 271 |
.. _screenshots: https://github.com/mher/flower/tree/master/docs/screenshots |
| 272 |
|
| 273 |
Usage |
| 274 |
~~~~~ |
| 275 |
|
| 276 |
You can use pip to install Flower: |
| 277 |
|
| 278 |
.. code-block:: console |
| 279 |
|
| 280 |
$ pip install flower |
| 281 |
|
| 282 |
Running the flower command will start a web-server that you can visit: |
| 283 |
|
| 284 |
.. code-block:: console |
| 285 |
|
| 286 |
$ celery -A proj flower |
| 287 |
|
| 288 |
The default port is http://localhost:5555, but you can change this using the |
| 289 |
`--port`_ argument: |
| 290 |
|
| 291 |
.. _--port: https://flower.readthedocs.io/en/latest/config.html#port |
| 292 |
|
| 293 |
.. code-block:: console |
| 294 |
|
| 295 |
$ celery -A proj flower --port=5555 |
| 296 |
|
| 297 |
Broker URL can also be passed through the |
| 298 |
:option:`--broker <celery --broker>` argument : |
| 299 |
|
| 300 |
.. code-block:: console |
| 301 |
|
| 302 |
$ celery --broker=amqp://guest:guest@localhost:5672// flower |
| 303 |
or |
| 304 |
$ celery --broker=redis://guest:guest@localhost:6379/0 flower |
| 305 |
|
| 306 |
Then, you can visit flower in your web browser : |
| 307 |
|
| 308 |
.. code-block:: console |
| 309 |
|
| 310 |
$ open http://localhost:5555 |
| 311 |
|
| 312 |
Flower has many more features than are detailed here, including |
| 313 |
authorization options. Check out the `official documentation`_ for more |
| 314 |
information. |
| 315 |
|
| 316 |
.. _official documentation: https://flower.readthedocs.io/en/latest/ |
| 317 |
|
| 318 |
|
| 319 |
.. _monitoring-celeryev: |
| 320 |
|
| 321 |
celery events: Curses Monitor |
| 322 |
----------------------------- |
| 323 |
|
| 324 |
.. versionadded:: 2.0 |
| 325 |
|
| 326 |
`celery events` is a simple curses monitor displaying |
| 327 |
task and worker history. You can inspect the result and traceback of tasks, |
| 328 |
and it also supports some management commands like rate limiting and shutting |
| 329 |
down workers. This monitor was started as a proof of concept, and you |
| 330 |
probably want to use Flower instead. |
| 331 |
|
| 332 |
Starting: |
| 333 |
|
| 334 |
.. code-block:: console |
| 335 |
|
| 336 |
$ celery -A proj events |
| 337 |
|
| 338 |
You should see a screen like: |
| 339 |
|
| 340 |
.. figure:: ../images/celeryevshotsm.jpg |
| 341 |
|
| 342 |
|
| 343 |
`celery events` is also used to start snapshot cameras (see |
| 344 |
:ref:`monitoring-snapshots`: |
| 345 |
|
| 346 |
.. code-block:: console |
| 347 |
|
| 348 |
$ celery -A proj events --camera=<camera-class> --frequency=1.0 |
| 349 |
|
| 350 |
and it includes a tool to dump events to :file:`stdout`: |
| 351 |
|
| 352 |
.. code-block:: console |
| 353 |
|
| 354 |
$ celery -A proj events --dump |
| 355 |
|
| 356 |
For a complete list of options use :option:`!--help`: |
| 357 |
|
| 358 |
.. code-block:: console |
| 359 |
|
| 360 |
$ celery events --help |
| 361 |
|
| 362 |
.. _`celerymon`: https://github.com/celery/celerymon/ |
| 363 |
|
| 364 |
.. _monitoring-rabbitmq: |
| 365 |
|
| 366 |
RabbitMQ |
| 367 |
======== |
| 368 |
|
| 369 |
To manage a Celery cluster it is important to know how |
| 370 |
RabbitMQ can be monitored. |
| 371 |
|
| 372 |
RabbitMQ ships with the `rabbitmqctl(1)`_ command, |
| 373 |
with this you can list queues, exchanges, bindings, |
| 374 |
queue lengths, the memory usage of each queue, as well |
| 375 |
as manage users, virtual hosts and their permissions. |
| 376 |
|
| 377 |
.. note:: |
| 378 |
|
| 379 |
The default virtual host (``"/"``) is used in these |
| 380 |
examples, if you use a custom virtual host you have to add |
| 381 |
the ``-p`` argument to the command, for example: |
| 382 |
``rabbitmqctl list_queues -p my_vhost …`` |
| 383 |
|
| 384 |
.. _`rabbitmqctl(1)`: http://www.rabbitmq.com/man/rabbitmqctl.1.man.html |
| 385 |
|
| 386 |
.. _monitoring-rmq-queues: |
| 387 |
|
| 388 |
Inspecting queues |
| 389 |
----------------- |
| 390 |
|
| 391 |
Finding the number of tasks in a queue: |
| 392 |
|
| 393 |
.. code-block:: console |
| 394 |
|
| 395 |
$ rabbitmqctl list_queues name messages messages_ready \ |
| 396 |
messages_unacknowledged |
| 397 |
|
| 398 |
|
| 399 |
Here `messages_ready` is the number of messages ready |
| 400 |
for delivery (sent but not received), `messages_unacknowledged` |
| 401 |
is the number of messages that's been received by a worker but |
| 402 |
not acknowledged yet (meaning it is in progress, or has been reserved). |
| 403 |
`messages` is the sum of ready and unacknowledged messages. |
| 404 |
|
| 405 |
|
| 406 |
Finding the number of workers currently consuming from a queue: |
| 407 |
|
| 408 |
.. code-block:: console |
| 409 |
|
| 410 |
$ rabbitmqctl list_queues name consumers |
| 411 |
|
| 412 |
Finding the amount of memory allocated to a queue: |
| 413 |
|
| 414 |
.. code-block:: console |
| 415 |
|
| 416 |
$ rabbitmqctl list_queues name memory |
| 417 |
|
| 418 |
:Tip: Adding the ``-q`` option to `rabbitmqctl(1)`_ makes the output |
| 419 |
easier to parse. |
| 420 |
|
| 421 |
|
| 422 |
.. _monitoring-redis: |
| 423 |
|
| 424 |
Redis |
| 425 |
===== |
| 426 |
|
| 427 |
If you're using Redis as the broker, you can monitor the Celery cluster using |
| 428 |
the `redis-cli(1)` command to list lengths of queues. |
| 429 |
|
| 430 |
.. _monitoring-redis-queues: |
| 431 |
|
| 432 |
Inspecting queues |
| 433 |
----------------- |
| 434 |
|
| 435 |
Finding the number of tasks in a queue: |
| 436 |
|
| 437 |
.. code-block:: console |
| 438 |
|
| 439 |
$ redis-cli -h HOST -p PORT -n DATABASE_NUMBER llen QUEUE_NAME |
| 440 |
|
| 441 |
The default queue is named `celery`. To get all available queues, invoke: |
| 442 |
|
| 443 |
.. code-block:: console |
| 444 |
|
| 445 |
$ redis-cli -h HOST -p PORT -n DATABASE_NUMBER keys \* |
| 446 |
|
| 447 |
.. note:: |
| 448 |
|
| 449 |
Queue keys only exists when there are tasks in them, so if a key |
| 450 |
doesn't exist it simply means there are no messages in that queue. |
| 451 |
This is because in Redis a list with no elements in it is automatically |
| 452 |
removed, and hence it won't show up in the `keys` command output, |
| 453 |
and `llen` for that list returns 0. |
| 454 |
|
| 455 |
Also, if you're using Redis for other purposes, the |
| 456 |
output of the `keys` command will include unrelated values stored in |
| 457 |
the database. The recommended way around this is to use a |
| 458 |
dedicated `DATABASE_NUMBER` for Celery, you can also use |
| 459 |
database numbers to separate Celery applications from each other (virtual |
| 460 |
hosts), but this won't affect the monitoring events used by for example |
| 461 |
Flower as Redis pub/sub commands are global rather than database based. |
| 462 |
|
| 463 |
.. _monitoring-prometheus: |
| 464 |
|
| 465 |
Prometheus |
| 466 |
========= |
| 467 |
|
| 468 |
While Prometheus monitoring is not a native part of Celery, |
| 469 |
you can easily monitor your Celery workers using Prometheus via Flower. |
| 470 |
Flower also provides pre-made Grafana dashboards to easily graph the amount |
| 471 |
of tasks, workers and other instrumental statistics. |
| 472 |
|
| 473 |
To set up Prometheus, refer to the Flower documentation: https://flower.readthedocs.io/en/latest/prometheus-integration.html |
| 474 |
|
| 475 |
.. _monitoring-munin: |
| 476 |
|
| 477 |
Munin |
| 478 |
===== |
| 479 |
|
| 480 |
This is a list of known Munin plug-ins that can be useful when |
| 481 |
maintaining a Celery cluster. |
| 482 |
|
| 483 |
* ``rabbitmq-munin``: Munin plug-ins for RabbitMQ. |
| 484 |
|
| 485 |
https://github.com/ask/rabbitmq-munin |
| 486 |
|
| 487 |
* ``celery_tasks``: Monitors the number of times each task type has |
| 488 |
been executed (requires `celerymon`). |
| 489 |
|
| 490 |
https://github.com/munin-monitoring/contrib/blob/master/plugins/celery/celery_tasks |
| 491 |
|
| 492 |
* ``celery_tasks_states``: Monitors the number of tasks in each state |
| 493 |
(requires `celerymon`). |
| 494 |
|
| 495 |
https://github.com/munin-monitoring/contrib/blob/master/plugins/celery/celery_tasks_states |
| 496 |
|
| 497 |
.. _monitoring-events: |
| 498 |
|
| 499 |
Events |
| 500 |
====== |
| 501 |
|
| 502 |
The worker has the ability to send a message whenever some event |
| 503 |
happens. These events are then captured by tools like Flower, |
| 504 |
and :program:`celery events` to monitor the cluster. |
| 505 |
|
| 506 |
.. _monitoring-snapshots: |
| 507 |
|
| 508 |
Snapshots |
| 509 |
--------- |
| 510 |
|
| 511 |
.. versionadded:: 2.1 |
| 512 |
|
| 513 |
Even a single worker can produce a huge amount of events, so storing |
| 514 |
the history of all events on disk may be very expensive. |
| 515 |
|
| 516 |
A sequence of events describes the cluster state in that time period, |
| 517 |
by taking periodic snapshots of this state you can keep all history, but |
| 518 |
still only periodically write it to disk. |
| 519 |
|
| 520 |
To take snapshots you need a Camera class, with this you can define |
| 521 |
what should happen every time the state is captured; You can |
| 522 |
write it to a database, send it by email or something else entirely. |
| 523 |
|
| 524 |
:program:`celery events` is then used to take snapshots with the camera, |
| 525 |
for example if you want to capture state every 2 seconds using the |
| 526 |
camera ``myapp.Camera`` you run :program:`celery events` with the following |
| 527 |
arguments: |
| 528 |
|
| 529 |
.. code-block:: console |
| 530 |
|
| 531 |
$ celery -A proj events -c myapp.Camera --frequency=2.0 |
| 532 |
|
| 533 |
|
| 534 |
.. _monitoring-camera: |
| 535 |
|
| 536 |
Custom Camera |
| 537 |
~~~~~~~~~~~~~ |
| 538 |
|
| 539 |
Cameras can be useful if you need to capture events and do something |
| 540 |
with those events at an interval. For real-time event processing |
| 541 |
you should use :class:`@events.Receiver` directly, like in |
| 542 |
:ref:`event-real-time-example`. |
| 543 |
|
| 544 |
Here is an example camera, dumping the snapshot to screen: |
| 545 |
|
| 546 |
.. code-block:: python |
| 547 |
|
| 548 |
from pprint import pformat |
| 549 |
|
| 550 |
from celery.events.snapshot import Polaroid |
| 551 |
|
| 552 |
class DumpCam(Polaroid): |
| 553 |
clear_after = True # clear after flush (incl, state.event_count). |
| 554 |
|
| 555 |
def on_shutter(self, state): |
| 556 |
if not state.event_count: |
| 557 |
# No new events since last snapshot. |
| 558 |
return |
| 559 |
print('Workers: {0}'.format(pformat(state.workers, indent=4))) |
| 560 |
print('Tasks: {0}'.format(pformat(state.tasks, indent=4))) |
| 561 |
print('Total: {0.event_count} events, {0.task_count} tasks'.format( |
| 562 |
state)) |
| 563 |
|
| 564 |
See the API reference for :mod:`celery.events.state` to read more |
| 565 |
about state objects. |
| 566 |
|
| 567 |
Now you can use this cam with :program:`celery events` by specifying |
| 568 |
it with the :option:`-c <celery events -c>` option: |
| 569 |
|
| 570 |
.. code-block:: console |
| 571 |
|
| 572 |
$ celery -A proj events -c myapp.DumpCam --frequency=2.0 |
| 573 |
|
| 574 |
Or you can use it programmatically like this: |
| 575 |
|
| 576 |
.. code-block:: python |
| 577 |
|
| 578 |
from celery import Celery |
| 579 |
from myapp import DumpCam |
| 580 |
|
| 581 |
def main(app, freq=1.0): |
| 582 |
state = app.events.State() |
| 583 |
with app.connection() as connection: |
| 584 |
recv = app.events.Receiver(connection, handlers={'*': state.event}) |
| 585 |
with DumpCam(state, freq=freq): |
| 586 |
recv.capture(limit=None, timeout=None) |
| 587 |
|
| 588 |
if __name__ == '__main__': |
| 589 |
app = Celery(broker='amqp://guest@localhost//') |
| 590 |
main(app) |
| 591 |
|
| 592 |
.. _event-real-time-example: |
| 593 |
|
| 594 |
Real-time processing |
| 595 |
-------------------- |
| 596 |
|
| 597 |
To process events in real-time you need the following |
| 598 |
|
| 599 |
- An event consumer (this is the ``Receiver``) |
| 600 |
|
| 601 |
- A set of handlers called when events come in. |
| 602 |
|
| 603 |
You can have different handlers for each event type, |
| 604 |
or a catch-all handler can be used ('*') |
| 605 |
|
| 606 |
- State (optional) |
| 607 |
|
| 608 |
:class:`@events.State` is a convenient in-memory representation |
| 609 |
of tasks and workers in the cluster that's updated as events come in. |
| 610 |
|
| 611 |
It encapsulates solutions for many common things, like checking if a |
| 612 |
worker is still alive (by verifying heartbeats), merging event fields |
| 613 |
together as events come in, making sure time-stamps are in sync, and so on. |
| 614 |
|
| 615 |
|
| 616 |
Combining these you can easily process events in real-time: |
| 617 |
|
| 618 |
|
| 619 |
.. code-block:: python |
| 620 |
|
| 621 |
from celery import Celery |
| 622 |
|
| 623 |
|
| 624 |
def my_monitor(app): |
| 625 |
state = app.events.State() |
| 626 |
|
| 627 |
def announce_failed_tasks(event): |
| 628 |
state.event(event) |
| 629 |
# task name is sent only with -received event, and state |
| 630 |
# will keep track of this for us. |
| 631 |
task = state.tasks.get(event['uuid']) |
| 632 |
|
| 633 |
print('TASK FAILED: %s[%s] %s' % ( |
| 634 |
task.name, task.uuid, task.info(),)) |
| 635 |
|
| 636 |
with app.connection() as connection: |
| 637 |
recv = app.events.Receiver(connection, handlers={ |
| 638 |
'task-failed': announce_failed_tasks, |
| 639 |
'*': state.event, |
| 640 |
}) |
| 641 |
recv.capture(limit=None, timeout=None, wakeup=True) |
| 642 |
|
| 643 |
if __name__ == '__main__': |
| 644 |
app = Celery(broker='amqp://guest@localhost//') |
| 645 |
my_monitor(app) |
| 646 |
|
| 647 |
.. note:: |
| 648 |
|
| 649 |
The ``wakeup`` argument to ``capture`` sends a signal to all workers |
| 650 |
to force them to send a heartbeat. This way you can immediately see |
| 651 |
workers when the monitor starts. |
| 652 |
|
| 653 |
|
| 654 |
You can listen to specific events by specifying the handlers: |
| 655 |
|
| 656 |
.. code-block:: python |
| 657 |
|
| 658 |
from celery import Celery |
| 659 |
|
| 660 |
def my_monitor(app): |
| 661 |
state = app.events.State() |
| 662 |
|
| 663 |
def announce_failed_tasks(event): |
| 664 |
state.event(event) |
| 665 |
# task name is sent only with -received event, and state |
| 666 |
# will keep track of this for us. |
| 667 |
task = state.tasks.get(event['uuid']) |
| 668 |
|
| 669 |
print('TASK FAILED: %s[%s] %s' % ( |
| 670 |
task.name, task.uuid, task.info(),)) |
| 671 |
|
| 672 |
with app.connection() as connection: |
| 673 |
recv = app.events.Receiver(connection, handlers={ |
| 674 |
'task-failed': announce_failed_tasks, |
| 675 |
}) |
| 676 |
recv.capture(limit=None, timeout=None, wakeup=True) |
| 677 |
|
| 678 |
if __name__ == '__main__': |
| 679 |
app = Celery(broker='amqp://guest@localhost//') |
| 680 |
my_monitor(app) |
| 681 |
|
| 682 |
.. _event-reference: |
| 683 |
|
| 684 |
Event Reference |
| 685 |
=============== |
| 686 |
|
| 687 |
This list contains the events sent by the worker, and their arguments. |
| 688 |
|
| 689 |
.. _event-reference-task: |
| 690 |
|
| 691 |
Task Events |
| 692 |
----------- |
| 693 |
|
| 694 |
.. event:: task-sent |
| 695 |
|
| 696 |
task-sent |
| 697 |
~~~~~~~~~ |
| 698 |
|
| 699 |
:signature: ``task-sent(uuid, name, args, kwargs, retries, eta, expires, |
| 700 |
queue, exchange, routing_key, root_id, parent_id)`` |
| 701 |
|
| 702 |
Sent when a task message is published and |
| 703 |
the :setting:`task_send_sent_event` setting is enabled. |
| 704 |
|
| 705 |
.. event:: task-received |
| 706 |
|
| 707 |
task-received |
| 708 |
~~~~~~~~~~~~~ |
| 709 |
|
| 710 |
:signature: ``task-received(uuid, name, args, kwargs, retries, eta, hostname, |
| 711 |
timestamp, root_id, parent_id)`` |
| 712 |
|
| 713 |
Sent when the worker receives a task. |
| 714 |
|
| 715 |
.. event:: task-started |
| 716 |
|
| 717 |
task-started |
| 718 |
~~~~~~~~~~~~ |
| 719 |
|
| 720 |
:signature: ``task-started(uuid, hostname, timestamp, pid)`` |
| 721 |
|
| 722 |
Sent just before the worker executes the task. |
| 723 |
|
| 724 |
.. event:: task-succeeded |
| 725 |
|
| 726 |
task-succeeded |
| 727 |
~~~~~~~~~~~~~~ |
| 728 |
|
| 729 |
:signature: ``task-succeeded(uuid, result, runtime, hostname, timestamp)`` |
| 730 |
|
| 731 |
Sent if the task executed successfully. |
| 732 |
|
| 733 |
Run-time is the time it took to execute the task using the pool. |
| 734 |
(Starting from the task is sent to the worker pool, and ending when the |
| 735 |
pool result handler callback is called). |
| 736 |
|
| 737 |
.. event:: task-failed |
| 738 |
|
| 739 |
task-failed |
| 740 |
~~~~~~~~~~~ |
| 741 |
|
| 742 |
:signature: ``task-failed(uuid, exception, traceback, hostname, timestamp)`` |
| 743 |
|
| 744 |
Sent if the execution of the task failed. |
| 745 |
|
| 746 |
.. event:: task-rejected |
| 747 |
|
| 748 |
task-rejected |
| 749 |
~~~~~~~~~~~~~ |
| 750 |
|
| 751 |
:signature: ``task-rejected(uuid, requeue)`` |
| 752 |
|
| 753 |
The task was rejected by the worker, possibly to be re-queued or moved to a |
| 754 |
dead letter queue. |
| 755 |
|
| 756 |
.. event:: task-revoked |
| 757 |
|
| 758 |
task-revoked |
| 759 |
~~~~~~~~~~~~ |
| 760 |
|
| 761 |
:signature: ``task-revoked(uuid, terminated, signum, expired)`` |
| 762 |
|
| 763 |
Sent if the task has been revoked (Note that this is likely |
| 764 |
to be sent by more than one worker). |
| 765 |
|
| 766 |
- ``terminated`` is set to true if the task process was terminated, |
| 767 |
and the ``signum`` field set to the signal used. |
| 768 |
|
| 769 |
- ``expired`` is set to true if the task expired. |
| 770 |
|
| 771 |
.. event:: task-retried |
| 772 |
|
| 773 |
task-retried |
| 774 |
~~~~~~~~~~~~ |
| 775 |
|
| 776 |
:signature: ``task-retried(uuid, exception, traceback, hostname, timestamp)`` |
| 777 |
|
| 778 |
Sent if the task failed, but will be retried in the future. |
| 779 |
|
| 780 |
.. _event-reference-worker: |
| 781 |
|
| 782 |
Worker Events |
| 783 |
------------- |
| 784 |
|
| 785 |
.. event:: worker-online |
| 786 |
|
| 787 |
worker-online |
| 788 |
~~~~~~~~~~~~~ |
| 789 |
|
| 790 |
:signature: ``worker-online(hostname, timestamp, freq, sw_ident, sw_ver, sw_sys)`` |
| 791 |
|
| 792 |
The worker has connected to the broker and is online. |
| 793 |
|
| 794 |
- `hostname`: Nodename of the worker. |
| 795 |
- `timestamp`: Event time-stamp. |
| 796 |
- `freq`: Heartbeat frequency in seconds (float). |
| 797 |
- `sw_ident`: Name of worker software (e.g., ``py-celery``). |
| 798 |
- `sw_ver`: Software version (e.g., 2.2.0). |
| 799 |
- `sw_sys`: Operating System (e.g., Linux/Darwin). |
| 800 |
|
| 801 |
.. event:: worker-heartbeat |
| 802 |
|
| 803 |
worker-heartbeat |
| 804 |
~~~~~~~~~~~~~~~~ |
| 805 |
|
| 806 |
:signature: ``worker-heartbeat(hostname, timestamp, freq, sw_ident, sw_ver, sw_sys, |
| 807 |
active, processed)`` |
| 808 |
|
| 809 |
Sent every minute, if the worker hasn't sent a heartbeat in 2 minutes, |
| 810 |
it is considered to be offline. |
| 811 |
|
| 812 |
- `hostname`: Nodename of the worker. |
| 813 |
- `timestamp`: Event time-stamp. |
| 814 |
- `freq`: Heartbeat frequency in seconds (float). |
| 815 |
- `sw_ident`: Name of worker software (e.g., ``py-celery``). |
| 816 |
- `sw_ver`: Software version (e.g., 2.2.0). |
| 817 |
- `sw_sys`: Operating System (e.g., Linux/Darwin). |
| 818 |
- `active`: Number of currently executing tasks. |
| 819 |
- `processed`: Total number of tasks processed by this worker. |
| 820 |
|
| 821 |
.. event:: worker-offline |
| 822 |
|
| 823 |
worker-offline |
| 824 |
~~~~~~~~~~~~~~ |
| 825 |
|
| 826 |
:signature: ``worker-offline(hostname, timestamp, freq, sw_ident, sw_ver, sw_sys)`` |
| 827 |
|
| 828 |
The worker has disconnected from the broker. |
| 829 |
|
| 830 |
Mailbox Configuration (Advanced) |
| 831 |
-------------------------------- |
| 832 |
|
| 833 |
Celery uses `kombu.pidbox.Mailbox` internally to send control and broadcast commands |
| 834 |
to workers. |
| 835 |
|
| 836 |
.. versionadded:: Kombu 5.6.0 |
| 837 |
|
| 838 |
Advanced users can configure the behavior of this mailbox by customizing how it is created. |
| 839 |
The following parameters are now supported by `Mailbox`: |
| 840 |
|
| 841 |
- ``durable`` (default: ``False``): If set to ``True``, the control exchanges will survive broker restarts. |
| 842 |
- ``exclusive`` (default: ``False``): If set to ``True``, the exchanges will be usable by only one connection. |
| 843 |
|
| 844 |
.. warning:: |
| 845 |
|
| 846 |
Setting both ``durable=True`` and ``exclusive=True`` is not permitted and will |
| 847 |
raise an error, as these two options are mutually incompatible in AMQP. |
| 848 |
|
| 849 |
See :setting:`event_queue_durable` and :setting:`event_queue_exclusive` for advanced configuration. |