workers.rst 25 KB

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