states.py 2.2 KB

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