signals.rst 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  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. .. _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_after_setup
  132. celeryd_after_setup
  133. ~~~~~~~~~~~~~~~~~~~
  134. This signal is sent after the worker instance is set up,
  135. but before it calls run. This means that any queues from the :option:`-Q`
  136. option is enabled, logging has been set up and so on.
  137. It can be used to e.g. add custom queues that should always be consumed
  138. from, disregarding the :option:`-Q` option. Here's an example
  139. that sets up a direct queue for each worker, these queues can then be
  140. used to route a task to any specific worker:
  141. .. code-block:: python
  142. from celery.signals import celeryd_after_setup
  143. @celeryd_after_setup.connect
  144. def setup_direct_queue(sender, instance, **kwargs):
  145. queue_name = '{0}.dq'.format(sender) # sender is the hostname of the worker
  146. instance.app.queues.select_add(queue_name)
  147. Provides arguments:
  148. * sender
  149. Hostname of the worker.
  150. * instance
  151. This is the :class:`celery.apps.worker.Worker` instance to be initialized.
  152. Note that only the :attr:`app` and :attr:`hostname` attributes have been
  153. set so far, and the rest of ``__init__`` has not been executed.
  154. * conf
  155. The configuration of the current app.
  156. .. signal:: celeryd_init
  157. celeryd_init
  158. ~~~~~~~~~~~~
  159. This is the first signal sent when :program:`celeryd` starts up.
  160. The ``sender`` is the host name of the worker, so this signal can be used
  161. to setup worker specific configuration:
  162. .. code-block:: python
  163. from celery.signals import celeryd_init
  164. @celeryd_init.connect(sender='worker12.example.com')
  165. def configure_worker12(conf=None, **kwargs):
  166. conf.CELERY_DEFAULT_RATE_LIMIT = '10/m'
  167. or to set up configuration for multiple workers you can omit specifying a
  168. sender when you connect:
  169. .. code-block:: python
  170. from celery.signals import celeryd_init
  171. @celeryd_init.connect
  172. def configure_workers(sender=None, conf=None, **kwargs):
  173. if sender in ('worker1.example.com', 'worker2.example.com'):
  174. conf.CELERY_DEFAULT_RATE_LIMIT = '10/m'
  175. if sender == 'worker3.example.com':
  176. conf.CELERYD_PREFETCH_MULTIPLIER = 0
  177. Provides arguments:
  178. * sender
  179. Hostname of the worker.
  180. * instance
  181. This is the :class:`celery.apps.worker.Worker` instance to be initialized.
  182. Note that only the :attr:`app` and :attr:`hostname` attributes have been
  183. set so far, and the rest of ``__init__`` has not been executed.
  184. * conf
  185. The configuration of the current app.
  186. .. signal:: worker_init
  187. worker_init
  188. ~~~~~~~~~~~
  189. Dispatched before the worker is started.
  190. .. signal:: worker_ready
  191. worker_ready
  192. ~~~~~~~~~~~~
  193. Dispatched when the worker is ready to accept work.
  194. .. signal:: worker_process_init
  195. worker_process_init
  196. ~~~~~~~~~~~~~~~~~~~
  197. Dispatched by each new pool worker process when it starts.
  198. .. signal:: worker_shutdown
  199. worker_shutdown
  200. ~~~~~~~~~~~~~~~
  201. Dispatched when the worker is about to shut down.
  202. Celerybeat Signals
  203. ------------------
  204. .. signal:: beat_init
  205. beat_init
  206. ~~~~~~~~~
  207. Dispatched when celerybeat starts (either standalone or embedded).
  208. Sender is the :class:`celery.beat.Service` instance.
  209. .. signal:: beat_embedded_init
  210. beat_embedded_init
  211. ~~~~~~~~~~~~~~~~~~
  212. Dispatched in addition to the :signal:`beat_init` signal when celerybeat is
  213. started as an embedded process. Sender is the
  214. :class:`celery.beat.Service` instance.
  215. Eventlet Signals
  216. ----------------
  217. .. signal:: eventlet_pool_started
  218. eventlet_pool_started
  219. ~~~~~~~~~~~~~~~~~~~~~
  220. Sent when the eventlet pool has been started.
  221. Sender is the :class:`celery.concurrency.eventlet.TaskPool` instance.
  222. .. signal:: eventlet_pool_preshutdown
  223. eventlet_pool_preshutdown
  224. ~~~~~~~~~~~~~~~~~~~~~~~~~
  225. Sent when the worker shutdown, just before the eventlet pool
  226. is requested to wait for remaining workers.
  227. Sender is the :class:`celery.concurrency.eventlet.TaskPool` instance.
  228. .. signal:: eventlet_pool_postshutdown
  229. eventlet_pool_postshutdown
  230. ~~~~~~~~~~~~~~~~~~~~~~~~~~
  231. Sent when the pool has been joined and the worker is ready to shutdown.
  232. Sender is the :class:`celery.concurrency.eventlet.TaskPool` instance.
  233. .. signal:: eventlet_pool_apply
  234. eventlet_pool_apply
  235. ~~~~~~~~~~~~~~~~~~~
  236. Sent whenever a task is applied to the pool.
  237. Sender is the :class:`celery.concurrency.eventlet.TaskPool` instance.
  238. Provides arguments:
  239. * target
  240. The target function.
  241. * args
  242. Positional arguments.
  243. * kwargs
  244. Keyword arguments.
  245. Logging Signals
  246. ---------------
  247. .. signal:: setup_logging
  248. setup_logging
  249. ~~~~~~~~~~~~~
  250. Celery won't configure the loggers if this signal is connected,
  251. so you can use this to completely override the logging configuration
  252. with your own.
  253. If you would like to augment the logging configuration setup by
  254. Celery then you can use the :signal:`after_setup_logger` and
  255. :signal:`after_setup_task_logger` signals.
  256. Provides arguments:
  257. * loglevel
  258. The level of the logging object.
  259. * logfile
  260. The name of the logfile.
  261. * format
  262. The log format string.
  263. * colorize
  264. Specify if log messages are colored or not.
  265. .. signal:: after_setup_logger
  266. after_setup_logger
  267. ~~~~~~~~~~~~~~~~~~
  268. Sent after the setup of every global logger (not task loggers).
  269. Used to augment logging configuration.
  270. Provides arguments:
  271. * logger
  272. The logger object.
  273. * loglevel
  274. The level of the logging object.
  275. * logfile
  276. The name of the logfile.
  277. * format
  278. The log format string.
  279. * colorize
  280. Specify if log messages are colored or not.
  281. .. signal:: after_setup_task_logger
  282. after_setup_task_logger
  283. ~~~~~~~~~~~~~~~~~~~~~~~
  284. Sent after the setup of every single task logger.
  285. Used to augment logging configuration.
  286. Provides arguments:
  287. * logger
  288. The logger object.
  289. * loglevel
  290. The level of the logging object.
  291. * logfile
  292. The name of the logfile.
  293. * format
  294. The log format string.
  295. * colorize
  296. Specify if log messages are colored or not.