signals.py 7.8 KB

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