states.py 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. """
  2. celery.states
  3. =============
  4. Built-in Task States.
  5. :copyright: (c) 2009 - 2011 by Ask Solem.
  6. :license: BSD, see LICENSE for more details.
  7. .. _states:
  8. States
  9. ------
  10. See :ref:`task-states`.
  11. Sets
  12. ----
  13. .. state:: READY_STATES
  14. READY_STATES
  15. ~~~~~~~~~~~~
  16. Set of states meaning the task result is ready (has been executed).
  17. .. state:: UNREADY_STATES
  18. UNREADY_STATES
  19. ~~~~~~~~~~~~~~
  20. Set of states meaning the task result is not ready (has not been executed).
  21. .. state:: EXCEPTION_STATES
  22. EXCEPTION_STATES
  23. ~~~~~~~~~~~~~~~~
  24. Set of states meaning the task returned an exception.
  25. .. state:: PROPAGATE_STATES
  26. PROPAGATE_STATES
  27. ~~~~~~~~~~~~~~~~
  28. Set of exception states that should propagate exceptions to the user.
  29. .. state:: ALL_STATES
  30. ALL_STATES
  31. ~~~~~~~~~~
  32. Set of all possible states.
  33. Misc.
  34. -----
  35. """
  36. #: State precedence.
  37. #: None represents the precedence of an unknown state.
  38. #: Lower index means higher precedence.
  39. PRECEDENCE = ["SUCCESS",
  40. "FAILURE",
  41. None,
  42. "REVOKED",
  43. "STARTED",
  44. "RECEIVED",
  45. "RETRY",
  46. "PENDING"]
  47. def precedence(state):
  48. """Get the precedence index for state.
  49. Lower index means higher precedence.
  50. """
  51. try:
  52. return PRECEDENCE.index(state)
  53. except ValueError:
  54. return PRECEDENCE.index(None)
  55. class state(str):
  56. """State is a subclass of :class:`str`, implementing comparison
  57. methods adhering to state precedence rules."""
  58. def compare(self, other, fun, default=False):
  59. return fun(precedence(self), precedence(other))
  60. def __gt__(self, other):
  61. return self.compare(other, lambda a, b: a < b, True)
  62. def __ge__(self, other):
  63. return self.compare(other, lambda a, b: a <= b, True)
  64. def __lt__(self, other):
  65. return self.compare(other, lambda a, b: a > b, False)
  66. def __le__(self, other):
  67. return self.compare(other, lambda a, b: a >= b, False)
  68. PENDING = "PENDING"
  69. RECEIVED = "RECEIVED"
  70. STARTED = "STARTED"
  71. SUCCESS = "SUCCESS"
  72. FAILURE = "FAILURE"
  73. REVOKED = "REVOKED"
  74. RETRY = "RETRY"
  75. READY_STATES = frozenset([SUCCESS, FAILURE, REVOKED])
  76. UNREADY_STATES = frozenset([PENDING, RECEIVED, STARTED, RETRY])
  77. EXCEPTION_STATES = frozenset([RETRY, FAILURE, REVOKED])
  78. PROPAGATE_STATES = frozenset([FAILURE, REVOKED])
  79. ALL_STATES = frozenset([PENDING, RECEIVED, STARTED,
  80. SUCCESS, FAILURE, RETRY, REVOKED])