| 1 |
.. _guide-workers: |
| 2 |
|
| 3 |
=============== |
| 4 |
Workers Guide |
| 5 |
=============== |
| 6 |
|
| 7 |
.. contents:: |
| 8 |
:local: |
| 9 |
:depth: 1 |
| 10 |
|
| 11 |
.. _worker-starting: |
| 12 |
|
| 13 |
Starting the worker |
| 14 |
=================== |
| 15 |
|
| 16 |
.. sidebar:: Daemonizing |
| 17 |
|
| 18 |
You probably want to use a daemonization tool to start |
| 19 |
the worker in the background. See :ref:`daemonizing` for help |
| 20 |
starting the worker as a daemon using popular service managers. |
| 21 |
|
| 22 |
You can start the worker in the foreground by executing the command: |
| 23 |
|
| 24 |
.. code-block:: console |
| 25 |
|
| 26 |
$ celery -A proj worker -l INFO |
| 27 |
|
| 28 |
For a full list of available command-line options see |
| 29 |
:mod:`~celery.bin.worker`, or simply do: |
| 30 |
|
| 31 |
.. code-block:: console |
| 32 |
|
| 33 |
$ celery worker --help |
| 34 |
|
| 35 |
You can start multiple workers on the same machine, but |
| 36 |
be sure to name each individual worker by specifying a |
| 37 |
node name with the :option:`--hostname <celery worker --hostname>` argument: |
| 38 |
|
| 39 |
.. code-block:: console |
| 40 |
|
| 41 |
$ celery -A proj worker --loglevel=INFO --concurrency=10 -n worker1@%h |
| 42 |
$ celery -A proj worker --loglevel=INFO --concurrency=10 -n worker2@%h |
| 43 |
$ celery -A proj worker --loglevel=INFO --concurrency=10 -n worker3@%h |
| 44 |
|
| 45 |
The ``hostname`` argument can expand the following variables: |
| 46 |
|
| 47 |
- ``%h``: Hostname, including domain name. |
| 48 |
- ``%n``: Hostname only. |
| 49 |
- ``%d``: Domain name only. |
| 50 |
|
| 51 |
If the current hostname is *george.example.com*, these will expand to: |
| 52 |
|
| 53 |
+----------+----------------+------------------------------+ |
| 54 |
| Variable | Template | Result | |
| 55 |
+----------+----------------+------------------------------+ |
| 56 |
| ``%h`` | ``worker1@%h`` | *[email protected]* | |
| 57 |
+----------+----------------+------------------------------+ |
| 58 |
| ``%n`` | ``worker1@%n`` | *worker1@george* | |
| 59 |
+----------+----------------+------------------------------+ |
| 60 |
| ``%d`` | ``worker1@%d`` | *[email protected]* | |
| 61 |
+----------+----------------+------------------------------+ |
| 62 |
|
| 63 |
.. admonition:: Note for :pypi:`supervisor` users |
| 64 |
|
| 65 |
The ``%`` sign must be escaped by adding a second one: `%%h`. |
| 66 |
|
| 67 |
.. _worker-stopping: |
| 68 |
|
| 69 |
Stopping the worker |
| 70 |
=================== |
| 71 |
|
| 72 |
Shutdown should be accomplished using the :sig:`TERM` signal. |
| 73 |
|
| 74 |
When shutdown is initiated the worker will finish all currently executing |
| 75 |
tasks before it actually terminates. If these tasks are important, you should |
| 76 |
wait for it to finish before doing anything drastic, like sending the :sig:`KILL` |
| 77 |
signal. |
| 78 |
|
| 79 |
If the worker won't shutdown after considerate time, for being |
| 80 |
stuck in an infinite-loop or similar, you can use the :sig:`KILL` signal to |
| 81 |
force terminate the worker: but be aware that currently executing tasks will |
| 82 |
be lost (i.e., unless the tasks have the :attr:`[email protected]_late` |
| 83 |
option set). |
| 84 |
|
| 85 |
Also as processes can't override the :sig:`KILL` signal, the worker will |
| 86 |
not be able to reap its children; make sure to do so manually. This |
| 87 |
command usually does the trick: |
| 88 |
|
| 89 |
.. code-block:: console |
| 90 |
|
| 91 |
$ pkill -9 -f 'celery worker' |
| 92 |
|
| 93 |
If you don't have the :command:`pkill` command on your system, you can use the slightly |
| 94 |
longer version: |
| 95 |
|
| 96 |
.. code-block:: console |
| 97 |
|
| 98 |
$ ps auxww | awk '/celery worker/ {print $2}' | xargs kill -9 |
| 99 |
|
| 100 |
.. versionchanged:: 5.2 |
| 101 |
On Linux systems, Celery now supports sending :sig:`KILL` signal to all child processes |
| 102 |
after worker termination. This is done via `PR_SET_PDEATHSIG` option of ``prctl(2)``. |
| 103 |
|
| 104 |
.. _worker_shutdown: |
| 105 |
|
| 106 |
Worker Shutdown |
| 107 |
--------------- |
| 108 |
|
| 109 |
We will use the terms *Warm, Soft, Cold, Hard* to describe the different stages of worker shutdown. |
| 110 |
The worker will initiate the shutdown process when it receives the :sig:`TERM` or :sig:`QUIT` signal. |
| 111 |
The :sig:`INT` (Ctrl-C) signal is also handled during the shutdown process and always triggers the |
| 112 |
next stage of the shutdown process. |
| 113 |
|
| 114 |
.. _worker-warm-shutdown: |
| 115 |
|
| 116 |
Warm Shutdown |
| 117 |
~~~~~~~~~~~~~ |
| 118 |
|
| 119 |
When the worker receives the :sig:`TERM` signal, it will initiate a warm shutdown. The worker will |
| 120 |
finish all currently executing tasks before it actually terminates. The first time the worker receives |
| 121 |
the :sig:`INT` (Ctrl-C) signal, it will initiate a warm shutdown as well. |
| 122 |
|
| 123 |
The warm shutdown will stop the call to :func:`WorkController.start() <celery.worker.worker.WorkController.start>` |
| 124 |
and will call :func:`WorkController.stop() <celery.worker.worker.WorkController.stop>`. |
| 125 |
|
| 126 |
- Additional :sig:`TERM` signals will be ignored during the warm shutdown process. |
| 127 |
- The next :sig:`INT` signal will trigger the next stage of the shutdown process. |
| 128 |
|
| 129 |
.. versionchanged:: 5.6 |
| 130 |
In previous versions of Celery, when the prefork pool was in use, heartbeats to the broker were not sent during |
| 131 |
warm shutdown. This caused the broker to terminate the connection, which meant that tasks were not able to complete. |
| 132 |
As of version 5.6, when the prefork pool is in use, heartbeats are now maintained during warm shutdown and tasks are |
| 133 |
able to complete before the worker terminates. |
| 134 |
|
| 135 |
.. _worker-cold-shutdown: |
| 136 |
|
| 137 |
Cold Shutdown |
| 138 |
~~~~~~~~~~~~~ |
| 139 |
|
| 140 |
Cold shutdown is initiated when the worker receives the :sig:`QUIT` signal. The worker will stop |
| 141 |
all currently executing tasks and terminate immediately. |
| 142 |
|
| 143 |
.. _worker-REMAP_SIGTERM: |
| 144 |
|
| 145 |
.. note:: |
| 146 |
|
| 147 |
If the environment variable ``REMAP_SIGTERM`` is set to ``SIGQUIT``, the worker will also initiate |
| 148 |
a cold shutdown when it receives the :sig:`TERM` signal instead of a warm shutdown. |
| 149 |
|
| 150 |
The cold shutdown will stop the call to :func:`WorkController.start() <celery.worker.worker.WorkController.start>` |
| 151 |
and will call :func:`WorkController.terminate() <celery.worker.worker.WorkController.terminate>`. |
| 152 |
|
| 153 |
If the warm shutdown already started, the transition to cold shutdown will run a signal handler ``on_cold_shutdown`` |
| 154 |
to cancel all currently executing tasks from the MainProcess and potentially trigger the :ref:`worker-soft-shutdown`. |
| 155 |
|
| 156 |
.. _worker-soft-shutdown: |
| 157 |
|
| 158 |
Soft Shutdown |
| 159 |
~~~~~~~~~~~~~ |
| 160 |
|
| 161 |
.. versionadded:: 5.5 |
| 162 |
|
| 163 |
Soft shutdown is a time limited warm shutdown, initiated just before the cold shutdown. The worker will |
| 164 |
allow :setting:`worker_soft_shutdown_timeout` seconds for all currently executing tasks to finish before |
| 165 |
it terminates. If the time limit is reached, the worker will initiate a cold shutdown and cancel all currently |
| 166 |
executing tasks. If the :sig:`QUIT` signal is received during the soft shutdown, the worker will cancel all |
| 167 |
currently executing tasks but still wait for the time limit to finish before terminating, giving a chance for |
| 168 |
the worker to perform the cold shutdown a little more gracefully. |
| 169 |
|
| 170 |
The soft shutdown is disabled by default to maintain backward compatibility with the :ref:`worker-cold-shutdown` |
| 171 |
behavior. To enable the soft shutdown, set :setting:`worker_soft_shutdown_timeout` to a positive float value. |
| 172 |
The soft shutdown will be skipped if there are no tasks running. To force the soft shutdown, *also* enable the |
| 173 |
:setting:`worker_enable_soft_shutdown_on_idle` setting. |
| 174 |
|
| 175 |
.. warning:: |
| 176 |
|
| 177 |
If the worker is not running any task but has ETA tasks reserved, the soft shutdown will not be initiated |
| 178 |
unless the :setting:`worker_enable_soft_shutdown_on_idle` setting is enabled, which may lead to task loss |
| 179 |
during the cold shutdown. When using ETA tasks, it is recommended to enable the soft shutdown on idle. |
| 180 |
Experiment which :setting:`worker_soft_shutdown_timeout` value works best for your setup to reduce the risk |
| 181 |
of task loss to a minimum. |
| 182 |
|
| 183 |
For example, when setting ``worker_soft_shutdown_timeout=3``, the worker will allow 3 seconds for all currently |
| 184 |
executing tasks to finish before it terminates. If the time limit is reached, the worker will initiate a cold shutdown |
| 185 |
and cancel all currently executing tasks. |
| 186 |
|
| 187 |
.. code-block:: console |
| 188 |
|
| 189 |
[INFO/MainProcess] Task myapp.long_running_task[6f748357-b2c7-456a-95de-f05c00504042] received |
| 190 |
[WARNING/ForkPoolWorker-8] long_running_task is running, sleeping 1/2000s |
| 191 |
[WARNING/ForkPoolWorker-8] long_running_task is running, sleeping 2/2000s |
| 192 |
[WARNING/ForkPoolWorker-8] long_running_task is running, sleeping 3/2000s |
| 193 |
^C |
| 194 |
worker: Hitting Ctrl+C again will initiate cold shutdown, terminating all running tasks! |
| 195 |
|
| 196 |
worker: Warm shutdown (MainProcess) |
| 197 |
[WARNING/ForkPoolWorker-8] long_running_task is running, sleeping 4/2000s |
| 198 |
[WARNING/ForkPoolWorker-8] long_running_task is running, sleeping 5/2000s |
| 199 |
[WARNING/ForkPoolWorker-8] long_running_task is running, sleeping 6/2000s |
| 200 |
^C |
| 201 |
worker: Hitting Ctrl+C again will terminate all running tasks! |
| 202 |
[WARNING/MainProcess] Initiating Soft Shutdown, terminating in 3 seconds |
| 203 |
[WARNING/ForkPoolWorker-8] long_running_task is running, sleeping 7/2000s |
| 204 |
[WARNING/ForkPoolWorker-8] long_running_task is running, sleeping 8/2000s |
| 205 |
[WARNING/ForkPoolWorker-8] long_running_task is running, sleeping 9/2000s |
| 206 |
[WARNING/MainProcess] Restoring 1 unacknowledged message(s) |
| 207 |
|
| 208 |
- The next :sig:`QUIT` signal will cancel the tasks that are still running in the soft shutdown, but the worker |
| 209 |
will still wait for the time limit to finish before terminating. |
| 210 |
- The next (2nd) :sig:`QUIT` or :sig:`INT` signal will trigger the next stage of the shutdown process. |
| 211 |
|
| 212 |
.. _worker-hard-shutdown: |
| 213 |
|
| 214 |
Hard Shutdown |
| 215 |
~~~~~~~~~~~~~ |
| 216 |
|
| 217 |
.. versionadded:: 5.5 |
| 218 |
|
| 219 |
Hard shutdown is mostly for local or debug purposes, allowing to spam the :sig:`INT` (Ctrl-C) signal |
| 220 |
to force the worker to terminate immediately. The worker will stop all currently executing tasks and |
| 221 |
terminate immediately by raising a :exc:`@WorkerTerminate` exception in the MainProcess. |
| 222 |
|
| 223 |
For example, notice the ``^C`` in the logs below (using the :sig:`INT` signal to move from stage to stage): |
| 224 |
|
| 225 |
.. code-block:: console |
| 226 |
|
| 227 |
[INFO/MainProcess] Task myapp.long_running_task[7235ac16-543d-4fd5-a9e1-2d2bb8ab630a] received |
| 228 |
[WARNING/ForkPoolWorker-8] long_running_task is running, sleeping 1/2000s |
| 229 |
[WARNING/ForkPoolWorker-8] long_running_task is running, sleeping 2/2000s |
| 230 |
^C |
| 231 |
worker: Hitting Ctrl+C again will initiate cold shutdown, terminating all running tasks! |
| 232 |
|
| 233 |
worker: Warm shutdown (MainProcess) |
| 234 |
[WARNING/ForkPoolWorker-8] long_running_task is running, sleeping 3/2000s |
| 235 |
[WARNING/ForkPoolWorker-8] long_running_task is running, sleeping 4/2000s |
| 236 |
^C |
| 237 |
worker: Hitting Ctrl+C again will terminate all running tasks! |
| 238 |
[WARNING/MainProcess] Initiating Soft Shutdown, terminating in 10 seconds |
| 239 |
[WARNING/ForkPoolWorker-8] long_running_task is running, sleeping 5/2000s |
| 240 |
[WARNING/ForkPoolWorker-8] long_running_task is running, sleeping 6/2000s |
| 241 |
^C |
| 242 |
Waiting gracefully for cold shutdown to complete... |
| 243 |
|
| 244 |
worker: Cold shutdown (MainProcess) |
| 245 |
^C[WARNING/MainProcess] Restoring 1 unacknowledged message(s) |
| 246 |
|
| 247 |
.. warning:: |
| 248 |
|
| 249 |
The log ``Restoring 1 unacknowledged message(s)`` is misleading as it is not guaranteed that the message |
| 250 |
will be restored after a hard shutdown. The :ref:`worker-soft-shutdown` allows adding a time window just between |
| 251 |
the warm and the cold shutdown that improves the gracefulness of the shutdown process. |
| 252 |
|
| 253 |
.. _worker-restarting: |
| 254 |
|
| 255 |
Restarting the worker |
| 256 |
===================== |
| 257 |
|
| 258 |
To restart the worker you should send the `TERM` signal and start a new |
| 259 |
instance. The easiest way to manage workers for development |
| 260 |
is by using `celery multi`: |
| 261 |
|
| 262 |
.. code-block:: console |
| 263 |
|
| 264 |
$ celery multi start 1 -A proj -l INFO -c4 --pidfile=/var/run/celery/%n.pid |
| 265 |
$ celery multi restart 1 --pidfile=/var/run/celery/%n.pid |
| 266 |
|
| 267 |
For production deployments you should be using init-scripts or a process |
| 268 |
supervision system (see :ref:`daemonizing`). |
| 269 |
|
| 270 |
Other than stopping, then starting the worker to restart, you can also |
| 271 |
restart the worker using the :sig:`HUP` signal. Note that the worker |
| 272 |
will be responsible for restarting itself so this is prone to problems and |
| 273 |
isn't recommended in production: |
| 274 |
|
| 275 |
.. code-block:: console |
| 276 |
|
| 277 |
$ kill -HUP $pid |
| 278 |
|
| 279 |
.. note:: |
| 280 |
|
| 281 |
Restarting by :sig:`HUP` only works if the worker is running |
| 282 |
in the background as a daemon (it doesn't have a controlling |
| 283 |
terminal). |
| 284 |
|
| 285 |
:sig:`HUP` is disabled on macOS because of a limitation on |
| 286 |
that platform. |
| 287 |
|
| 288 |
Automatic re-connection on connection loss to broker |
| 289 |
==================================================== |
| 290 |
|
| 291 |
.. versionadded:: 5.3 |
| 292 |
|
| 293 |
Unless :setting:`broker_connection_retry_on_startup` is set to False, |
| 294 |
Celery will automatically retry reconnecting to the broker after the first |
| 295 |
connection loss. :setting:`broker_connection_retry` controls whether to automatically |
| 296 |
retry reconnecting to the broker for subsequent reconnects. |
| 297 |
|
| 298 |
.. versionadded:: 5.1 |
| 299 |
|
| 300 |
If :setting:`worker_cancel_long_running_tasks_on_connection_loss` is set to True, |
| 301 |
Celery will also cancel any long running task that is currently running. |
| 302 |
|
| 303 |
.. versionadded:: 5.3 |
| 304 |
|
| 305 |
Since the message broker does not track how many tasks were already fetched before |
| 306 |
the connection was lost, Celery will reduce the prefetch count by the number of |
| 307 |
tasks that are currently running multiplied by :setting:`worker_prefetch_multiplier`. |
| 308 |
The prefetch count will be gradually restored to the maximum allowed after |
| 309 |
each time a task that was running before the connection was lost is complete. |
| 310 |
|
| 311 |
This feature is enabled by default, but can be disabled by setting False |
| 312 |
to :setting:`worker_enable_prefetch_count_reduction`. |
| 313 |
|
| 314 |
.. _worker-process-signals: |
| 315 |
|
| 316 |
Process Signals |
| 317 |
=============== |
| 318 |
|
| 319 |
The worker's main process overrides the following signals: |
| 320 |
|
| 321 |
+--------------+-------------------------------------------------+ |
| 322 |
| :sig:`TERM` | Warm shutdown, wait for tasks to complete. | |
| 323 |
+--------------+-------------------------------------------------+ |
| 324 |
| :sig:`QUIT` | Cold shutdown, terminate ASAP | |
| 325 |
+--------------+-------------------------------------------------+ |
| 326 |
| :sig:`USR1` | Dump traceback for all active threads. | |
| 327 |
+--------------+-------------------------------------------------+ |
| 328 |
| :sig:`USR2` | Remote debug, see :mod:`celery.contrib.rdb`. | |
| 329 |
+--------------+-------------------------------------------------+ |
| 330 |
|
| 331 |
.. _worker-files: |
| 332 |
|
| 333 |
Variables in file paths |
| 334 |
======================= |
| 335 |
|
| 336 |
The file path arguments for :option:`--logfile <celery worker --logfile>`, |
| 337 |
:option:`--pidfile <celery worker --pidfile>`, and |
| 338 |
:option:`--statedb <celery worker --statedb>` can contain variables that the |
| 339 |
worker will expand: |
| 340 |
|
| 341 |
Node name replacements |
| 342 |
---------------------- |
| 343 |
|
| 344 |
- ``%p``: Full node name. |
| 345 |
- ``%h``: Hostname, including domain name. |
| 346 |
- ``%n``: Hostname only. |
| 347 |
- ``%d``: Domain name only. |
| 348 |
- ``%i``: Prefork pool process index or 0 if MainProcess. |
| 349 |
- ``%I``: Prefork pool process index with separator. |
| 350 |
|
| 351 |
For example, if the current hostname is ``[email protected]`` then |
| 352 |
these will expand to: |
| 353 |
|
| 354 |
- ``--logfile=%p.log`` -> :file:`[email protected]` |
| 355 |
- ``--logfile=%h.log`` -> :file:`foo.example.com.log` |
| 356 |
- ``--logfile=%n.log`` -> :file:`george.log` |
| 357 |
- ``--logfile=%d.log`` -> :file:`example.com.log` |
| 358 |
|
| 359 |
.. _worker-files-process-index: |
| 360 |
|
| 361 |
Prefork pool process index |
| 362 |
-------------------------- |
| 363 |
|
| 364 |
The prefork pool process index specifiers will expand into a different |
| 365 |
filename depending on the process that'll eventually need to open the file. |
| 366 |
|
| 367 |
This can be used to specify one log file per child process. |
| 368 |
|
| 369 |
Note that the numbers will stay within the process limit even if processes |
| 370 |
exit or if autoscale/``maxtasksperchild``/time limits are used. That is, the number |
| 371 |
is the *process index* not the process count or pid. |
| 372 |
|
| 373 |
* ``%i`` - Pool process index or 0 if MainProcess. |
| 374 |
|
| 375 |
Where ``-n [email protected] -c2 -f %n-%i.log`` will result in |
| 376 |
three log files: |
| 377 |
|
| 378 |
- :file:`worker1-0.log` (main process) |
| 379 |
- :file:`worker1-1.log` (pool process 1) |
| 380 |
- :file:`worker1-2.log` (pool process 2) |
| 381 |
|
| 382 |
* ``%I`` - Pool process index with separator. |
| 383 |
|
| 384 |
Where ``-n [email protected] -c2 -f %n%I.log`` will result in |
| 385 |
three log files: |
| 386 |
|
| 387 |
- :file:`worker1.log` (main process) |
| 388 |
- :file:`worker1-1.log` (pool process 1) |
| 389 |
- :file:`worker1-2.log` (pool process 2) |
| 390 |
|
| 391 |
.. _worker-concurrency: |
| 392 |
|
| 393 |
Concurrency |
| 394 |
=========== |
| 395 |
|
| 396 |
By default multiprocessing is used to perform concurrent execution of tasks, |
| 397 |
but you can also use :ref:`Eventlet <concurrency-eventlet>`. The number |
| 398 |
of worker processes/threads can be changed using the |
| 399 |
:option:`--concurrency <celery worker --concurrency>` argument and defaults |
| 400 |
to the number of CPUs available on the machine. |
| 401 |
|
| 402 |
.. admonition:: Number of processes (multiprocessing/prefork pool) |
| 403 |
|
| 404 |
More pool processes are usually better, but there's a cut-off point where |
| 405 |
adding more pool processes affects performance in negative ways. |
| 406 |
There's even some evidence to support that having multiple worker |
| 407 |
instances running, may perform better than having a single worker. |
| 408 |
For example 3 workers with 10 pool processes each. You need to experiment |
| 409 |
to find the numbers that works best for you, as this varies based on |
| 410 |
application, work load, task run times and other factors. |
| 411 |
|
| 412 |
.. _worker-remote-control: |
| 413 |
|
| 414 |
Remote control |
| 415 |
============== |
| 416 |
|
| 417 |
.. versionadded:: 2.0 |
| 418 |
|
| 419 |
.. sidebar:: The ``celery`` command |
| 420 |
|
| 421 |
The :program:`celery` program is used to execute remote control |
| 422 |
commands from the command-line. It supports all of the commands |
| 423 |
listed below. See :ref:`monitoring-control` for more information. |
| 424 |
|
| 425 |
:pool support: *prefork, eventlet, gevent, thread*, blocking:*solo* (see note) |
| 426 |
:broker support: *amqp, redis* |
| 427 |
|
| 428 |
Workers have the ability to be remote controlled using a high-priority |
| 429 |
broadcast message queue. The commands can be directed to all, or a specific |
| 430 |
list of workers. |
| 431 |
|
| 432 |
Commands can also have replies. The client can then wait for and collect |
| 433 |
those replies. Since there's no central authority to know how many |
| 434 |
workers are available in the cluster, there's also no way to estimate |
| 435 |
how many workers may send a reply, so the client has a configurable |
| 436 |
timeout — the deadline in seconds for replies to arrive in. This timeout |
| 437 |
defaults to one second. If the worker doesn't reply within the deadline |
| 438 |
it doesn't necessarily mean the worker didn't reply, or worse is dead, but |
| 439 |
may simply be caused by network latency or the worker being slow at processing |
| 440 |
commands, so adjust the timeout accordingly. |
| 441 |
|
| 442 |
In addition to timeouts, the client can specify the maximum number |
| 443 |
of replies to wait for. If a destination is specified, this limit is set |
| 444 |
to the number of destination hosts. |
| 445 |
|
| 446 |
.. note:: |
| 447 |
|
| 448 |
The ``solo`` pool supports remote control commands, |
| 449 |
but any task executing will block any waiting control command, |
| 450 |
so it is of limited use if the worker is very busy. In that |
| 451 |
case you must increase the timeout waiting for replies in the client. |
| 452 |
|
| 453 |
.. _worker-broadcast-fun: |
| 454 |
|
| 455 |
The :meth:`[email protected]` function |
| 456 |
---------------------------------------------------- |
| 457 |
|
| 458 |
This is the client function used to send commands to the workers. |
| 459 |
Some remote control commands also have higher-level interfaces using |
| 460 |
:meth:`[email protected]` in the background, like |
| 461 |
:meth:`[email protected]_limit`, and :meth:`[email protected]`. |
| 462 |
|
| 463 |
Sending the :control:`rate_limit` command and keyword arguments: |
| 464 |
|
| 465 |
.. code-block:: pycon |
| 466 |
|
| 467 |
>>> app.control.broadcast('rate_limit', |
| 468 |
... arguments={'task_name': 'myapp.mytask', |
| 469 |
... 'rate_limit': '200/m'}) |
| 470 |
|
| 471 |
This will send the command asynchronously, without waiting for a reply. |
| 472 |
To request a reply you have to use the `reply` argument: |
| 473 |
|
| 474 |
.. code-block:: pycon |
| 475 |
|
| 476 |
>>> app.control.broadcast('rate_limit', { |
| 477 |
... 'task_name': 'myapp.mytask', 'rate_limit': '200/m'}, reply=True) |
| 478 |
[{'worker1.example.com': 'New rate limit set successfully'}, |
| 479 |
{'worker2.example.com': 'New rate limit set successfully'}, |
| 480 |
{'worker3.example.com': 'New rate limit set successfully'}] |
| 481 |
|
| 482 |
Using the `destination` argument you can specify a list of workers |
| 483 |
to receive the command: |
| 484 |
|
| 485 |
.. code-block:: pycon |
| 486 |
|
| 487 |
>>> app.control.broadcast('rate_limit', { |
| 488 |
... 'task_name': 'myapp.mytask', |
| 489 |
... 'rate_limit': '200/m'}, reply=True, |
| 490 |
... destination=['[email protected]']) |
| 491 |
[{'worker1.example.com': 'New rate limit set successfully'}] |
| 492 |
|
| 493 |
|
| 494 |
Of course, using the higher-level interface to set rate limits is much |
| 495 |
more convenient, but there are commands that can only be requested |
| 496 |
using :meth:`[email protected]`. |
| 497 |
|
| 498 |
Commands |
| 499 |
======== |
| 500 |
|
| 501 |
.. control:: revoke |
| 502 |
|
| 503 |
``revoke``: Revoking tasks |
| 504 |
-------------------------- |
| 505 |
:pool support: all, terminate only supported by prefork, eventlet and gevent |
| 506 |
:broker support: *amqp, redis* |
| 507 |
:command: :program:`celery -A proj control revoke <task_id>` |
| 508 |
|
| 509 |
All worker nodes keeps a memory of revoked task ids, either in-memory or |
| 510 |
persistent on disk (see :ref:`worker-persistent-revokes`). |
| 511 |
|
| 512 |
.. note:: |
| 513 |
|
| 514 |
The maximum number of revoked tasks to keep in memory can be |
| 515 |
specified using the ``CELERY_WORKER_REVOKES_MAX`` environment |
| 516 |
variable, which defaults to 50000. When the limit has been exceeded, |
| 517 |
the revokes will be active for 10800 seconds (3 hours) before being |
| 518 |
expired. This value can be changed using the |
| 519 |
``CELERY_WORKER_REVOKE_EXPIRES`` environment variable. |
| 520 |
|
| 521 |
Memory limits can also be set for successful tasks through the |
| 522 |
``CELERY_WORKER_SUCCESSFUL_MAX`` and |
| 523 |
``CELERY_WORKER_SUCCESSFUL_EXPIRES`` environment variables, and |
| 524 |
default to 1000 and 10800 respectively. |
| 525 |
|
| 526 |
When a worker receives a revoke request it will skip executing |
| 527 |
the task, but it won't terminate an already executing task unless |
| 528 |
the `terminate` option is set. |
| 529 |
|
| 530 |
.. note:: |
| 531 |
|
| 532 |
The terminate option is a last resort for administrators when |
| 533 |
a task is stuck. It's not for terminating the task, |
| 534 |
it's for terminating the process that's executing the task, and that |
| 535 |
process may have already started processing another task at the point |
| 536 |
when the signal is sent, so for this reason you must never call this |
| 537 |
programmatically. |
| 538 |
|
| 539 |
If `terminate` is set the worker child process processing the task |
| 540 |
will be terminated. The default signal sent is `TERM`, but you can |
| 541 |
specify this using the `signal` argument. Signal can be the uppercase name |
| 542 |
of any signal defined in the :mod:`signal` module in the Python Standard |
| 543 |
Library. |
| 544 |
|
| 545 |
Terminating a task also revokes it. |
| 546 |
|
| 547 |
.. versionchanged:: 5.6 |
| 548 |
|
| 549 |
When a task is revoked, the result backend is now immediately updated |
| 550 |
to reflect the ``REVOKED`` status. Previously, the backend was only |
| 551 |
updated when a worker attempted to process the revoked task, which |
| 552 |
could leave tasks with ETA/countdown in ``PENDING`` status indefinitely |
| 553 |
if the worker was shut down before the scheduled time. |
| 554 |
|
| 555 |
**Example** |
| 556 |
|
| 557 |
.. code-block:: pycon |
| 558 |
|
| 559 |
>>> result.revoke() |
| 560 |
|
| 561 |
>>> AsyncResult(id).revoke() |
| 562 |
|
| 563 |
>>> app.control.revoke('d9078da5-9915-40a0-bfa1-392c7bde42ed') |
| 564 |
|
| 565 |
>>> app.control.revoke('d9078da5-9915-40a0-bfa1-392c7bde42ed', |
| 566 |
... terminate=True) |
| 567 |
|
| 568 |
>>> app.control.revoke('d9078da5-9915-40a0-bfa1-392c7bde42ed', |
| 569 |
... terminate=True, signal='SIGKILL') |
| 570 |
|
| 571 |
|
| 572 |
|
| 573 |
|
| 574 |
Revoking multiple tasks |
| 575 |
----------------------- |
| 576 |
|
| 577 |
.. versionadded:: 3.1 |
| 578 |
|
| 579 |
|
| 580 |
The revoke method also accepts a list argument, where it will revoke |
| 581 |
several tasks at once. |
| 582 |
|
| 583 |
**Example** |
| 584 |
|
| 585 |
.. code-block:: pycon |
| 586 |
|
| 587 |
>>> app.control.revoke([ |
| 588 |
... '7993b0aa-1f0b-4780-9af0-c47c0858b3f2', |
| 589 |
... 'f565793e-b041-4b2b-9ca4-dca22762a55d', |
| 590 |
... 'd9d35e03-2997-42d0-a13e-64a66b88a618', |
| 591 |
]) |
| 592 |
|
| 593 |
|
| 594 |
The ``GroupResult.revoke`` method takes advantage of this since |
| 595 |
version 3.1. |
| 596 |
|
| 597 |
.. _worker-persistent-revokes: |
| 598 |
|
| 599 |
Persistent revokes |
| 600 |
------------------ |
| 601 |
|
| 602 |
Revoking tasks works by sending a broadcast message to all the workers, |
| 603 |
the workers then keep a list of revoked tasks in memory. When a worker starts |
| 604 |
up it will synchronize revoked tasks with other workers in the cluster. |
| 605 |
|
| 606 |
The list of revoked tasks is in-memory so if all workers restart the list |
| 607 |
of revoked ids will also vanish. If you want to preserve this list between |
| 608 |
restarts you need to specify a file for these to be stored in by using the `--statedb` |
| 609 |
argument to :program:`celery worker`: |
| 610 |
|
| 611 |
.. code-block:: console |
| 612 |
|
| 613 |
$ celery -A proj worker -l INFO --statedb=/var/run/celery/worker.state |
| 614 |
|
| 615 |
or if you use :program:`celery multi` you want to create one file per |
| 616 |
worker instance so use the `%n` format to expand the current node |
| 617 |
name: |
| 618 |
|
| 619 |
.. code-block:: console |
| 620 |
|
| 621 |
celery multi start 2 -l INFO --statedb=/var/run/celery/%n.state |
| 622 |
|
| 623 |
|
| 624 |
See also :ref:`worker-files` |
| 625 |
|
| 626 |
Note that remote control commands must be working for revokes to work. |
| 627 |
Remote control commands are only supported by the RabbitMQ (amqp) and Redis |
| 628 |
at this point. |
| 629 |
|
| 630 |
.. control:: revoke_by_stamped_headers |
| 631 |
|
| 632 |
``revoke_by_stamped_headers``: Revoking tasks by their stamped headers |
| 633 |
---------------------------------------------------------------------- |
| 634 |
:pool support: all, terminate only supported by prefork and eventlet |
| 635 |
:broker support: *amqp, redis* |
| 636 |
:command: :program:`celery -A proj control revoke_by_stamped_headers <header=value>` |
| 637 |
|
| 638 |
This command is similar to :meth:`[email protected]`, but instead of |
| 639 |
specifying the task id(s), you specify the stamped header(s) as key-value pair(s), |
| 640 |
and each task that has a stamped header matching the key-value pair(s) will be revoked. |
| 641 |
|
| 642 |
.. warning:: |
| 643 |
|
| 644 |
The revoked headers mapping is not persistent across restarts, so if you |
| 645 |
restart the workers, the revoked headers will be lost and need to be |
| 646 |
mapped again. |
| 647 |
|
| 648 |
.. warning:: |
| 649 |
|
| 650 |
This command may perform poorly if your worker pool concurrency is high |
| 651 |
and terminate is enabled, since it will have to iterate over all the running |
| 652 |
tasks to find the ones with the specified stamped header. |
| 653 |
|
| 654 |
**Example** |
| 655 |
|
| 656 |
.. code-block:: pycon |
| 657 |
|
| 658 |
>>> app.control.revoke_by_stamped_headers({'header': 'value'}) |
| 659 |
|
| 660 |
>>> app.control.revoke_by_stamped_headers({'header': 'value'}, terminate=True) |
| 661 |
|
| 662 |
>>> app.control.revoke_by_stamped_headers({'header': 'value'}, terminate=True, signal='SIGKILL') |
| 663 |
|
| 664 |
|
| 665 |
Revoking multiple tasks by stamped headers |
| 666 |
------------------------------------------ |
| 667 |
|
| 668 |
.. versionadded:: 5.3 |
| 669 |
|
| 670 |
The ``revoke_by_stamped_headers`` method also accepts a list argument, where it will revoke |
| 671 |
by several headers or several values. |
| 672 |
|
| 673 |
**Example** |
| 674 |
|
| 675 |
.. code-block:: pycon |
| 676 |
|
| 677 |
>> app.control.revoke_by_stamped_headers({ |
| 678 |
... 'header_A': 'value_1', |
| 679 |
... 'header_B': ['value_2', 'value_3'], |
| 680 |
}) |
| 681 |
|
| 682 |
This will revoke all of the tasks that have a stamped header ``header_A`` with value ``value_1``, |
| 683 |
and all of the tasks that have a stamped header ``header_B`` with values ``value_2`` or ``value_3``. |
| 684 |
|
| 685 |
**CLI Example** |
| 686 |
|
| 687 |
.. code-block:: console |
| 688 |
|
| 689 |
$ celery -A proj control revoke_by_stamped_headers stamped_header_key_A=stamped_header_value_1 stamped_header_key_B=stamped_header_value_2 |
| 690 |
|
| 691 |
$ celery -A proj control revoke_by_stamped_headers stamped_header_key_A=stamped_header_value_1 stamped_header_key_B=stamped_header_value_2 --terminate |
| 692 |
|
| 693 |
$ celery -A proj control revoke_by_stamped_headers stamped_header_key_A=stamped_header_value_1 stamped_header_key_B=stamped_header_value_2 --terminate --signal=SIGKILL |
| 694 |
|
| 695 |
.. _worker-time-limits: |
| 696 |
|
| 697 |
Time Limits |
| 698 |
=========== |
| 699 |
|
| 700 |
.. versionadded:: 2.0 |
| 701 |
|
| 702 |
:pool support: *prefork/gevent (see note below)* |
| 703 |
|
| 704 |
.. sidebar:: Soft, or hard? |
| 705 |
|
| 706 |
The time limit is set in two values, `soft` and `hard`. |
| 707 |
The soft time limit allows the task to catch an exception |
| 708 |
to clean up before it is killed: the hard timeout isn't catch-able |
| 709 |
and force terminates the task. |
| 710 |
|
| 711 |
A single task can potentially run forever, if you have lots of tasks |
| 712 |
waiting for some event that'll never happen you'll block the worker |
| 713 |
from processing new tasks indefinitely. The best way to defend against |
| 714 |
this scenario happening is enabling time limits. |
| 715 |
|
| 716 |
The time limit (`--time-limit`) is the maximum number of seconds a task |
| 717 |
may run before the process executing it is terminated and replaced by a |
| 718 |
new process. You can also enable a soft time limit (`--soft-time-limit`), |
| 719 |
this raises an exception the task can catch to clean up before the hard |
| 720 |
time limit kills it: |
| 721 |
|
| 722 |
.. code-block:: python |
| 723 |
|
| 724 |
from myapp import app |
| 725 |
from celery.exceptions import SoftTimeLimitExceeded |
| 726 |
|
| 727 |
@app.task |
| 728 |
def mytask(): |
| 729 |
try: |
| 730 |
do_work() |
| 731 |
except SoftTimeLimitExceeded: |
| 732 |
clean_up_in_a_hurry() |
| 733 |
|
| 734 |
Time limits can also be set using the :setting:`task_time_limit` / |
| 735 |
:setting:`task_soft_time_limit` settings. You can also specify time |
| 736 |
limits for client side operation using ``timeout`` argument of |
| 737 |
``AsyncResult.get()`` function. |
| 738 |
|
| 739 |
.. note:: |
| 740 |
|
| 741 |
Time limits don't currently work on platforms that don't support |
| 742 |
the :sig:`SIGUSR1` signal. |
| 743 |
|
| 744 |
.. note:: |
| 745 |
|
| 746 |
The gevent pool does not implement soft time limits. Additionally, |
| 747 |
it will not enforce the hard time limit if the task is blocking. |
| 748 |
|
| 749 |
|
| 750 |
Changing time limits at run-time |
| 751 |
-------------------------------- |
| 752 |
.. versionadded:: 2.3 |
| 753 |
|
| 754 |
:broker support: *amqp, redis* |
| 755 |
|
| 756 |
There's a remote control command that enables you to change both soft |
| 757 |
and hard time limits for a task — named ``time_limit``. |
| 758 |
|
| 759 |
Example changing the time limit for the ``tasks.crawl_the_web`` task |
| 760 |
to have a soft time limit of one minute, and a hard time limit of |
| 761 |
two minutes: |
| 762 |
|
| 763 |
.. code-block:: pycon |
| 764 |
|
| 765 |
>>> app.control.time_limit('tasks.crawl_the_web', |
| 766 |
soft=60, hard=120, reply=True) |
| 767 |
[{'worker1.example.com': {'ok': 'time limits set successfully'}}] |
| 768 |
|
| 769 |
Only tasks that starts executing after the time limit change will be affected. |
| 770 |
|
| 771 |
.. _worker-rate-limits: |
| 772 |
|
| 773 |
Rate Limits |
| 774 |
=========== |
| 775 |
|
| 776 |
.. control:: rate_limit |
| 777 |
|
| 778 |
Changing rate-limits at run-time |
| 779 |
-------------------------------- |
| 780 |
|
| 781 |
Example changing the rate limit for the `myapp.mytask` task to execute |
| 782 |
at most 200 tasks of that type every minute: |
| 783 |
|
| 784 |
.. code-block:: pycon |
| 785 |
|
| 786 |
>>> app.control.rate_limit('myapp.mytask', '200/m') |
| 787 |
|
| 788 |
The above doesn't specify a destination, so the change request will affect |
| 789 |
all worker instances in the cluster. If you only want to affect a specific |
| 790 |
list of workers you can include the ``destination`` argument: |
| 791 |
|
| 792 |
.. code-block:: pycon |
| 793 |
|
| 794 |
>>> app.control.rate_limit('myapp.mytask', '200/m', |
| 795 |
... destination=['[email protected]']) |
| 796 |
|
| 797 |
.. warning:: |
| 798 |
|
| 799 |
This won't affect workers with the |
| 800 |
:setting:`worker_disable_rate_limits` setting enabled. |
| 801 |
|
| 802 |
.. _worker-max-tasks-per-child: |
| 803 |
|
| 804 |
Max tasks per child setting |
| 805 |
=========================== |
| 806 |
|
| 807 |
.. versionadded:: 2.0 |
| 808 |
|
| 809 |
:pool support: *prefork* |
| 810 |
|
| 811 |
With this option you can configure the maximum number of tasks |
| 812 |
a worker can execute before it's replaced by a new process. |
| 813 |
|
| 814 |
This is useful if you have memory leaks you have no control over |
| 815 |
for example from closed source C extensions. |
| 816 |
|
| 817 |
The option can be set using the workers |
| 818 |
:option:`--max-tasks-per-child <celery worker --max-tasks-per-child>` argument |
| 819 |
or using the :setting:`worker_max_tasks_per_child` setting. |
| 820 |
|
| 821 |
.. _worker-max-memory-per-child: |
| 822 |
|
| 823 |
Max memory per child setting |
| 824 |
============================ |
| 825 |
|
| 826 |
.. versionadded:: 4.0 |
| 827 |
|
| 828 |
:pool support: *prefork* |
| 829 |
|
| 830 |
With this option you can configure the maximum amount of resident |
| 831 |
memory a worker can execute before it's replaced by a new process. |
| 832 |
|
| 833 |
This is useful if you have memory leaks you have no control over |
| 834 |
for example from closed source C extensions. |
| 835 |
|
| 836 |
The option can be set using the workers |
| 837 |
:option:`--max-memory-per-child <celery worker --max-memory-per-child>` argument |
| 838 |
or using the :setting:`worker_max_memory_per_child` setting. |
| 839 |
|
| 840 |
.. _worker-autoscaling: |
| 841 |
|
| 842 |
Autoscaling |
| 843 |
=========== |
| 844 |
|
| 845 |
.. versionadded:: 2.2 |
| 846 |
|
| 847 |
:pool support: *prefork*, *gevent* |
| 848 |
|
| 849 |
The *autoscaler* component is used to dynamically resize the pool |
| 850 |
based on load: |
| 851 |
|
| 852 |
- The autoscaler adds more pool processes when there is work to do, |
| 853 |
- and starts removing processes when the workload is low. |
| 854 |
|
| 855 |
It's enabled by the :option:`--autoscale <celery worker --autoscale>` option, |
| 856 |
which needs two numbers: the maximum and minimum number of pool processes: |
| 857 |
|
| 858 |
.. code-block:: text |
| 859 |
|
| 860 |
--autoscale=AUTOSCALE |
| 861 |
Enable autoscaling by providing |
| 862 |
max_concurrency,min_concurrency. Example: |
| 863 |
--autoscale=10,3 (always keep 3 processes, but grow to |
| 864 |
10 if necessary). |
| 865 |
|
| 866 |
You can also define your own rules for the autoscaler by subclassing |
| 867 |
:class:`~celery.worker.autoscale.Autoscaler`. |
| 868 |
Some ideas for metrics include load average or the amount of memory available. |
| 869 |
You can specify a custom autoscaler with the :setting:`worker_autoscaler` setting. |
| 870 |
|
| 871 |
.. _worker-queues: |
| 872 |
|
| 873 |
Queues |
| 874 |
====== |
| 875 |
|
| 876 |
A worker instance can consume from any number of queues. |
| 877 |
By default it will consume from all queues defined in the |
| 878 |
:setting:`task_queues` setting (that if not specified falls back to the |
| 879 |
default queue named ``celery``). |
| 880 |
|
| 881 |
You can specify what queues to consume from at start-up, by giving a comma |
| 882 |
separated list of queues to the :option:`-Q <celery worker -Q>` option: |
| 883 |
|
| 884 |
.. code-block:: console |
| 885 |
|
| 886 |
$ celery -A proj worker -l INFO -Q foo,bar,baz |
| 887 |
|
| 888 |
If the queue name is defined in :setting:`task_queues` it will use that |
| 889 |
configuration, but if it's not defined in the list of queues Celery will |
| 890 |
automatically generate a new queue for you (depending on the |
| 891 |
:setting:`task_create_missing_queues` option). |
| 892 |
|
| 893 |
You can also tell the worker to start and stop consuming from a queue at |
| 894 |
run-time using the remote control commands :control:`add_consumer` and |
| 895 |
:control:`cancel_consumer`. |
| 896 |
|
| 897 |
.. control:: add_consumer |
| 898 |
|
| 899 |
Queues: Adding consumers |
| 900 |
------------------------ |
| 901 |
|
| 902 |
The :control:`add_consumer` control command will tell one or more workers |
| 903 |
to start consuming from a queue. This operation is idempotent. |
| 904 |
|
| 905 |
To tell all workers in the cluster to start consuming from a queue |
| 906 |
named "``foo``" you can use the :program:`celery control` program: |
| 907 |
|
| 908 |
.. code-block:: console |
| 909 |
|
| 910 |
$ celery -A proj control add_consumer foo |
| 911 |
-> worker1.local: OK |
| 912 |
started consuming from u'foo' |
| 913 |
|
| 914 |
If you want to specify a specific worker you can use the |
| 915 |
:option:`--destination <celery control --destination>` argument: |
| 916 |
|
| 917 |
.. code-block:: console |
| 918 |
|
| 919 |
$ celery -A proj control add_consumer foo -d [email protected] |
| 920 |
|
| 921 |
The same can be accomplished dynamically using the :meth:`@control.add_consumer` method: |
| 922 |
|
| 923 |
.. code-block:: pycon |
| 924 |
|
| 925 |
>>> app.control.add_consumer('foo', reply=True) |
| 926 |
[{u'worker1.local': {u'ok': u"already consuming from u'foo'"}}] |
| 927 |
|
| 928 |
>>> app.control.add_consumer('foo', reply=True, |
| 929 |
... destination=['[email protected]']) |
| 930 |
[{u'worker1.local': {u'ok': u"already consuming from u'foo'"}}] |
| 931 |
|
| 932 |
|
| 933 |
By now we've only shown examples using automatic queues, |
| 934 |
If you need more control you can also specify the exchange, routing_key and |
| 935 |
even other options: |
| 936 |
|
| 937 |
.. code-block:: pycon |
| 938 |
|
| 939 |
>>> app.control.add_consumer( |
| 940 |
... queue='baz', |
| 941 |
... exchange='ex', |
| 942 |
... exchange_type='topic', |
| 943 |
... routing_key='media.*', |
| 944 |
... options={ |
| 945 |
... 'queue_durable': False, |
| 946 |
... 'exchange_durable': False, |
| 947 |
... }, |
| 948 |
... reply=True, |
| 949 |
... destination=['[email protected]', '[email protected]']) |
| 950 |
|
| 951 |
|
| 952 |
.. control:: cancel_consumer |
| 953 |
|
| 954 |
Queues: Canceling consumers |
| 955 |
--------------------------- |
| 956 |
|
| 957 |
You can cancel a consumer by queue name using the :control:`cancel_consumer` |
| 958 |
control command. |
| 959 |
|
| 960 |
To force all workers in the cluster to cancel consuming from a queue |
| 961 |
you can use the :program:`celery control` program: |
| 962 |
|
| 963 |
.. code-block:: console |
| 964 |
|
| 965 |
$ celery -A proj control cancel_consumer foo |
| 966 |
|
| 967 |
The :option:`--destination <celery control --destination>` argument can be |
| 968 |
used to specify a worker, or a list of workers, to act on the command: |
| 969 |
|
| 970 |
.. code-block:: console |
| 971 |
|
| 972 |
$ celery -A proj control cancel_consumer foo -d [email protected] |
| 973 |
|
| 974 |
|
| 975 |
You can also cancel consumers programmatically using the |
| 976 |
:meth:`@control.cancel_consumer` method: |
| 977 |
|
| 978 |
.. code-block:: console |
| 979 |
|
| 980 |
>>> app.control.cancel_consumer('foo', reply=True) |
| 981 |
[{u'worker1.local': {u'ok': u"no longer consuming from u'foo'"}}] |
| 982 |
|
| 983 |
.. control:: active_queues |
| 984 |
|
| 985 |
Queues: List of active queues |
| 986 |
----------------------------- |
| 987 |
|
| 988 |
You can get a list of queues that a worker consumes from by using |
| 989 |
the :control:`active_queues` control command: |
| 990 |
|
| 991 |
.. code-block:: console |
| 992 |
|
| 993 |
$ celery -A proj inspect active_queues |
| 994 |
[...] |
| 995 |
|
| 996 |
Like all other remote control commands this also supports the |
| 997 |
:option:`--destination <celery inspect --destination>` argument used |
| 998 |
to specify the workers that should reply to the request: |
| 999 |
|
| 1000 |
.. code-block:: console |
| 1001 |
|
| 1002 |
$ celery -A proj inspect active_queues -d [email protected] |
| 1003 |
[...] |
| 1004 |
|
| 1005 |
|
| 1006 |
This can also be done programmatically by using the |
| 1007 |
:meth:`~celery.app.control.Inspect.active_queues` method: |
| 1008 |
|
| 1009 |
.. code-block:: pycon |
| 1010 |
|
| 1011 |
>>> app.control.inspect().active_queues() |
| 1012 |
[...] |
| 1013 |
|
| 1014 |
>>> app.control.inspect(['worker1.local']).active_queues() |
| 1015 |
[...] |
| 1016 |
|
| 1017 |
.. _worker-inspect: |
| 1018 |
|
| 1019 |
Inspecting workers |
| 1020 |
================== |
| 1021 |
|
| 1022 |
:class:`@control.inspect` lets you inspect running workers. It |
| 1023 |
uses remote control commands under the hood. |
| 1024 |
|
| 1025 |
You can also use the ``celery`` command to inspect workers, |
| 1026 |
and it supports the same commands as the :class:`@control` interface. |
| 1027 |
|
| 1028 |
.. code-block:: pycon |
| 1029 |
|
| 1030 |
>>> # Inspect all nodes. |
| 1031 |
>>> i = app.control.inspect() |
| 1032 |
|
| 1033 |
>>> # Specify multiple nodes to inspect. |
| 1034 |
>>> i = app.control.inspect(['worker1.example.com', |
| 1035 |
'worker2.example.com']) |
| 1036 |
|
| 1037 |
>>> # Specify a single node to inspect. |
| 1038 |
>>> i = app.control.inspect('worker1.example.com') |
| 1039 |
|
| 1040 |
.. _worker-inspect-registered-tasks: |
| 1041 |
|
| 1042 |
Dump of registered tasks |
| 1043 |
------------------------ |
| 1044 |
|
| 1045 |
You can get a list of tasks registered in the worker using the |
| 1046 |
:meth:`~celery.app.control.Inspect.registered`: |
| 1047 |
|
| 1048 |
.. code-block:: pycon |
| 1049 |
|
| 1050 |
>>> i.registered() |
| 1051 |
[{'worker1.example.com': ['tasks.add', |
| 1052 |
'tasks.sleeptask']}] |
| 1053 |
|
| 1054 |
.. _worker-inspect-active-tasks: |
| 1055 |
|
| 1056 |
Dump of currently executing tasks |
| 1057 |
--------------------------------- |
| 1058 |
|
| 1059 |
You can get a list of active tasks using |
| 1060 |
:meth:`~celery.app.control.Inspect.active`: |
| 1061 |
|
| 1062 |
.. code-block:: pycon |
| 1063 |
|
| 1064 |
>>> i.active() |
| 1065 |
[{'worker1.example.com': |
| 1066 |
[{'name': 'tasks.sleeptask', |
| 1067 |
'id': '32666e9b-809c-41fa-8e93-5ae0c80afbbf', |
| 1068 |
'args': '(8,)', |
| 1069 |
'kwargs': '{}'}]}] |
| 1070 |
|
| 1071 |
.. _worker-inspect-eta-schedule: |
| 1072 |
|
| 1073 |
Dump of scheduled (ETA) tasks |
| 1074 |
----------------------------- |
| 1075 |
|
| 1076 |
You can get a list of tasks waiting to be scheduled by using |
| 1077 |
:meth:`~celery.app.control.Inspect.scheduled`: |
| 1078 |
|
| 1079 |
.. code-block:: pycon |
| 1080 |
|
| 1081 |
>>> i.scheduled() |
| 1082 |
[{'worker1.example.com': |
| 1083 |
[{'eta': '2010-06-07 09:07:52', 'priority': 0, |
| 1084 |
'request': { |
| 1085 |
'name': 'tasks.sleeptask', |
| 1086 |
'id': '1a7980ea-8b19-413e-91d2-0b74f3844c4d', |
| 1087 |
'args': '[1]', |
| 1088 |
'kwargs': '{}'}}, |
| 1089 |
{'eta': '2010-06-07 09:07:53', 'priority': 0, |
| 1090 |
'request': { |
| 1091 |
'name': 'tasks.sleeptask', |
| 1092 |
'id': '49661b9a-aa22-4120-94b7-9ee8031d219d', |
| 1093 |
'args': '[2]', |
| 1094 |
'kwargs': '{}'}}]}] |
| 1095 |
|
| 1096 |
.. note:: |
| 1097 |
|
| 1098 |
These are tasks with an ETA/countdown argument, not periodic tasks. |
| 1099 |
|
| 1100 |
.. _worker-inspect-reserved: |
| 1101 |
|
| 1102 |
Dump of reserved tasks |
| 1103 |
---------------------- |
| 1104 |
|
| 1105 |
Reserved tasks are tasks that have been received, but are still waiting to be |
| 1106 |
executed. |
| 1107 |
|
| 1108 |
You can get a list of these using |
| 1109 |
:meth:`~celery.app.control.Inspect.reserved`: |
| 1110 |
|
| 1111 |
.. code-block:: pycon |
| 1112 |
|
| 1113 |
>>> i.reserved() |
| 1114 |
[{'worker1.example.com': |
| 1115 |
[{'name': 'tasks.sleeptask', |
| 1116 |
'id': '32666e9b-809c-41fa-8e93-5ae0c80afbbf', |
| 1117 |
'args': '(8,)', |
| 1118 |
'kwargs': '{}'}]}] |
| 1119 |
|
| 1120 |
|
| 1121 |
.. _worker-statistics: |
| 1122 |
|
| 1123 |
Statistics |
| 1124 |
---------- |
| 1125 |
|
| 1126 |
The remote control command ``inspect stats`` (or |
| 1127 |
:meth:`~celery.app.control.Inspect.stats`) will give you a long list of useful (or not |
| 1128 |
so useful) statistics about the worker: |
| 1129 |
|
| 1130 |
.. code-block:: console |
| 1131 |
|
| 1132 |
$ celery -A proj inspect stats |
| 1133 |
|
| 1134 |
For the output details, consult the reference documentation of :meth:`~celery.app.control.Inspect.stats`. |
| 1135 |
|
| 1136 |
Additional Commands |
| 1137 |
=================== |
| 1138 |
|
| 1139 |
.. control:: shutdown |
| 1140 |
|
| 1141 |
Remote shutdown |
| 1142 |
--------------- |
| 1143 |
|
| 1144 |
This command will gracefully shut down the worker remotely: |
| 1145 |
|
| 1146 |
.. code-block:: pycon |
| 1147 |
|
| 1148 |
>>> app.control.broadcast('shutdown') # shutdown all workers |
| 1149 |
>>> app.control.broadcast('shutdown', destination='[email protected]') |
| 1150 |
|
| 1151 |
.. control:: ping |
| 1152 |
|
| 1153 |
Ping |
| 1154 |
---- |
| 1155 |
|
| 1156 |
This command requests a ping from alive workers. |
| 1157 |
The workers reply with the string 'pong', and that's just about it. |
| 1158 |
It will use the default one second timeout for replies unless you specify |
| 1159 |
a custom timeout: |
| 1160 |
|
| 1161 |
.. code-block:: pycon |
| 1162 |
|
| 1163 |
>>> app.control.ping(timeout=0.5) |
| 1164 |
[{'worker1.example.com': 'pong'}, |
| 1165 |
{'worker2.example.com': 'pong'}, |
| 1166 |
{'worker3.example.com': 'pong'}] |
| 1167 |
|
| 1168 |
:meth:`[email protected]` also supports the `destination` argument, |
| 1169 |
so you can specify the workers to ping: |
| 1170 |
|
| 1171 |
.. code-block:: pycon |
| 1172 |
|
| 1173 |
>>> ping(['worker2.example.com', 'worker3.example.com']) |
| 1174 |
[{'worker2.example.com': 'pong'}, |
| 1175 |
{'worker3.example.com': 'pong'}] |
| 1176 |
|
| 1177 |
.. _worker-enable-events: |
| 1178 |
|
| 1179 |
.. control:: enable_events |
| 1180 |
.. control:: disable_events |
| 1181 |
|
| 1182 |
Enable/disable events |
| 1183 |
--------------------- |
| 1184 |
|
| 1185 |
You can enable/disable events by using the `enable_events`, |
| 1186 |
`disable_events` commands. This is useful to temporarily monitor |
| 1187 |
a worker using :program:`celery events`/:program:`celerymon`. |
| 1188 |
|
| 1189 |
.. code-block:: pycon |
| 1190 |
|
| 1191 |
>>> app.control.enable_events() |
| 1192 |
>>> app.control.disable_events() |
| 1193 |
|
| 1194 |
.. _worker-custom-control-commands: |
| 1195 |
|
| 1196 |
Writing your own remote control commands |
| 1197 |
======================================== |
| 1198 |
|
| 1199 |
There are two types of remote control commands: |
| 1200 |
|
| 1201 |
- Inspect command |
| 1202 |
|
| 1203 |
Does not have side effects, will usually just return some value |
| 1204 |
found in the worker, like the list of currently registered tasks, |
| 1205 |
the list of active tasks, etc. |
| 1206 |
|
| 1207 |
- Control command |
| 1208 |
|
| 1209 |
Performs side effects, like adding a new queue to consume from. |
| 1210 |
|
| 1211 |
Remote control commands are registered in the control panel and |
| 1212 |
they take a single argument: the current |
| 1213 |
:class:`!celery.worker.control.ControlDispatch` instance. |
| 1214 |
From there you have access to the active |
| 1215 |
:class:`~celery.worker.consumer.Consumer` if needed. |
| 1216 |
|
| 1217 |
Here's an example control command that increments the task prefetch count: |
| 1218 |
|
| 1219 |
.. code-block:: python |
| 1220 |
|
| 1221 |
from celery.worker.control import control_command |
| 1222 |
|
| 1223 |
@control_command( |
| 1224 |
args=[('n', int)], |
| 1225 |
signature='[N=1]', # <- used for help on the command-line. |
| 1226 |
) |
| 1227 |
def increase_prefetch_count(state, n=1): |
| 1228 |
state.consumer.qos.increment_eventually(n) |
| 1229 |
return {'ok': 'prefetch count incremented'} |
| 1230 |
|
| 1231 |
Make sure you add this code to a module that is imported by the worker: |
| 1232 |
this could be the same module as where your Celery app is defined, or you |
| 1233 |
can add the module to the :setting:`imports` setting. |
| 1234 |
|
| 1235 |
Restart the worker so that the control command is registered, and now you |
| 1236 |
can call your command using the :program:`celery control` utility: |
| 1237 |
|
| 1238 |
.. code-block:: console |
| 1239 |
|
| 1240 |
$ celery -A proj control increase_prefetch_count 3 |
| 1241 |
|
| 1242 |
You can also add actions to the :program:`celery inspect` program, |
| 1243 |
for example one that reads the current prefetch count: |
| 1244 |
|
| 1245 |
.. code-block:: python |
| 1246 |
|
| 1247 |
from celery.worker.control import inspect_command |
| 1248 |
|
| 1249 |
@inspect_command() |
| 1250 |
def current_prefetch_count(state): |
| 1251 |
return {'prefetch_count': state.consumer.qos.value} |
| 1252 |
|
| 1253 |
|
| 1254 |
After restarting the worker you can now query this value using the |
| 1255 |
:program:`celery inspect` program: |
| 1256 |
|
| 1257 |
.. code-block:: console |
| 1258 |
|
| 1259 |
$ celery -A proj inspect current_prefetch_count |