signals.rst 9.8 KB

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