__init__.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  1. # -*- coding: utf-8 -*-
  2. """
  3. celery.events
  4. ~~~~~~~~~~~~~
  5. Events is a stream of messages sent for certain actions occurring
  6. in the worker (and clients if :setting:`CELERY_SEND_TASK_SENT_EVENT`
  7. is enabled), used for monitoring purposes.
  8. """
  9. from __future__ import absolute_import
  10. import os
  11. import time
  12. import threading
  13. from collections import defaultdict, deque
  14. from contextlib import contextmanager
  15. from copy import copy
  16. from operator import itemgetter
  17. from kombu import Exchange, Queue, Producer
  18. from kombu.connection import maybe_channel
  19. from kombu.mixins import ConsumerMixin
  20. from kombu.utils import cached_property
  21. from celery.app import app_or_default
  22. from celery.five import items
  23. from celery.utils import anon_nodename, uuid
  24. from celery.utils.functional import dictfilter
  25. from celery.utils.timeutils import adjust_timestamp, utcoffset, maybe_s_to_ms
  26. __all__ = ['Events', 'Event', 'EventDispatcher', 'EventReceiver']
  27. event_exchange = Exchange('celeryev', type='topic')
  28. _TZGETTER = itemgetter('utcoffset', 'timestamp')
  29. CLIENT_CLOCK_SKEW = -1
  30. def get_exchange(conn):
  31. ex = copy(event_exchange)
  32. if conn.transport.driver_type == 'redis':
  33. # quick hack for Issue #436
  34. ex.type = 'fanout'
  35. return ex
  36. def Event(type, _fields=None, __dict__=dict, __now__=time.time, **fields):
  37. """Create an event.
  38. An event is a dictionary, the only required field is ``type``.
  39. A ``timestamp`` field will be set to the current time if not provided.
  40. """
  41. event = __dict__(_fields, **fields) if _fields else fields
  42. if 'timestamp' not in event:
  43. event.update(timestamp=__now__(), type=type)
  44. else:
  45. event['type'] = type
  46. return event
  47. def group_from(type):
  48. """Get the group part of an event type name.
  49. E.g.::
  50. >>> group_from('task-sent')
  51. 'task'
  52. >>> group_from('custom-my-event')
  53. 'custom'
  54. """
  55. return type.split('-', 1)[0]
  56. class EventDispatcher(object):
  57. """Dispatches event messages.
  58. :param connection: Connection to the broker.
  59. :keyword hostname: Hostname to identify ourselves as,
  60. by default uses the hostname returned by
  61. :func:`~celery.utils.anon_nodename`.
  62. :keyword groups: List of groups to send events for. :meth:`send` will
  63. ignore send requests to groups not in this list.
  64. If this is :const:`None`, all events will be sent. Example groups
  65. include ``"task"`` and ``"worker"``.
  66. :keyword enabled: Set to :const:`False` to not actually publish any events,
  67. making :meth:`send` a noop operation.
  68. :keyword channel: Can be used instead of `connection` to specify
  69. an exact channel to use when sending events.
  70. :keyword buffer_while_offline: If enabled events will be buffered
  71. while the connection is down. :meth:`flush` must be called
  72. as soon as the connection is re-established.
  73. You need to :meth:`close` this after use.
  74. """
  75. DISABLED_TRANSPORTS = {'sql'}
  76. app = None
  77. # set of callbacks to be called when :meth:`enabled`.
  78. on_enabled = None
  79. # set of callbacks to be called when :meth:`disabled`.
  80. on_disabled = None
  81. def __init__(self, connection=None, hostname=None, enabled=True,
  82. channel=None, buffer_while_offline=True, app=None,
  83. serializer=None, groups=None, delivery_mode=1,
  84. buffer_group=None, buffer_limit=24, on_send_buffered=None):
  85. self.app = app_or_default(app or self.app)
  86. self.connection = connection
  87. self.channel = channel
  88. self.hostname = hostname or anon_nodename()
  89. self.buffer_while_offline = buffer_while_offline
  90. self.buffer_group = buffer_group or frozenset()
  91. self.buffer_limit = buffer_limit
  92. self.on_send_buffered = on_send_buffered
  93. self._group_buffer = defaultdict(list)
  94. self.mutex = threading.Lock()
  95. self.producer = None
  96. self._outbound_buffer = deque()
  97. self.serializer = serializer or self.app.conf.CELERY_EVENT_SERIALIZER
  98. self.on_enabled = set()
  99. self.on_disabled = set()
  100. self.groups = set(groups or [])
  101. self.tzoffset = [-time.timezone, -time.altzone]
  102. self.clock = self.app.clock
  103. self.delivery_mode = delivery_mode
  104. if not connection and channel:
  105. self.connection = channel.connection.client
  106. self.enabled = enabled
  107. conninfo = self.connection or self.app.connection()
  108. self.exchange = get_exchange(conninfo)
  109. if conninfo.transport.driver_type in self.DISABLED_TRANSPORTS:
  110. self.enabled = False
  111. if self.enabled:
  112. self.enable()
  113. self.headers = {'hostname': self.hostname}
  114. self.pid = os.getpid()
  115. def __enter__(self):
  116. return self
  117. def __exit__(self, *exc_info):
  118. self.close()
  119. def enable(self):
  120. self.producer = Producer(self.channel or self.connection,
  121. exchange=self.exchange,
  122. serializer=self.serializer)
  123. self.enabled = True
  124. for callback in self.on_enabled:
  125. callback()
  126. def disable(self):
  127. if self.enabled:
  128. self.enabled = False
  129. self.close()
  130. for callback in self.on_disabled:
  131. callback()
  132. def publish(self, type, fields, producer,
  133. blind=False, Event=Event, **kwargs):
  134. """Publish event using a custom :class:`~kombu.Producer`
  135. instance.
  136. :param type: Event type name, with group separated by dash (`-`).
  137. :param fields: Dictionary of event fields, must be json serializable.
  138. :param producer: :class:`~kombu.Producer` instance to use,
  139. only the ``publish`` method will be called.
  140. :keyword retry: Retry in the event of connection failure.
  141. :keyword retry_policy: Dict of custom retry policy, see
  142. :meth:`~kombu.Connection.ensure`.
  143. :keyword blind: Don't set logical clock value (also do not forward
  144. the internal logical clock).
  145. :keyword Event: Event type used to create event,
  146. defaults to :func:`Event`.
  147. :keyword utcoffset: Function returning the current utcoffset in hours.
  148. """
  149. clock = None if blind else self.clock.forward()
  150. event = Event(type, hostname=self.hostname, utcoffset=utcoffset(),
  151. pid=self.pid, clock=clock, **fields)
  152. with self.mutex:
  153. return self._publish(event, producer,
  154. routing_key=type.replace('-', '.'), **kwargs)
  155. def _publish(self, event, producer, routing_key, retry=False,
  156. retry_policy=None, utcoffset=utcoffset):
  157. exchange = self.exchange
  158. try:
  159. producer.publish(
  160. event,
  161. routing_key=routing_key,
  162. exchange=exchange.name,
  163. retry=retry,
  164. retry_policy=retry_policy,
  165. declare=[exchange],
  166. serializer=self.serializer,
  167. headers=self.headers,
  168. delivery_mode=self.delivery_mode,
  169. )
  170. except Exception as exc:
  171. if not self.buffer_while_offline:
  172. raise
  173. self._outbound_buffer.append((event, routing_key, exc))
  174. def send(self, type, blind=False, utcoffset=utcoffset, **fields):
  175. """Send event.
  176. :param type: Event type name, with group separated by dash (`-`).
  177. :keyword retry: Retry in the event of connection failure.
  178. :keyword retry_policy: Dict of custom retry policy, see
  179. :meth:`~kombu.Connection.ensure`.
  180. :keyword blind: Don't set logical clock value (also do not forward
  181. the internal logical clock).
  182. :keyword Event: Event type used to create event,
  183. defaults to :func:`Event`.
  184. :keyword utcoffset: Function returning the current utcoffset in hours.
  185. :keyword \*\*fields: Event fields, must be json serializable.
  186. """
  187. if self.enabled:
  188. groups, group = self.groups, group_from(type)
  189. if groups and group not in groups:
  190. return
  191. if group in self.buffer_group:
  192. clock = self.clock.forward()
  193. event = Event(type, hostname=self.hostname,
  194. utcoffset=utcoffset(),
  195. pid=self.pid, clock=clock, **fields)
  196. buf = self._group_buffer[group]
  197. buf.append(event)
  198. if len(buf) >= self.buffer_limit:
  199. self.flush()
  200. elif self.on_send_buffered:
  201. self.on_send_buffered()
  202. else:
  203. return self.publish(type, fields, self.producer, blind)
  204. def flush(self, errors=True, groups=True):
  205. """Flushes the outbound buffer."""
  206. if errors:
  207. buf = list(self._outbound_buffer)
  208. try:
  209. with self.mutex:
  210. for event, routing_key, _ in buf:
  211. self._publish(event, self.producer, routing_key)
  212. finally:
  213. self._outbound_buffer.clear()
  214. if groups:
  215. with self.mutex:
  216. for group, events in items(self._group_buffer):
  217. self._publish(events, self.producer, '%s.multi' % group)
  218. events[:] = [] # list.clear
  219. def extend_buffer(self, other):
  220. """Copies the outbound buffer of another instance."""
  221. self._outbound_buffer.extend(other._outbound_buffer)
  222. def close(self):
  223. """Close the event dispatcher."""
  224. self.mutex.locked() and self.mutex.release()
  225. self.producer = None
  226. def _get_publisher(self):
  227. return self.producer
  228. def _set_publisher(self, producer):
  229. self.producer = producer
  230. publisher = property(_get_publisher, _set_publisher) # XXX compat
  231. class EventReceiver(ConsumerMixin):
  232. """Capture events.
  233. :param connection: Connection to the broker.
  234. :keyword handlers: Event handlers.
  235. :attr:`handlers` is a dict of event types and their handlers,
  236. the special handler `"*"` captures all events that doesn't have a
  237. handler.
  238. """
  239. app = None
  240. def __init__(self, channel, handlers=None, routing_key='#',
  241. node_id=None, app=None, queue_prefix='celeryev',
  242. accept=None, queue_ttl=None, queue_expires=None):
  243. self.app = app_or_default(app or self.app)
  244. self.channel = maybe_channel(channel)
  245. self.handlers = {} if handlers is None else handlers
  246. self.routing_key = routing_key
  247. self.node_id = node_id or uuid()
  248. self.queue_prefix = queue_prefix
  249. self.exchange = get_exchange(self.connection or self.app.connection())
  250. self.queue = Queue(
  251. '.'.join([self.queue_prefix, self.node_id]),
  252. exchange=self.exchange,
  253. routing_key=self.routing_key,
  254. auto_delete=True, durable=False,
  255. queue_arguments=self._get_queue_arguments(
  256. ttl=queue_ttl, expires=queue_expires,
  257. ),
  258. )
  259. self.clock = self.app.clock
  260. self.adjust_clock = self.clock.adjust
  261. self.forward_clock = self.clock.forward
  262. if accept is None:
  263. accept = {self.app.conf.CELERY_EVENT_SERIALIZER, 'json'}
  264. self.accept = accept
  265. def _get_queue_arguments(self, ttl=None, expires=None):
  266. conf = self.app.conf
  267. return dictfilter({
  268. 'x-message-ttl': maybe_s_to_ms(
  269. ttl if ttl is not None else conf.CELERY_EVENT_QUEUE_TTL,
  270. ),
  271. 'x-expires': maybe_s_to_ms(
  272. expires if expires is not None
  273. else conf.CELERY_EVENT_QUEUE_EXPIRES,
  274. ),
  275. })
  276. def process(self, type, event):
  277. """Process the received event by dispatching it to the appropriate
  278. handler."""
  279. handler = self.handlers.get(type) or self.handlers.get('*')
  280. handler and handler(event)
  281. def get_consumers(self, Consumer, channel):
  282. return [Consumer(queues=[self.queue],
  283. callbacks=[self._receive], no_ack=True,
  284. accept=self.accept)]
  285. def on_consume_ready(self, connection, channel, consumers,
  286. wakeup=True, **kwargs):
  287. if wakeup:
  288. self.wakeup_workers(channel=channel)
  289. def itercapture(self, limit=None, timeout=None, wakeup=True):
  290. return self.consume(limit=limit, timeout=timeout, wakeup=wakeup)
  291. def capture(self, limit=None, timeout=None, wakeup=True):
  292. """Open up a consumer capturing events.
  293. This has to run in the main process, and it will never
  294. stop unless forced via :exc:`KeyboardInterrupt` or :exc:`SystemExit`.
  295. """
  296. return list(self.consume(limit=limit, timeout=timeout, wakeup=wakeup))
  297. def wakeup_workers(self, channel=None):
  298. self.app.control.broadcast('heartbeat',
  299. connection=self.connection,
  300. channel=channel)
  301. def event_from_message(self, body, localize=True,
  302. now=time.time, tzfields=_TZGETTER,
  303. adjust_timestamp=adjust_timestamp,
  304. CLIENT_CLOCK_SKEW=CLIENT_CLOCK_SKEW):
  305. type = body['type']
  306. if type == 'task-sent':
  307. # clients never sync so cannot use their clock value
  308. _c = body['clock'] = (self.clock.value or 1) + CLIENT_CLOCK_SKEW
  309. self.adjust_clock(_c)
  310. else:
  311. try:
  312. clock = body['clock']
  313. except KeyError:
  314. body['clock'] = self.forward_clock()
  315. else:
  316. self.adjust_clock(clock)
  317. if localize:
  318. try:
  319. offset, timestamp = tzfields(body)
  320. except KeyError:
  321. pass
  322. else:
  323. body['timestamp'] = adjust_timestamp(timestamp, offset)
  324. body['local_received'] = now()
  325. return type, body
  326. def _receive(self, body, message, list=list, isinstance=isinstance):
  327. if isinstance(body, list): # 3.2: List of events
  328. process, from_message = self.process, self.event_from_message
  329. [process(*from_message(event)) for event in body]
  330. else:
  331. self.process(*self.event_from_message(body))
  332. @property
  333. def connection(self):
  334. return self.channel.connection.client if self.channel else None
  335. class Events(object):
  336. def __init__(self, app=None):
  337. self.app = app
  338. @cached_property
  339. def Receiver(self):
  340. return self.app.subclass_with_self(EventReceiver,
  341. reverse='events.Receiver')
  342. @cached_property
  343. def Dispatcher(self):
  344. return self.app.subclass_with_self(EventDispatcher,
  345. reverse='events.Dispatcher')
  346. @cached_property
  347. def State(self):
  348. return self.app.subclass_with_self('celery.events.state:State',
  349. reverse='events.State')
  350. @contextmanager
  351. def default_dispatcher(self, hostname=None, enabled=True,
  352. buffer_while_offline=False):
  353. with self.app.amqp.producer_pool.acquire(block=True) as prod:
  354. with self.Dispatcher(prod.connection, hostname, enabled,
  355. prod.channel, buffer_while_offline) as d:
  356. yield d