/celery/exceptions.py

https://github.com/ionelmc/celery · Python · 162 lines · 73 code · 56 blank · 33 comment · 7 complexity · a48795a5b04fae9cc27de7b1c4b67438 MD5 · raw file

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