signals.rst 9.8 KB

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