exceptions.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. # -*- coding: utf-8 -*-
  2. """
  3. celery.exceptions
  4. ~~~~~~~~~~~~~~~~~
  5. This module contains all exceptions used by the Celery API.
  6. """
  7. from __future__ import absolute_import
  8. import numbers
  9. from .five import string_t
  10. from billiard.exceptions import ( # noqa
  11. SoftTimeLimitExceeded, TimeLimitExceeded, WorkerLostError, Terminated,
  12. )
  13. __all__ = [
  14. 'CeleryError', 'CeleryWarning', 'TaskPredicate',
  15. 'SecurityError', 'Ignore', 'QueueNotFound',
  16. 'WorkerShutdown', 'WorkerTerminate',
  17. 'ImproperlyConfigured', 'NotRegistered', 'AlreadyRegistered',
  18. 'TimeoutError', 'MaxRetriesExceededError', 'Retry', 'Reject',
  19. 'TaskRevokedError', 'NotConfigured', 'AlwaysEagerIgnored',
  20. 'InvalidTaskError', 'ChordError', 'CPendingDeprecationWarning',
  21. 'CDeprecationWarning', 'FixupWarning', 'DuplicateNodenameWarning',
  22. 'SoftTimeLimitExceeded', 'TimeLimitExceeded', 'WorkerLostError',
  23. 'Terminated', 'IncompleteStream'
  24. ]
  25. UNREGISTERED_FMT = """\
  26. Task of kind {0} is not registered, please make sure it's imported.\
  27. """
  28. class CeleryError(Exception):
  29. pass
  30. class CeleryWarning(UserWarning):
  31. pass
  32. class SecurityError(CeleryError):
  33. """Security related exceptions.
  34. Handle with care.
  35. """
  36. class TaskPredicate(CeleryError):
  37. pass
  38. class Retry(TaskPredicate):
  39. """The task is to be retried later."""
  40. #: Optional message describing context of retry.
  41. message = None
  42. #: Exception (if any) that caused the retry to happen.
  43. exc = None
  44. #: Time of retry (ETA), either :class:`numbers.Real` or
  45. #: :class:`~datetime.datetime`.
  46. when = None
  47. def __init__(self, message=None, exc=None, when=None, **kwargs):
  48. from kombu.utils.encoding import safe_repr
  49. self.message = message
  50. if isinstance(exc, string_t):
  51. self.exc, self.excs = None, exc
  52. else:
  53. self.exc, self.excs = exc, safe_repr(exc) if exc else None
  54. self.when = when
  55. Exception.__init__(self, exc, when, **kwargs)
  56. def humanize(self):
  57. if isinstance(self.when, numbers.Real):
  58. return 'in {0.when}s'.format(self)
  59. return 'at {0.when}'.format(self)
  60. def __str__(self):
  61. if self.message:
  62. return self.message
  63. if self.excs:
  64. return 'Retry {0}: {1}'.format(self.humanize(), self.excs)
  65. return 'Retry {0}'.format(self.humanize())
  66. def __reduce__(self):
  67. return self.__class__, (self.message, self.excs, self.when)
  68. RetryTaskError = Retry # XXX compat
  69. class Ignore(TaskPredicate):
  70. """A task can raise this to ignore doing state updates."""
  71. class Reject(TaskPredicate):
  72. """A task can raise this if it wants to reject/requeue the message."""
  73. def __init__(self, reason=None, requeue=False):
  74. self.reason = reason
  75. self.requeue = requeue
  76. super(Reject, self).__init__(reason, requeue)
  77. def __repr__(self):
  78. return 'reject requeue=%s: %s' % (self.requeue, self.reason)
  79. class WorkerTerminate(SystemExit):
  80. """Signals that the worker should terminate immediately."""
  81. SystemTerminate = WorkerTerminate # XXX compat
  82. class WorkerShutdown(SystemExit):
  83. """Signals that the worker should perform a warm shutdown."""
  84. class QueueNotFound(KeyError):
  85. """Task routed to a queue not in ``conf.queues``."""
  86. class ImproperlyConfigured(ImportError):
  87. """Celery is somehow improperly configured."""
  88. class NotRegistered(KeyError, CeleryError):
  89. """The task is not registered."""
  90. def __repr__(self):
  91. return UNREGISTERED_FMT.format(self)
  92. class AlreadyRegistered(CeleryError):
  93. """The task is already registered."""
  94. class TimeoutError(CeleryError):
  95. """The operation timed out."""
  96. class MaxRetriesExceededError(CeleryError):
  97. """The tasks max restart limit has been exceeded."""
  98. class TaskRevokedError(CeleryError):
  99. """The task has been revoked, so no result available."""
  100. class NotConfigured(CeleryWarning):
  101. """Celery has not been configured, as no config module has been found."""
  102. class AlwaysEagerIgnored(CeleryWarning):
  103. """send_task ignores :setting:`task_always_eager` option"""
  104. class InvalidTaskError(CeleryError):
  105. """The task has invalid data or is not properly constructed."""
  106. class IncompleteStream(CeleryError):
  107. """Found the end of a stream of data, but the data is not yet complete."""
  108. class ChordError(CeleryError):
  109. """A task part of the chord raised an exception."""
  110. class CPendingDeprecationWarning(PendingDeprecationWarning):
  111. pass
  112. class CDeprecationWarning(DeprecationWarning):
  113. pass
  114. class FixupWarning(CeleryWarning):
  115. pass
  116. class DuplicateNodenameWarning(CeleryWarning):
  117. """Multiple workers are using the same nodename."""