signals.rst 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  1. .. _signals:
  2. =======
  3. Signals
  4. =======
  5. .. contents::
  6. :local:
  7. Signals allows decoupled applications to receive notifications when
  8. certain actions occur elsewhere in the application.
  9. Celery ships with many signals that you application can hook into
  10. to augment behavior of certain actions.
  11. .. _signal-basics:
  12. Basics
  13. ======
  14. Several kinds of events trigger signals, you can connect to these signals
  15. to perform actions as they trigger.
  16. Example connecting to the :signal:`task_sent` signal:
  17. .. code-block:: python
  18. from celery.signals import task_sent
  19. @task_sent.connect
  20. def task_sent_handler(sender=None, task_id=None, task=None, args=None,
  21. kwargs=None, **kwds):
  22. print('Got signal task_sent for task id {0}'.format(task_id))
  23. Some signals also have a sender which you can filter by. For example the
  24. :signal:`task_sent` signal uses the task name as a sender, so you can
  25. connect your handler to be called only when tasks with name `"tasks.add"`
  26. has been sent by providing the `sender` argument to
  27. :class:`~celery.utils.dispatch.signal.Signal.connect`:
  28. .. code-block:: python
  29. @task_sent.connect(sender='tasks.add')
  30. def task_sent_handler(sender=None, task_id=None, task=None, args=None,
  31. kwargs=None, **kwds):
  32. print('Got signal task_sent for task id {0}'.format(task_id)
  33. Signals use the same implementation as django.core.dispatch. As a result other
  34. keyword parameters (e.g. signal) are passed to all signal handlers by default.
  35. The best practice for signal handlers is to accept arbitrary keyword arguments (**kwargs.
  36. That way new celery versions can add additional arguments without breaking user code.
  37. .. _signal-ref:
  38. Signals
  39. =======
  40. Task Signals
  41. ------------
  42. .. signal:: task_sent
  43. task_sent
  44. ~~~~~~~~~
  45. Dispatched when a task has been sent to the broker.
  46. Note that this is executed in the client process, the one sending
  47. the task, not in the worker.
  48. Sender is the name of the task being sent.
  49. Provides arguments:
  50. * task_id
  51. Id of the task to be executed.
  52. * task
  53. The task being executed.
  54. * args
  55. the tasks positional arguments.
  56. * kwargs
  57. The tasks keyword arguments.
  58. * eta
  59. The time to execute the task.
  60. * taskset
  61. Id of the taskset this task is part of (if any).
  62. .. signal:: task_prerun
  63. task_prerun
  64. ~~~~~~~~~~~
  65. Dispatched before a task is executed.
  66. Sender is the task class being executed.
  67. Provides arguments:
  68. * task_id
  69. Id of the task to be executed.
  70. * task
  71. The task being executed.
  72. * args
  73. the tasks positional arguments.
  74. * kwargs
  75. The tasks keyword arguments.
  76. .. signal:: task_postrun
  77. task_postrun
  78. ~~~~~~~~~~~~
  79. Dispatched after a task has been executed.
  80. Sender is the task class executed.
  81. Provides arguments:
  82. * task_id
  83. Id of the task to be executed.
  84. * task
  85. The task being executed.
  86. * args
  87. The tasks positional arguments.
  88. * kwargs
  89. The tasks keyword arguments.
  90. * retval
  91. The return value of the task.
  92. * state
  93. Name of the resulting state.
  94. .. signal:: task_success
  95. task_success
  96. ~~~~~~~~~~~~
  97. Dispatched when a task succeeds.
  98. Sender is the task class executed.
  99. Provides arguments
  100. * result
  101. Return value of the task.
  102. .. signal:: task_failure
  103. task_failure
  104. ~~~~~~~~~~~~
  105. Dispatched when a task fails.
  106. Sender is the task class executed.
  107. Provides arguments:
  108. * task_id
  109. Id of the task.
  110. * exception
  111. Exception instance raised.
  112. * args
  113. Positional arguments the task was called with.
  114. * kwargs
  115. Keyword arguments the task was called with.
  116. * traceback
  117. Stack trace object.
  118. * einfo
  119. The :class:`celery.datastructures.ExceptionInfo` instance.
  120. .. signal:: task_revoked
  121. task_revoked
  122. ~~~~~~~~~~~~
  123. Dispatched when a task is revoked/terminated by the worker.
  124. Sender is the task class revoked/terminated.
  125. Provides arguments:
  126. * terminated
  127. Set to :const:`True` if the task was terminated.
  128. * signum
  129. Signal number used to terminate the task. If this is :const:`None` and
  130. terminated is :const:`True` then :sig:`TERM` should be assumed.
  131. * expired
  132. Set to :const:`True` if the task expired.
  133. Worker Signals
  134. --------------
  135. .. signal:: celeryd_after_setup
  136. celeryd_after_setup
  137. ~~~~~~~~~~~~~~~~~~~
  138. This signal is sent after the worker instance is set up,
  139. but before it calls run. This means that any queues from the :option:`-Q`
  140. option is enabled, logging has been set up and so on.
  141. It can be used to e.g. add custom queues that should always be consumed
  142. from, disregarding the :option:`-Q` option. Here's an example
  143. that sets up a direct queue for each worker, these queues can then be
  144. used to route a task to any specific worker:
  145. .. code-block:: python
  146. from celery.signals import celeryd_after_setup
  147. @celeryd_after_setup.connect
  148. def setup_direct_queue(sender, instance, **kwargs):
  149. queue_name = '{0}.dq'.format(sender) # sender is the hostname of the worker
  150. instance.app.amqp.queues.select_add(queue_name)
  151. Provides arguments:
  152. * sender
  153. Hostname of the worker.
  154. * instance
  155. This is the :class:`celery.apps.worker.Worker` instance to be initialized.
  156. Note that only the :attr:`app` and :attr:`hostname` attributes have been
  157. set so far, and the rest of ``__init__`` has not been executed.
  158. * conf
  159. The configuration of the current app.
  160. .. signal:: celeryd_init
  161. celeryd_init
  162. ~~~~~~~~~~~~
  163. This is the first signal sent when :program:`celery worker` starts up.
  164. The ``sender`` is the host name of the worker, so this signal can be used
  165. to setup worker specific configuration:
  166. .. code-block:: python
  167. from celery.signals import celeryd_init
  168. @celeryd_init.connect(sender='worker12.example.com')
  169. def configure_worker12(conf=None, **kwargs):
  170. conf.CELERY_DEFAULT_RATE_LIMIT = '10/m'
  171. or to set up configuration for multiple workers you can omit specifying a
  172. sender when you connect:
  173. .. code-block:: python
  174. from celery.signals import celeryd_init
  175. @celeryd_init.connect
  176. def configure_workers(sender=None, conf=None, **kwargs):
  177. if sender in ('worker1.example.com', 'worker2.example.com'):
  178. conf.CELERY_DEFAULT_RATE_LIMIT = '10/m'
  179. if sender == 'worker3.example.com':
  180. conf.CELERYD_PREFETCH_MULTIPLIER = 0
  181. Provides arguments:
  182. * sender
  183. Hostname of the worker.
  184. * instance
  185. This is the :class:`celery.apps.worker.Worker` instance to be initialized.
  186. Note that only the :attr:`app` and :attr:`hostname` attributes have been
  187. set so far, and the rest of ``__init__`` has not been executed.
  188. * conf
  189. The configuration of the current app.
  190. .. signal:: worker_init
  191. worker_init
  192. ~~~~~~~~~~~
  193. Dispatched before the worker is started.
  194. .. signal:: worker_ready
  195. worker_ready
  196. ~~~~~~~~~~~~
  197. Dispatched when the worker is ready to accept work.
  198. .. signal:: worker_process_init
  199. worker_process_init
  200. ~~~~~~~~~~~~~~~~~~~
  201. Dispatched by each new pool worker process when it starts.
  202. .. signal:: worker_shutdown
  203. worker_shutdown
  204. ~~~~~~~~~~~~~~~
  205. Dispatched when the worker is about to shut down.
  206. Beat Signals
  207. ------------
  208. .. signal:: beat_init
  209. beat_init
  210. ~~~~~~~~~
  211. Dispatched when :program:`celery beat` starts (either standalone or embedded).
  212. Sender is the :class:`celery.beat.Service` instance.
  213. .. signal:: beat_embedded_init
  214. beat_embedded_init
  215. ~~~~~~~~~~~~~~~~~~
  216. Dispatched in addition to the :signal:`beat_init` signal when :program:`celery
  217. beat` is started as an embedded process. Sender is the
  218. :class:`celery.beat.Service` instance.
  219. Eventlet Signals
  220. ----------------
  221. .. signal:: eventlet_pool_started
  222. eventlet_pool_started
  223. ~~~~~~~~~~~~~~~~~~~~~
  224. Sent when the eventlet pool has been started.
  225. Sender is the :class:`celery.concurrency.eventlet.TaskPool` instance.
  226. .. signal:: eventlet_pool_preshutdown
  227. eventlet_pool_preshutdown
  228. ~~~~~~~~~~~~~~~~~~~~~~~~~
  229. Sent when the worker shutdown, just before the eventlet pool
  230. is requested to wait for remaining workers.
  231. Sender is the :class:`celery.concurrency.eventlet.TaskPool` instance.
  232. .. signal:: eventlet_pool_postshutdown
  233. eventlet_pool_postshutdown
  234. ~~~~~~~~~~~~~~~~~~~~~~~~~~
  235. Sent when the pool has been joined and the worker is ready to shutdown.
  236. Sender is the :class:`celery.concurrency.eventlet.TaskPool` instance.
  237. .. signal:: eventlet_pool_apply
  238. eventlet_pool_apply
  239. ~~~~~~~~~~~~~~~~~~~
  240. Sent whenever a task is applied to the pool.
  241. Sender is the :class:`celery.concurrency.eventlet.TaskPool` instance.
  242. Provides arguments:
  243. * target
  244. The target function.
  245. * args
  246. Positional arguments.
  247. * kwargs
  248. Keyword arguments.
  249. Logging Signals
  250. ---------------
  251. .. signal:: setup_logging
  252. setup_logging
  253. ~~~~~~~~~~~~~
  254. Celery won't configure the loggers if this signal is connected,
  255. so you can use this to completely override the logging configuration
  256. with your own.
  257. If you would like to augment the logging configuration setup by
  258. Celery then you can use the :signal:`after_setup_logger` and
  259. :signal:`after_setup_task_logger` signals.
  260. Provides arguments:
  261. * loglevel
  262. The level of the logging object.
  263. * logfile
  264. The name of the logfile.
  265. * format
  266. The log format string.
  267. * colorize
  268. Specify if log messages are colored or not.
  269. .. signal:: after_setup_logger
  270. after_setup_logger
  271. ~~~~~~~~~~~~~~~~~~
  272. Sent after the setup of every global logger (not task loggers).
  273. Used to augment logging configuration.
  274. Provides arguments:
  275. * logger
  276. The logger object.
  277. * loglevel
  278. The level of the logging object.
  279. * logfile
  280. The name of the logfile.
  281. * format
  282. The log format string.
  283. * colorize
  284. Specify if log messages are colored or not.
  285. .. signal:: after_setup_task_logger
  286. after_setup_task_logger
  287. ~~~~~~~~~~~~~~~~~~~~~~~
  288. Sent after the setup of every single task logger.
  289. Used to augment logging configuration.
  290. Provides arguments:
  291. * logger
  292. The logger object.
  293. * loglevel
  294. The level of the logging object.
  295. * logfile
  296. The name of the logfile.
  297. * format
  298. The log format string.
  299. * colorize
  300. Specify if log messages are colored or not.