exceptions.py 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. # -*- coding: utf-8 -*-
  2. """Celery error types.
  3. Error Hierarchy
  4. ===============
  5. - :exc:`Exception`
  6. - :exc:`celery.exceptions.CeleryError`
  7. - :exc:`~celery.exceptions.ImproperlyConfigured`
  8. - :exc:`~celery.exceptions.SecurityError`
  9. - :exc:`~celery.exceptions.TaskPredicate`
  10. - :exc:`~celery.exceptions.Ignore`
  11. - :exc:`~celery.exceptions.Reject`
  12. - :exc:`~celery.exceptions.Retry`
  13. - :exc:`~celery.exceptions.TaskError`
  14. - :exc:`~celery.exceptions.QueueNotFound`
  15. - :exc:`~celery.exceptions.IncompleteStream`
  16. - :exc:`~celery.exceptions.NotRegistered`
  17. - :exc:`~celery.exceptions.AlreadyRegistered`
  18. - :exc:`~celery.exceptions.TimeoutError`
  19. - :exc:`~celery.exceptions.MaxRetriesExceededError`
  20. - :exc:`~celery.exceptions.TaskRevokedError`
  21. - :exc:`~celery.exceptions.InvalidTaskError`
  22. - :exc:`~celery.exceptions.ChordError`
  23. - :class:`kombu.exceptions.KombuError`
  24. - :exc:`~celery.exceptions.OperationalError`
  25. Raised when a transport connection error occurs while
  26. sending a message (be it a task, remote control command error).
  27. .. note::
  28. This exception does not inherit from
  29. :exc:`~celery.exceptions.CeleryError`.
  30. - **billiard errors** (prefork pool)
  31. - :exc:`~celery.exceptions.SoftTimeLimitExceeded`
  32. - :exc:`~celery.exceptions.TimeLimitExceeded`
  33. - :exc:`~celery.exceptions.WorkerLostError`
  34. - :exc:`~celery.exceptions.Terminated`
  35. - :class:`UserWarning`
  36. - :class:`~celery.exceptions.CeleryWarning`
  37. - :class:`~celery.exceptions.AlwaysEagerIgnored`
  38. - :class:`~celery.exceptions.DuplicateNodenameWarning`
  39. - :class:`~celery.exceptions.FixupWarning`
  40. - :class:`~celery.exceptions.NotConfigured`
  41. - :exc:`BaseException`
  42. - :exc:`SystemExit`
  43. - :exc:`~celery.exceptions.WorkerTerminate`
  44. - :exc:`~celery.exceptions.WorkerShutdown`
  45. """
  46. from __future__ import absolute_import, unicode_literals
  47. import numbers
  48. from .five import python_2_unicode_compatible, string_t
  49. from billiard.exceptions import (
  50. SoftTimeLimitExceeded, TimeLimitExceeded, WorkerLostError, Terminated,
  51. )
  52. from kombu.exceptions import OperationalError
  53. __all__ = [
  54. # Warnings
  55. 'CeleryWarning',
  56. 'AlwaysEagerIgnored', 'DuplicateNodenameWarning',
  57. 'FixupWarning', 'NotConfigured',
  58. # Core errors
  59. 'CeleryError',
  60. 'ImproperlyConfigured', 'SecurityError',
  61. # Kombu (messaging) errors.
  62. 'OperationalError',
  63. # Task semi-predicates
  64. 'TaskPredicate', 'Ignore', 'Reject', 'Retry',
  65. # Task related errors.
  66. 'TaskError', 'QueueNotFound', 'IncompleteStream',
  67. 'NotRegistered', 'AlreadyRegistered', 'TimeoutError',
  68. 'MaxRetriesExceededError', 'TaskRevokedError',
  69. 'InvalidTaskError', 'ChordError',
  70. # Billiard task errors.
  71. 'SoftTimeLimitExceeded', 'TimeLimitExceeded',
  72. 'WorkerLostError', 'Terminated',
  73. # Deprecation warnings (forcing Python to emit them).
  74. 'CPendingDeprecationWarning', 'CDeprecationWarning',
  75. # Worker shutdown semi-predicates (inherits from SystemExit).
  76. 'WorkerShutdown', 'WorkerTerminate',
  77. ]
  78. UNREGISTERED_FMT = """\
  79. Task of kind {0} never registered, please make sure it's imported.\
  80. """
  81. class CeleryWarning(UserWarning):
  82. """Base class for all Celery warnings."""
  83. class AlwaysEagerIgnored(CeleryWarning):
  84. """send_task ignores :setting:`task_always_eager` option."""
  85. class DuplicateNodenameWarning(CeleryWarning):
  86. """Multiple workers are using the same nodename."""
  87. class FixupWarning(CeleryWarning):
  88. """Fixup related warning."""
  89. class NotConfigured(CeleryWarning):
  90. """Celery hasn't been configured, as no config module has been found."""
  91. class CeleryError(Exception):
  92. """Base class for all Celery errors."""
  93. class TaskPredicate(CeleryError):
  94. """Base class for task-related semi-predicates."""
  95. @python_2_unicode_compatible
  96. class Retry(TaskPredicate):
  97. """The task is to be retried later."""
  98. #: Optional message describing context of retry.
  99. message = None
  100. #: Exception (if any) that caused the retry to happen.
  101. exc = None
  102. #: Time of retry (ETA), either :class:`numbers.Real` or
  103. #: :class:`~datetime.datetime`.
  104. when = None
  105. def __init__(self, message=None, exc=None, when=None, **kwargs):
  106. from kombu.utils.encoding import safe_repr
  107. self.message = message
  108. if isinstance(exc, string_t):
  109. self.exc, self.excs = None, exc
  110. else:
  111. self.exc, self.excs = exc, safe_repr(exc) if exc else None
  112. self.when = when
  113. super(Retry, self).__init__(self, exc, when, **kwargs)
  114. def humanize(self):
  115. if isinstance(self.when, numbers.Number):
  116. return 'in {0.when}s'.format(self)
  117. return 'at {0.when}'.format(self)
  118. def __str__(self):
  119. if self.message:
  120. return self.message
  121. if self.excs:
  122. return 'Retry {0}: {1}'.format(self.humanize(), self.excs)
  123. return 'Retry {0}'.format(self.humanize())
  124. def __reduce__(self):
  125. return self.__class__, (self.message, self.excs, self.when)
  126. RetryTaskError = Retry # noqa: E305 XXX compat
  127. class Ignore(TaskPredicate):
  128. """A task can raise this to ignore doing state updates."""
  129. @python_2_unicode_compatible
  130. class Reject(TaskPredicate):
  131. """A task can raise this if it wants to reject/re-queue the message."""
  132. def __init__(self, reason=None, requeue=False):
  133. self.reason = reason
  134. self.requeue = requeue
  135. super(Reject, self).__init__(reason, requeue)
  136. def __repr__(self):
  137. return 'reject requeue=%s: %s' % (self.requeue, self.reason)
  138. class ImproperlyConfigured(CeleryError):
  139. """Celery is somehow improperly configured."""
  140. class SecurityError(CeleryError):
  141. """Security related exception."""
  142. class TaskError(CeleryError):
  143. """Task related errors."""
  144. class QueueNotFound(KeyError, TaskError):
  145. """Task routed to a queue not in ``conf.queues``."""
  146. class IncompleteStream(TaskError):
  147. """Found the end of a stream of data, but the data isn't complete."""
  148. @python_2_unicode_compatible
  149. class NotRegistered(KeyError, TaskError):
  150. """The task ain't registered."""
  151. def __repr__(self):
  152. return UNREGISTERED_FMT.format(self)
  153. class AlreadyRegistered(TaskError):
  154. """The task is already registered."""
  155. # XXX Unused
  156. class TimeoutError(TaskError):
  157. """The operation timed out."""
  158. class MaxRetriesExceededError(TaskError):
  159. """The tasks max restart limit has been exceeded."""
  160. class TaskRevokedError(TaskError):
  161. """The task has been revoked, so no result available."""
  162. class InvalidTaskError(TaskError):
  163. """The task has invalid data or ain't properly constructed."""
  164. class ChordError(TaskError):
  165. """A task part of the chord raised an exception."""
  166. class CPendingDeprecationWarning(PendingDeprecationWarning):
  167. """Warning of pending deprecation."""
  168. class CDeprecationWarning(DeprecationWarning):
  169. """Warning of deprecation."""
  170. class WorkerTerminate(SystemExit):
  171. """Signals that the worker should terminate immediately."""
  172. SystemTerminate = WorkerTerminate # noqa: E305 XXX compat
  173. class WorkerShutdown(SystemExit):
  174. """Signals that the worker should perform a warm shutdown."""