Manager responsible for consuming result messages.
| 343 | |
| 344 | |
| 345 | class BaseResultConsumer: |
| 346 | """Manager responsible for consuming result messages.""" |
| 347 | |
| 348 | #: Tuple of transport-layer exceptions that signal a lost connection. |
| 349 | #: Subclasses should override this with the appropriate exception types |
| 350 | #: so that :meth:`reconnect_on_error` can catch and recover from them. |
| 351 | _connection_errors = () |
| 352 | |
| 353 | def __init__(self, backend, app, accept, |
| 354 | pending_results, pending_messages): |
| 355 | self.backend = backend |
| 356 | self.app = app |
| 357 | self.accept = accept |
| 358 | self._pending_results = pending_results |
| 359 | self._pending_messages = pending_messages |
| 360 | self.on_message = None |
| 361 | self.buckets = WeakKeyDictionary() |
| 362 | self.drainer = drainers[detect_environment()](self) |
| 363 | |
| 364 | def start(self, initial_task_id, **kwargs): |
| 365 | raise NotImplementedError() |
| 366 | |
| 367 | @contextmanager |
| 368 | def reconnect_on_error(self): |
| 369 | """Context manager that catches connection errors and reconnects. |
| 370 | |
| 371 | Wraps a block of code so that any :attr:`_connection_errors` raised |
| 372 | inside it trigger a call to :meth:`_reconnect`. If reconnection |
| 373 | itself raises a connection error the consumer is considered |
| 374 | unrecoverable and a :exc:`RuntimeError` is raised to signal that |
| 375 | the Celery application must be restarted. |
| 376 | """ |
| 377 | try: |
| 378 | yield |
| 379 | except socket.timeout: |
| 380 | # socket.timeout (builtin TimeoutError) from drain_events(timeout=N) |
| 381 | # is normal polling behavior, not a connection error. |
| 382 | # Let it propagate as an expected polling timeout for the caller |
| 383 | # to handle. |
| 384 | raise |
| 385 | except self._connection_errors: |
| 386 | try: |
| 387 | self._reconnect() |
| 388 | except self._connection_errors as exc: |
| 389 | logger.critical(E_RETRY_LIMIT_EXCEEDED) |
| 390 | raise RuntimeError(E_RETRY_LIMIT_EXCEEDED) from exc |
| 391 | |
| 392 | def _reconnect(self): |
| 393 | """Re-establish the backend connection. |
| 394 | |
| 395 | Subclasses must override this method to perform the transport-specific |
| 396 | reconnection logic that should be executed when a connection error is |
| 397 | caught by :meth:`reconnect_on_error`. |
| 398 | """ |
| 399 | pass |
| 400 | |
| 401 | def stop(self): |
| 402 | pass |
no outgoing calls