workers.rst 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841
  1. .. _guide-workers:
  2. ===============
  3. Workers Guide
  4. ===============
  5. .. contents::
  6. :local:
  7. :depth: 1
  8. .. _worker-starting:
  9. Starting the worker
  10. ===================
  11. .. sidebar:: Daemonizing
  12. You probably want to use a daemonization tool to start
  13. in the background. See :ref:`daemonizing` for help
  14. detaching the worker using popular daemonization tools.
  15. You can start the worker in the foreground by executing the command:
  16. .. code-block:: bash
  17. $ celery worker --app=app -l info
  18. For a full list of available command-line options see
  19. :mod:`~celery.bin.worker`, or simply do:
  20. .. code-block:: bash
  21. $ celery worker --help
  22. You can also start multiple workers on the same machine. If you do so
  23. be sure to give a unique name to each individual worker by specifying a
  24. host name with the :option:`--hostname|-n` argument:
  25. .. code-block:: bash
  26. $ celery worker --loglevel=INFO --concurrency=10 -n worker1.%h
  27. $ celery worker --loglevel=INFO --concurrency=10 -n worker2.%h
  28. $ celery worker --loglevel=INFO --concurrency=10 -n worker3.%h
  29. The hostname argument can expand the following variables:
  30. - ``%h``: Hostname including domain name.
  31. - ``%n``: Hostname only.
  32. - ``%d``: Domain name only.
  33. E.g. if the current hostname is ``george.example.com`` then
  34. these will expand to:
  35. - ``worker1.%h`` -> ``worker1.george.example.com``
  36. - ``worker1.%n`` -> ``worker1.george``
  37. - ``worker1.%d`` -> ``worker1.example.com``
  38. .. _worker-stopping:
  39. Stopping the worker
  40. ===================
  41. Shutdown should be accomplished using the :sig:`TERM` signal.
  42. When shutdown is initiated the worker will finish all currently executing
  43. tasks before it actually terminates, so if these tasks are important you should
  44. wait for it to finish before doing anything drastic (like sending the :sig:`KILL`
  45. signal).
  46. If the worker won't shutdown after considerate time, for example because
  47. of tasks stuck in an infinite-loop, you can use the :sig:`KILL` signal to
  48. force terminate the worker, but be aware that currently executing tasks will
  49. be lost (unless the tasks have the :attr:`~@Task.acks_late`
  50. option set).
  51. Also as processes can't override the :sig:`KILL` signal, the worker will
  52. not be able to reap its children, so make sure to do so manually. This
  53. command usually does the trick:
  54. .. code-block:: bash
  55. $ ps auxww | grep 'celery worker' | awk '{print $2}' | xargs kill -9
  56. .. _worker-restarting:
  57. Restarting the worker
  58. =====================
  59. Other than stopping then starting the worker to restart, you can also
  60. restart the worker using the :sig:`HUP` signal:
  61. .. code-block:: bash
  62. $ kill -HUP $pid
  63. The worker will then replace itself with a new instance using the same
  64. arguments as it was started with.
  65. .. note::
  66. Restarting by :sig:`HUP` only works if the worker is running
  67. in the background as a daemon (it does not have a controlling
  68. terminal).
  69. :sig:`HUP` is disabled on OS X because of a limitation on
  70. that platform.
  71. .. _worker-process-signals:
  72. Process Signals
  73. ===============
  74. The worker's main process overrides the following signals:
  75. +--------------+-------------------------------------------------+
  76. | :sig:`TERM` | Warm shutdown, wait for tasks to complete. |
  77. +--------------+-------------------------------------------------+
  78. | :sig:`QUIT` | Cold shutdown, terminate ASAP |
  79. +--------------+-------------------------------------------------+
  80. | :sig:`USR1` | Dump traceback for all active threads. |
  81. +--------------+-------------------------------------------------+
  82. | :sig:`USR2` | Remote debug, see :mod:`celery.contrib.rdb`. |
  83. +--------------+-------------------------------------------------+
  84. .. _worker-concurrency:
  85. Concurrency
  86. ===========
  87. By default multiprocessing is used to perform concurrent execution of tasks,
  88. but you can also use :ref:`Eventlet <concurrency-eventlet>`. The number
  89. of worker processes/threads can be changed using the :option:`--concurrency`
  90. argument and defaults to the number of CPUs available on the machine.
  91. .. admonition:: Number of processes (multiprocessing)
  92. More pool processes are usually better, but there's a cut-off point where
  93. adding more pool processes affects performance in negative ways.
  94. There is even some evidence to support that having multiple worker
  95. instances running, may perform better than having a single worker.
  96. For example 3 workers with 10 pool processes each. You need to experiment
  97. to find the numbers that works best for you, as this varies based on
  98. application, work load, task run times and other factors.
  99. .. _worker-remote-control:
  100. Remote control
  101. ==============
  102. .. versionadded:: 2.0
  103. .. sidebar:: The ``celery`` command
  104. The :program:`celery` program is used to execute remote control
  105. commands from the command-line. It supports all of the commands
  106. listed below. See :ref:`monitoring-control` for more information.
  107. pool support: *processes, eventlet, gevent*, blocking:*threads/solo* (see note)
  108. broker support: *amqp, redis, mongodb*
  109. Workers have the ability to be remote controlled using a high-priority
  110. broadcast message queue. The commands can be directed to all, or a specific
  111. list of workers.
  112. Commands can also have replies. The client can then wait for and collect
  113. those replies. Since there's no central authority to know how many
  114. workers are available in the cluster, there is also no way to estimate
  115. how many workers may send a reply, so the client has a configurable
  116. timeout — the deadline in seconds for replies to arrive in. This timeout
  117. defaults to one second. If the worker doesn't reply within the deadline
  118. it doesn't necessarily mean the worker didn't reply, or worse is dead, but
  119. may simply be caused by network latency or the worker being slow at processing
  120. commands, so adjust the timeout accordingly.
  121. In addition to timeouts, the client can specify the maximum number
  122. of replies to wait for. If a destination is specified, this limit is set
  123. to the number of destination hosts.
  124. .. note::
  125. The solo and threads pool supports remote control commands,
  126. but any task executing will block any waiting control command,
  127. so it is of limited use if the worker is very busy. In that
  128. case you must increase the timeout waiting for replies in the client.
  129. .. _worker-broadcast-fun:
  130. The :meth:`~@control.broadcast` function.
  131. ----------------------------------------------------
  132. This is the client function used to send commands to the workers.
  133. Some remote control commands also have higher-level interfaces using
  134. :meth:`~@control.broadcast` in the background, like
  135. :meth:`~@control.rate_limit` and :meth:`~@control.ping`.
  136. Sending the :control:`rate_limit` command and keyword arguments::
  137. >>> celery.control.broadcast('rate_limit',
  138. ... arguments={'task_name': 'myapp.mytask',
  139. ... 'rate_limit': '200/m'})
  140. This will send the command asynchronously, without waiting for a reply.
  141. To request a reply you have to use the `reply` argument::
  142. >>> celery.control.broadcast('rate_limit', {
  143. ... 'task_name': 'myapp.mytask', 'rate_limit': '200/m'}, reply=True)
  144. [{'worker1.example.com': 'New rate limit set successfully'},
  145. {'worker2.example.com': 'New rate limit set successfully'},
  146. {'worker3.example.com': 'New rate limit set successfully'}]
  147. Using the `destination` argument you can specify a list of workers
  148. to receive the command::
  149. >>> celery.control.broadcast('rate_limit', {
  150. ... 'task_name': 'myapp.mytask',
  151. ... 'rate_limit': '200/m'}, reply=True,
  152. ... destination=['worker1.example.com'])
  153. [{'worker1.example.com': 'New rate limit set successfully'}]
  154. Of course, using the higher-level interface to set rate limits is much
  155. more convenient, but there are commands that can only be requested
  156. using :meth:`~@control.broadcast`.
  157. .. control:: revoke
  158. Revoking tasks
  159. ==============
  160. pool support: all
  161. broker support: *amqp, redis, mongodb*
  162. All worker nodes keeps a memory of revoked task ids, either in-memory or
  163. persistent on disk (see :ref:`worker-persistent-revokes`).
  164. When a worker receives a revoke request it will skip executing
  165. the task, but it won't terminate an already executing task unless
  166. the `terminate` option is set.
  167. If `terminate` is set the worker child process processing the task
  168. will be terminated. The default signal sent is `TERM`, but you can
  169. specify this using the `signal` argument. Signal can be the uppercase name
  170. of any signal defined in the :mod:`signal` module in the Python Standard
  171. Library.
  172. Terminating a task also revokes it.
  173. **Example**
  174. ::
  175. >>> celery.control.revoke('d9078da5-9915-40a0-bfa1-392c7bde42ed')
  176. >>> celery.control.revoke('d9078da5-9915-40a0-bfa1-392c7bde42ed',
  177. ... terminate=True)
  178. >>> celery.control.revoke('d9078da5-9915-40a0-bfa1-392c7bde42ed',
  179. ... terminate=True, signal='SIGKILL')
  180. .. _worker-persistent-revokes:
  181. Persistent revokes
  182. ------------------
  183. Revoking tasks works by sending a broadcast message to all the workers,
  184. the workers then keep a list of revoked tasks in memory.
  185. If you want tasks to remain revoked after worker restart you need to
  186. specify a file for these to be stored in, either by using the `--statedb`
  187. argument to :program:`celery worker` or the :setting:`CELERYD_STATE_DB`
  188. setting.
  189. Note that remote control commands must be working for revokes to work.
  190. Remote control commands are only supported by the RabbitMQ (amqp), Redis and MongDB
  191. transports at this point.
  192. .. _worker-time-limits:
  193. Time Limits
  194. ===========
  195. .. versionadded:: 2.0
  196. pool support: *processes*
  197. .. sidebar:: Soft, or hard?
  198. The time limit is set in two values, `soft` and `hard`.
  199. The soft time limit allows the task to catch an exception
  200. to clean up before it is killed: the hard timeout is not catchable
  201. and force terminates the task.
  202. A single task can potentially run forever, if you have lots of tasks
  203. waiting for some event that will never happen you will block the worker
  204. from processing new tasks indefinitely. The best way to defend against
  205. this scenario happening is enabling time limits.
  206. The time limit (`--time-limit`) is the maximum number of seconds a task
  207. may run before the process executing it is terminated and replaced by a
  208. new process. You can also enable a soft time limit (`--soft-time-limit`),
  209. this raises an exception the task can catch to clean up before the hard
  210. time limit kills it:
  211. .. code-block:: python
  212. from myapp import celery
  213. from celery.exceptions import SoftTimeLimitExceeded
  214. @celery.task
  215. def mytask():
  216. try:
  217. do_work()
  218. except SoftTimeLimitExceeded:
  219. clean_up_in_a_hurry()
  220. Time limits can also be set using the :setting:`CELERYD_TASK_TIME_LIMIT` /
  221. :setting:`CELERYD_TASK_SOFT_TIME_LIMIT` settings.
  222. .. note::
  223. Time limits do not currently work on Windows and other
  224. platforms that do not support the ``SIGUSR1`` signal.
  225. Changing time limits at runtime
  226. -------------------------------
  227. .. versionadded:: 2.3
  228. broker support: *amqp, redis, mongodb*
  229. There is a remote control command that enables you to change both soft
  230. and hard time limits for a task — named ``time_limit``.
  231. Example changing the time limit for the ``tasks.crawl_the_web`` task
  232. to have a soft time limit of one minute, and a hard time limit of
  233. two minutes::
  234. >>> celery.control.time_limit('tasks.crawl_the_web',
  235. soft=60, hard=120, reply=True)
  236. [{'worker1.example.com': {'ok': 'time limits set successfully'}}]
  237. Only tasks that starts executing after the time limit change will be affected.
  238. .. _worker-rate-limits:
  239. Rate Limits
  240. ===========
  241. .. control:: rate_limit
  242. Changing rate-limits at runtime
  243. -------------------------------
  244. Example changing the rate limit for the `myapp.mytask` task to accept
  245. 200 tasks a minute on all servers::
  246. >>> celery.control.rate_limit('myapp.mytask', '200/m')
  247. Example changing the rate limit on a single host by specifying the
  248. destination host name::
  249. >>> celery.control.rate_limit('myapp.mytask', '200/m',
  250. ... destination=['worker1.example.com'])
  251. .. warning::
  252. This won't affect workers with the
  253. :setting:`CELERY_DISABLE_RATE_LIMITS` setting enabled.
  254. .. _worker-maxtasksperchild:
  255. Max tasks per child setting
  256. ===========================
  257. .. versionadded:: 2.0
  258. pool support: *processes*
  259. With this option you can configure the maximum number of tasks
  260. a worker can execute before it's replaced by a new process.
  261. This is useful if you have memory leaks you have no control over
  262. for example from closed source C extensions.
  263. The option can be set using the workers `--maxtasksperchild` argument
  264. or using the :setting:`CELERYD_MAX_TASKS_PER_CHILD` setting.
  265. .. _worker-autoscaling:
  266. Autoscaling
  267. ===========
  268. .. versionadded:: 2.2
  269. pool support: *processes*, *gevent*
  270. The *autoscaler* component is used to dynamically resize the pool
  271. based on load:
  272. - The autoscaler adds more pool processes when there is work to do,
  273. - and starts removing processes when the workload is low.
  274. It's enabled by the :option:`--autoscale` option, which needs two
  275. numbers: the maximum and minimum number of pool processes::
  276. --autoscale=AUTOSCALE
  277. Enable autoscaling by providing
  278. max_concurrency,min_concurrency. Example:
  279. --autoscale=10,3 (always keep 3 processes, but grow to
  280. 10 if necessary).
  281. You can also define your own rules for the autoscaler by subclassing
  282. :class:`~celery.worker.autoscaler.Autoscaler`.
  283. Some ideas for metrics include load average or the amount of memory available.
  284. You can specify a custom autoscaler with the :setting:`CELERYD_AUTOSCALER` setting.
  285. .. _worker-queues:
  286. Queues
  287. ======
  288. A worker instance can consume from any number of queues.
  289. By default it will consume from all queues defined in the
  290. :setting:`CELERY_QUEUES` setting (which if not specified defaults to the
  291. queue named ``celery``).
  292. You can specify what queues to consume from at startup,
  293. by giving a comma separated list of queues to the :option:`-Q` option:
  294. .. code-block:: bash
  295. $ celery worker -l info -Q foo,bar,baz
  296. If the queue name is defined in :setting:`CELERY_QUEUES` it will use that
  297. configuration, but if it's not defined in the list of queues Celery will
  298. automatically generate a new queue for you (depending on the
  299. :setting:`CELERY_CREATE_MISSING_QUEUES` option).
  300. You can also tell the worker to start and stop consuming from a queue at
  301. runtime using the remote control commands :control:`add_consumer` and
  302. :control:`cancel_consumer`.
  303. .. control:: add_consumer
  304. Queues: Adding consumers
  305. ------------------------
  306. The :control:`add_consumer` control command will tell one or more workers
  307. to start consuming from a queue. This operation is idempotent.
  308. To tell all workers in the cluster to start consuming from a queue
  309. named "``foo``" you can use the :program:`celery control` program:
  310. .. code-block:: bash
  311. $ celery control add_consumer foo
  312. -> worker1.local: OK
  313. started consuming from u'foo'
  314. If you want to specify a specific worker you can use the
  315. :option:`--destination`` argument:
  316. .. code-block:: bash
  317. $ celery control add_consumer foo -d worker1.local
  318. The same can be accomplished dynamically using the :meth:`@control.add_consumer` method::
  319. >>> myapp.control.add_consumer('foo', reply=True)
  320. [{u'worker1.local': {u'ok': u"already consuming from u'foo'"}}]
  321. >>> myapp.control.add_consumer('foo', reply=True,
  322. ... destination=['worker1.local'])
  323. [{u'worker1.local': {u'ok': u"already consuming from u'foo'"}}]
  324. By now I have only shown examples using automatic queues,
  325. If you need more control you can also specify the exchange, routing_key and
  326. even other options::
  327. >>> myapp.control.add_consumer(
  328. ... queue='baz',
  329. ... exchange='ex',
  330. ... exchange_type='topic',
  331. ... routing_key='media.*',
  332. ... options={
  333. ... 'queue_durable': False,
  334. ... 'exchange_durable': False,
  335. ... },
  336. ... reply=True,
  337. ... destination=['worker1.local', 'worker2.local'])
  338. .. control:: cancel_consumer
  339. Queues: Cancelling consumers
  340. ----------------------------
  341. You can cancel a consumer by queue name using the :control:`cancel_consumer`
  342. control command.
  343. To force all workers in the cluster to cancel consuming from a queue
  344. you can use the :program:`celery control` program:
  345. .. code-block:: bash
  346. $ celery control cancel_consumer foo
  347. The :option:`--destination` argument can be used to specify a worker, or a
  348. list of workers, to act on the command:
  349. .. code-block:: bash
  350. $ celery control cancel_consumer foo -d worker1.local
  351. You can also cancel consumers programmatically using the
  352. :meth:`@control.cancel_consumer` method:
  353. .. code-block:: bash
  354. >>> myapp.control.cancel_consumer('foo', reply=True)
  355. [{u'worker1.local': {u'ok': u"no longer consuming from u'foo'"}}]
  356. .. control:: active_queues
  357. Queues: List of active queues
  358. -----------------------------
  359. You can get a list of queues that a worker consumes from by using
  360. the :control:`active_queues` control command:
  361. .. code-block:: bash
  362. $ celery inspect active_queues
  363. [...]
  364. Like all other remote control commands this also supports the
  365. :option:`--destination` argument used to specify which workers should
  366. reply to the request:
  367. .. code-block:: bash
  368. $ celery inspect active_queues -d worker1.local
  369. [...]
  370. This can also be done programmatically by using the
  371. :meth:`@control.inspect.active_queues` method::
  372. >>> myapp.inspect().active_queues()
  373. [...]
  374. >>> myapp.inspect(['worker1.local']).active_queues()
  375. [...]
  376. .. _worker-autoreloading:
  377. Autoreloading
  378. =============
  379. .. versionadded:: 2.5
  380. pool support: *processes, eventlet, gevent, threads, solo*
  381. Starting :program:`celery worker` with the :option:`--autoreload` option will
  382. enable the worker to watch for file system changes to all imported task
  383. modules imported (and also any non-task modules added to the
  384. :setting:`CELERY_IMPORTS` setting or the :option:`-I|--include` option).
  385. This is an experimental feature intended for use in development only,
  386. using auto-reload in production is discouraged as the behavior of reloading
  387. a module in Python is undefined, and may cause hard to diagnose bugs and
  388. crashes. Celery uses the same approach as the auto-reloader found in e.g.
  389. the Django ``runserver`` command.
  390. When auto-reload is enabled the worker starts an additional thread
  391. that watches for changes in the file system. New modules are imported,
  392. and already imported modules are reloaded whenever a change is detected,
  393. and if the processes pool is used the child processes will finish the work
  394. they are doing and exit, so that they can be replaced by fresh processes
  395. effectively reloading the code.
  396. File system notification backends are pluggable, and it comes with three
  397. implementations:
  398. * inotify (Linux)
  399. Used if the :mod:`pyinotify` library is installed.
  400. If you are running on Linux this is the recommended implementation,
  401. to install the :mod:`pyinotify` library you have to run the following
  402. command:
  403. .. code-block:: bash
  404. $ pip install pyinotify
  405. * kqueue (OS X/BSD)
  406. * stat
  407. The fallback implementation simply polls the files using ``stat`` and is very
  408. expensive.
  409. You can force an implementation by setting the :envvar:`CELERYD_FSNOTIFY`
  410. environment variable:
  411. .. code-block:: bash
  412. $ env CELERYD_FSNOTIFY=stat celery worker -l info --autoreload
  413. .. _worker-autoreload:
  414. .. control:: pool_restart
  415. Pool Restart Command
  416. --------------------
  417. .. versionadded:: 2.5
  418. Requires the :setting:`CELERYD_POOL_RESTARTS` setting to be enabled.
  419. The remote control command :control:`pool_restart` sends restart requests to
  420. the workers child processes. It is particularly useful for forcing
  421. the worker to import new modules, or for reloading already imported
  422. modules. This command does not interrupt executing tasks.
  423. Example
  424. ~~~~~~~
  425. Running the following command will result in the `foo` and `bar` modules
  426. being imported by the worker processes:
  427. .. code-block:: python
  428. >>> celery.control.broadcast('pool_restart',
  429. ... arguments={'modules': ['foo', 'bar']})
  430. Use the ``reload`` argument to reload modules it has already imported:
  431. .. code-block:: python
  432. >>> celery.control.broadcast('pool_restart',
  433. ... arguments={'modules': ['foo'],
  434. ... 'reload': True})
  435. If you don't specify any modules then all known tasks modules will
  436. be imported/reloaded:
  437. .. code-block:: python
  438. >>> celery.control.broadcast('pool_restart', arguments={'reload': True})
  439. The ``modules`` argument is a list of modules to modify. ``reload``
  440. specifies whether to reload modules if they have previously been imported.
  441. By default ``reload`` is disabled. The `pool_restart` command uses the
  442. Python :func:`reload` function to reload modules, or you can provide
  443. your own custom reloader by passing the ``reloader`` argument.
  444. .. note::
  445. Module reloading comes with caveats that are documented in :func:`reload`.
  446. Please read this documentation and make sure your modules are suitable
  447. for reloading.
  448. .. seealso::
  449. - http://pyunit.sourceforge.net/notes/reloading.html
  450. - http://www.indelible.org/ink/python-reloading/
  451. - http://docs.python.org/library/functions.html#reload
  452. .. _worker-inspect:
  453. Inspecting workers
  454. ==================
  455. :class:`@control.inspect` lets you inspect running workers. It
  456. uses remote control commands under the hood.
  457. You can also use the ``celery`` command to inspect workers,
  458. and it supports the same commands as the :class:`@Celery.control` interface.
  459. .. code-block:: python
  460. # Inspect all nodes.
  461. >>> i = celery.control.inspect()
  462. # Specify multiple nodes to inspect.
  463. >>> i = celery.control.inspect(['worker1.example.com',
  464. 'worker2.example.com'])
  465. # Specify a single node to inspect.
  466. >>> i = celery.control.inspect('worker1.example.com')
  467. .. _worker-inspect-registered-tasks:
  468. Dump of registered tasks
  469. ------------------------
  470. You can get a list of tasks registered in the worker using the
  471. :meth:`~@control.inspect.registered`::
  472. >>> i.registered()
  473. [{'worker1.example.com': ['tasks.add',
  474. 'tasks.sleeptask']}]
  475. .. _worker-inspect-active-tasks:
  476. Dump of currently executing tasks
  477. ---------------------------------
  478. You can get a list of active tasks using
  479. :meth:`~@control.inspect.active`::
  480. >>> i.active()
  481. [{'worker1.example.com':
  482. [{'name': 'tasks.sleeptask',
  483. 'id': '32666e9b-809c-41fa-8e93-5ae0c80afbbf',
  484. 'args': '(8,)',
  485. 'kwargs': '{}'}]}]
  486. .. _worker-inspect-eta-schedule:
  487. Dump of scheduled (ETA) tasks
  488. -----------------------------
  489. You can get a list of tasks waiting to be scheduled by using
  490. :meth:`~@control.inspect.scheduled`::
  491. >>> i.scheduled()
  492. [{'worker1.example.com':
  493. [{'eta': '2010-06-07 09:07:52', 'priority': 0,
  494. 'request': {
  495. 'name': 'tasks.sleeptask',
  496. 'id': '1a7980ea-8b19-413e-91d2-0b74f3844c4d',
  497. 'args': '[1]',
  498. 'kwargs': '{}'}},
  499. {'eta': '2010-06-07 09:07:53', 'priority': 0,
  500. 'request': {
  501. 'name': 'tasks.sleeptask',
  502. 'id': '49661b9a-aa22-4120-94b7-9ee8031d219d',
  503. 'args': '[2]',
  504. 'kwargs': '{}'}}]}]
  505. .. note::
  506. These are tasks with an eta/countdown argument, not periodic tasks.
  507. .. _worker-inspect-reserved:
  508. Dump of reserved tasks
  509. ----------------------
  510. Reserved tasks are tasks that has been received, but is still waiting to be
  511. executed.
  512. You can get a list of these using
  513. :meth:`~@control.inspect.reserved`::
  514. >>> i.reserved()
  515. [{'worker1.example.com':
  516. [{'name': 'tasks.sleeptask',
  517. 'id': '32666e9b-809c-41fa-8e93-5ae0c80afbbf',
  518. 'args': '(8,)',
  519. 'kwargs': '{}'}]}]
  520. Additional Commands
  521. ===================
  522. .. control:: shutdown
  523. Remote shutdown
  524. ---------------
  525. This command will gracefully shut down the worker remotely::
  526. >>> celery.control.broadcast('shutdown') # shutdown all workers
  527. >>> celery.control.broadcast('shutdown, destination='worker1.example.com')
  528. .. control:: ping
  529. Ping
  530. ----
  531. This command requests a ping from alive workers.
  532. The workers reply with the string 'pong', and that's just about it.
  533. It will use the default one second timeout for replies unless you specify
  534. a custom timeout::
  535. >>> celery.control.ping(timeout=0.5)
  536. [{'worker1.example.com': 'pong'},
  537. {'worker2.example.com': 'pong'},
  538. {'worker3.example.com': 'pong'}]
  539. :meth:`~@control.ping` also supports the `destination` argument,
  540. so you can specify which workers to ping::
  541. >>> ping(['worker2.example.com', 'worker3.example.com'])
  542. [{'worker2.example.com': 'pong'},
  543. {'worker3.example.com': 'pong'}]
  544. .. _worker-enable-events:
  545. .. control:: enable_events
  546. .. control:: disable_events
  547. Enable/disable events
  548. ---------------------
  549. You can enable/disable events by using the `enable_events`,
  550. `disable_events` commands. This is useful to temporarily monitor
  551. a worker using :program:`celery events`/:program:`celerymon`.
  552. .. code-block:: python
  553. >>> celery.control.enable_events()
  554. >>> celery.control.disable_events()
  555. .. _worker-custom-control-commands:
  556. Writing your own remote control commands
  557. ========================================
  558. Remote control commands are registered in the control panel and
  559. they take a single argument: the current
  560. :class:`~celery.worker.control.ControlDispatch` instance.
  561. From there you have access to the active
  562. :class:`~celery.worker.consumer.Consumer` if needed.
  563. Here's an example control command that restarts the broker connection:
  564. .. code-block:: python
  565. from celery.worker.control import Panel
  566. @Panel.register
  567. def reset_connection(panel):
  568. panel.consumer.reset_connection()
  569. return {'ok': 'connection reset'}