signals.py 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. """
  2. ==============
  3. celery.signals
  4. ==============
  5. Signals allows decoupled applications to receive notifications when
  6. certain actions occur elsewhere in the application.
  7. .. contents::
  8. :local:
  9. .. _signal-basics:
  10. Basics
  11. ======
  12. Several kinds of events trigger signals, you can connect to these signals
  13. to perform actions as they trigger.
  14. Example connecting to the :signal:`task_sent` signal:
  15. .. code-block:: python
  16. from celery.signals import task_sent
  17. def task_sent_handler(sender=None, task_id=None, task=None, args=None,
  18. kwargs=None, **kwds):
  19. print("Got signal task_sent for task id %s" % (task_id, ))
  20. task_sent.connect(task_sent_handler)
  21. Some signals also have a sender which you can filter by. For example the
  22. :signal:`task_sent` signal uses the task name as a sender, so you can
  23. connect your handler to be called only when tasks with name `"tasks.add"`
  24. has been sent by providing the `sender` argument to
  25. :class:`~celery.utils.dispatch.signal.Signal.connect`:
  26. .. code-block:: python
  27. task_sent.connect(task_sent_handler, sender="tasks.add")
  28. .. _signal-ref:
  29. Signals
  30. =======
  31. Task Signals
  32. ------------
  33. .. signal:: task_sent
  34. task_sent
  35. ~~~~~~~~~
  36. Dispatched when a task has been sent to the broker.
  37. Note that this is executed in the client process, the one sending
  38. the task, not in the worker.
  39. Sender is the name of the task being sent.
  40. Provides arguments:
  41. * task_id
  42. Id of the task to be executed.
  43. * task
  44. The task being executed.
  45. * args
  46. the tasks positional arguments.
  47. * kwargs
  48. The tasks keyword arguments.
  49. * eta
  50. The time to execute the task.
  51. * taskset
  52. Id of the taskset this task is part of (if any).
  53. .. signal:: task_prerun
  54. task_prerun
  55. ~~~~~~~~~~~
  56. Dispatched before a task is executed.
  57. Sender is the task class being executed.
  58. Provides arguments:
  59. * task_id
  60. Id of the task to be executed.
  61. * task
  62. The task being executed.
  63. * args
  64. the tasks positional arguments.
  65. * kwargs
  66. The tasks keyword arguments.
  67. .. signal:: task_postrun
  68. task_postrun
  69. ~~~~~~~~~~~~
  70. Dispatched after a task has been executed.
  71. Sender is the task class executed.
  72. Provides arguments:
  73. * task_id
  74. Id of the task to be executed.
  75. * task
  76. The task being executed.
  77. * args
  78. The tasks positional arguments.
  79. * kwargs
  80. The tasks keyword arguments.
  81. * retval
  82. The return value of the task.
  83. .. signal:: task_failure
  84. task_failure
  85. ~~~~~~~~~~~~
  86. Dispatched when a task fails.
  87. Sender is the task class executed.
  88. Provides arguments:
  89. * task_id
  90. Id of the task.
  91. * exception
  92. Exception instance raised.
  93. * args
  94. Positional arguments the task was called with.
  95. * kwargs
  96. Keyword arguments the task was called with.
  97. * traceback
  98. Stack trace object.
  99. * einfo
  100. The :class:`celery.datastructures.ExceptionInfo` instance.
  101. Worker Signals
  102. --------------
  103. .. signal:: worker_init
  104. worker_init
  105. ~~~~~~~~~~~
  106. Dispatched before the worker is started.
  107. .. signal:: worker_ready
  108. worker_ready
  109. ~~~~~~~~~~~~
  110. Dispatched when the worker is ready to accept work.
  111. .. signal:: worker_process_init
  112. worker_process_init
  113. ~~~~~~~~~~~~~~~~~~~
  114. Dispatched by each new pool worker process when it starts.
  115. .. signal:: worker_shutdown
  116. worker_shutdown
  117. ~~~~~~~~~~~~~~~
  118. Dispatched when the worker is about to shut down.
  119. Celerybeat Signals
  120. ------------------
  121. .. signal:: beat_init
  122. beat_init
  123. ~~~~~~~~~
  124. Dispatched when celerybeat starts (either standalone or embedded).
  125. Sender is the :class:`celery.beat.Service` instance.
  126. .. signal:: beat_embedded_init
  127. beat_embedded_init
  128. ~~~~~~~~~~~~~~~~~~
  129. Dispatched in addition to the :signal:`beat_init` signal when celerybeat is
  130. started as an embedded process. Sender is the
  131. :class:`celery.beat.Service` instance.
  132. Eventlet Signals
  133. ----------------
  134. .. signal:: eventlet_pool_started
  135. eventlet_pool_started
  136. ~~~~~~~~~~~~~~~~~~~~~
  137. Sent when the eventlet pool has been started.
  138. Sender is the :class:`celery.concurrency.eventlet.TaskPool` instance.
  139. .. signal:: eventlet_pool_preshutdown
  140. eventlet_pool_preshutdown
  141. ~~~~~~~~~~~~~~~~~~~~~~~~~
  142. Sent when the worker shutdown, just before the eventlet pool
  143. is requested to wait for remaining workers.
  144. Sender is the :class:`celery.concurrency.eventlet.TaskPool` instance.
  145. .. signal:: eventlet_pool_postshutdown
  146. eventlet_pool_postshutdown
  147. ~~~~~~~~~~~~~~~~~~~~~~~~~~
  148. Sent when the pool has been joined and the worker is ready to shutdown.
  149. Sender is the :class:`celery.concurrency.eventlet.TaskPool` instance.
  150. .. signal:: eventlet_pool_apply
  151. eventlet_pool_apply
  152. ~~~~~~~~~~~~~~~~~~~
  153. Sent whenever a task is applied to the pool.
  154. Sender is the :class:`celery.concurrency.eventlet.TaskPool` instance.
  155. Provides arguments:
  156. * target
  157. The target function.
  158. * args
  159. Positional arguments.
  160. * kwargs
  161. Keyword arguments.
  162. Logging Signals
  163. ---------------
  164. .. signal:: setup_logging
  165. setup_logging
  166. ~~~~~~~~~~~~~
  167. Celery won't configure the loggers if this signal is connected,
  168. so you can use this to completely override the logging configuration
  169. with your own.
  170. If you would like to augment the logging configuration setup by
  171. Celery then you can use the :signal:`after_setup_logger` and
  172. :signal:`after_setup_task_logger` signals.
  173. Provides arguments:
  174. * loglevel
  175. The level of the logging object.
  176. * logfile
  177. The name of the logfile.
  178. * format
  179. The log format string.
  180. * colorize
  181. Specify if log messages are colored or not.
  182. .. signal:: after_setup_logger
  183. after_setup_logger
  184. ~~~~~~~~~~~~~~~~~~
  185. Sent after the setup of every global logger (not task loggers).
  186. Used to augment logging configuration.
  187. Provides arguments:
  188. * logger
  189. The logger object.
  190. * loglevel
  191. The level of the logging object.
  192. * logfile
  193. The name of the logfile.
  194. * format
  195. The log format string.
  196. * colorize
  197. Specify if log messages are colored or not.
  198. .. signal:: after_setup_task_logger
  199. after_setup_task_logger
  200. ~~~~~~~~~~~~~~~~~~~~~~~
  201. Sent after the setup of every single task logger.
  202. Used to augment logging configuration.
  203. Provides arguments:
  204. * logger
  205. The logger object.
  206. * loglevel
  207. The level of the logging object.
  208. * logfile
  209. The name of the logfile.
  210. * format
  211. The log format string.
  212. * colorize
  213. Specify if log messages are colored or not.
  214. """
  215. from __future__ import absolute_import
  216. from .utils.dispatch import Signal
  217. task_sent = Signal(providing_args=["task_id", "task",
  218. "args", "kwargs",
  219. "eta", "taskset"])
  220. task_prerun = Signal(providing_args=["task_id", "task",
  221. "args", "kwargs"])
  222. task_postrun = Signal(providing_args=["task_id", "task",
  223. "args", "kwargs", "retval"])
  224. task_failure = Signal(providing_args=["task_id", "exception",
  225. "args", "kwargs", "traceback",
  226. "einfo"])
  227. worker_init = Signal(providing_args=[])
  228. worker_process_init = Signal(providing_args=[])
  229. worker_ready = Signal(providing_args=[])
  230. worker_shutdown = Signal(providing_args=[])
  231. setup_logging = Signal(providing_args=["loglevel", "logfile",
  232. "format", "colorize"])
  233. after_setup_logger = Signal(providing_args=["logger", "loglevel", "logfile",
  234. "format", "colorize"])
  235. after_setup_task_logger = Signal(providing_args=["logger", "loglevel",
  236. "logfile", "format",
  237. "colorize"])
  238. beat_init = Signal(providing_args=[])
  239. beat_embedded_init = Signal(providing_args=[])
  240. eventlet_pool_started = Signal(providing_args=[])
  241. eventlet_pool_preshutdown = Signal(providing_args=[])
  242. eventlet_pool_postshutdown = Signal(providing_args=[])
  243. eventlet_pool_apply = Signal(providing_args=["target", "args", "kwargs"])