signals.rst 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  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 %s' % (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(task_sent_handler, 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 %s' % (task_id, ))
  33. .. _signal-ref:
  34. Signals
  35. =======
  36. Task Signals
  37. ------------
  38. .. signal:: task_sent
  39. task_sent
  40. ~~~~~~~~~
  41. Dispatched when a task has been sent to the broker.
  42. Note that this is executed in the client process, the one sending
  43. the task, not in the worker.
  44. Sender is the name of the task being sent.
  45. Provides arguments:
  46. * task_id
  47. Id of the task to be executed.
  48. * task
  49. The task being executed.
  50. * args
  51. the tasks positional arguments.
  52. * kwargs
  53. The tasks keyword arguments.
  54. * eta
  55. The time to execute the task.
  56. * taskset
  57. Id of the taskset this task is part of (if any).
  58. .. signal:: task_prerun
  59. task_prerun
  60. ~~~~~~~~~~~
  61. Dispatched before a task is executed.
  62. Sender is the task class being executed.
  63. Provides arguments:
  64. * task_id
  65. Id of the task to be executed.
  66. * task
  67. The task being executed.
  68. * args
  69. the tasks positional arguments.
  70. * kwargs
  71. The tasks keyword arguments.
  72. .. signal:: task_postrun
  73. task_postrun
  74. ~~~~~~~~~~~~
  75. Dispatched after a task has been executed.
  76. Sender is the task class executed.
  77. Provides arguments:
  78. * task_id
  79. Id of the task to be executed.
  80. * task
  81. The task being executed.
  82. * args
  83. The tasks positional arguments.
  84. * kwargs
  85. The tasks keyword arguments.
  86. * retval
  87. The return value of the task.
  88. * state
  89. Name of the resulting state.
  90. .. signal:: task_success
  91. task_success
  92. ~~~~~~~~~~~~
  93. Dispatched when a task succeeds.
  94. Sender is the task class executed.
  95. Provides arguments
  96. * result
  97. Return value of the task.
  98. .. signal:: task_failure
  99. task_failure
  100. ~~~~~~~~~~~~
  101. Dispatched when a task fails.
  102. Sender is the task class executed.
  103. Provides arguments:
  104. * task_id
  105. Id of the task.
  106. * exception
  107. Exception instance raised.
  108. * args
  109. Positional arguments the task was called with.
  110. * kwargs
  111. Keyword arguments the task was called with.
  112. * traceback
  113. Stack trace object.
  114. * einfo
  115. The :class:`celery.datastructures.ExceptionInfo` instance.
  116. .. signal:: task_revoked
  117. task_revoked
  118. ~~~~~~~~~~~~
  119. Dispatched when a task is revoked/terminated by the worker.
  120. Sender is the task class revoked/terminated.
  121. Provides arguments:
  122. * terminated
  123. Set to :const:`True` if the task was terminated.
  124. * signum
  125. Signal number used to terminate the task. If this is :const:`None` and
  126. terminated is :const:`True` then :sig:`TERM` should be assumed.
  127. * expired
  128. Set to :const:`True` if the task expired.
  129. Worker Signals
  130. --------------
  131. .. signal:: celeryd_init
  132. celeryd_init
  133. ~~~~~~~~~~~~
  134. This is the first signal sent when :program:`celeryd` starts up.
  135. The ``sender`` is the host name of the worker, so this signal can be used
  136. to setup worker specific configuration:
  137. .. code-block:: python
  138. from celery.signals import celeryd_init
  139. @celeryd_init.connect(sender='worker12.example.com')
  140. def configure_worker12(conf=None, **kwargs):
  141. conf.CELERY_DEFAULT_RATE_LIMIT = '10/m'
  142. or to set up configuration for multiple workers you can omit specifying a
  143. sender when you connect:
  144. .. code-block:: python
  145. from celery.signals import celeryd_init
  146. @celeryd_init.connect
  147. def configure_workers(sender=None, conf=None, **kwargs):
  148. if sender in ('worker1.example.com', 'worker2.example.com'):
  149. conf.CELERY_DEFAULT_RATE_LIMIT = '10/m'
  150. if sender == 'worker3.example.com':
  151. conf.CELERYD_PREFETCH_MULTIPLIER = 0
  152. Provides arguments:
  153. * instance
  154. This is the :class:`celery.apps.worker.Worker` instance to be initialized.
  155. Note that only the :attr:`app` and :attr:`hostname` attributes have been
  156. set so far, and the rest of ``__init__`` has not been executed.
  157. * conf
  158. The configuration of the current app.
  159. .. signal:: worker_init
  160. worker_init
  161. ~~~~~~~~~~~
  162. Dispatched before the worker is started.
  163. .. signal:: worker_ready
  164. worker_ready
  165. ~~~~~~~~~~~~
  166. Dispatched when the worker is ready to accept work.
  167. .. signal:: worker_process_init
  168. worker_process_init
  169. ~~~~~~~~~~~~~~~~~~~
  170. Dispatched by each new pool worker process when it starts.
  171. .. signal:: worker_shutdown
  172. worker_shutdown
  173. ~~~~~~~~~~~~~~~
  174. Dispatched when the worker is about to shut down.
  175. Celerybeat Signals
  176. ------------------
  177. .. signal:: beat_init
  178. beat_init
  179. ~~~~~~~~~
  180. Dispatched when celerybeat starts (either standalone or embedded).
  181. Sender is the :class:`celery.beat.Service` instance.
  182. .. signal:: beat_embedded_init
  183. beat_embedded_init
  184. ~~~~~~~~~~~~~~~~~~
  185. Dispatched in addition to the :signal:`beat_init` signal when celerybeat is
  186. started as an embedded process. Sender is the
  187. :class:`celery.beat.Service` instance.
  188. Eventlet Signals
  189. ----------------
  190. .. signal:: eventlet_pool_started
  191. eventlet_pool_started
  192. ~~~~~~~~~~~~~~~~~~~~~
  193. Sent when the eventlet pool has been started.
  194. Sender is the :class:`celery.concurrency.eventlet.TaskPool` instance.
  195. .. signal:: eventlet_pool_preshutdown
  196. eventlet_pool_preshutdown
  197. ~~~~~~~~~~~~~~~~~~~~~~~~~
  198. Sent when the worker shutdown, just before the eventlet pool
  199. is requested to wait for remaining workers.
  200. Sender is the :class:`celery.concurrency.eventlet.TaskPool` instance.
  201. .. signal:: eventlet_pool_postshutdown
  202. eventlet_pool_postshutdown
  203. ~~~~~~~~~~~~~~~~~~~~~~~~~~
  204. Sent when the pool has been joined and the worker is ready to shutdown.
  205. Sender is the :class:`celery.concurrency.eventlet.TaskPool` instance.
  206. .. signal:: eventlet_pool_apply
  207. eventlet_pool_apply
  208. ~~~~~~~~~~~~~~~~~~~
  209. Sent whenever a task is applied to the pool.
  210. Sender is the :class:`celery.concurrency.eventlet.TaskPool` instance.
  211. Provides arguments:
  212. * target
  213. The target function.
  214. * args
  215. Positional arguments.
  216. * kwargs
  217. Keyword arguments.
  218. Logging Signals
  219. ---------------
  220. .. signal:: setup_logging
  221. setup_logging
  222. ~~~~~~~~~~~~~
  223. Celery won't configure the loggers if this signal is connected,
  224. so you can use this to completely override the logging configuration
  225. with your own.
  226. If you would like to augment the logging configuration setup by
  227. Celery then you can use the :signal:`after_setup_logger` and
  228. :signal:`after_setup_task_logger` signals.
  229. Provides arguments:
  230. * loglevel
  231. The level of the logging object.
  232. * logfile
  233. The name of the logfile.
  234. * format
  235. The log format string.
  236. * colorize
  237. Specify if log messages are colored or not.
  238. .. signal:: after_setup_logger
  239. after_setup_logger
  240. ~~~~~~~~~~~~~~~~~~
  241. Sent after the setup of every global logger (not task loggers).
  242. Used to augment logging configuration.
  243. Provides arguments:
  244. * logger
  245. The logger object.
  246. * loglevel
  247. The level of the logging object.
  248. * logfile
  249. The name of the logfile.
  250. * format
  251. The log format string.
  252. * colorize
  253. Specify if log messages are colored or not.
  254. .. signal:: after_setup_task_logger
  255. after_setup_task_logger
  256. ~~~~~~~~~~~~~~~~~~~~~~~
  257. Sent after the setup of every single task logger.
  258. Used to augment logging configuration.
  259. Provides arguments:
  260. * logger
  261. The logger object.
  262. * loglevel
  263. The level of the logging object.
  264. * logfile
  265. The name of the logfile.
  266. * format
  267. The log format string.
  268. * colorize
  269. Specify if log messages are colored or not.