states.py 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. "PENDING"]
  39. def precedence(state):
  40. """Get the precedence index for state.
  41. Lower index means higher precedence.
  42. """
  43. try:
  44. return PRECEDENCE.index(state)
  45. except ValueError:
  46. return PRECEDENCE.index(None)
  47. class state(str):
  48. """State is a subclass of :class:`str`, implementing comparison
  49. methods adhering to state precedence rules."""
  50. def compare(self, other, fun, default=False):
  51. return fun(precedence(self), precedence(other))
  52. def __gt__(self, other):
  53. return self.compare(other, lambda a, b: a < b, True)
  54. def __ge__(self, other):
  55. return self.compare(other, lambda a, b: a <= b, True)
  56. def __lt__(self, other):
  57. return self.compare(other, lambda a, b: a > b, False)
  58. def __le__(self, other):
  59. return self.compare(other, lambda a, b: a >= b, False)
  60. PENDING = "PENDING"
  61. RECEIVED = "RECEIVED"
  62. STARTED = "STARTED"
  63. SUCCESS = "SUCCESS"
  64. FAILURE = "FAILURE"
  65. REVOKED = "REVOKED"
  66. RETRY = "RETRY"
  67. READY_STATES = frozenset([SUCCESS, FAILURE, REVOKED])
  68. UNREADY_STATES = frozenset([PENDING, RECEIVED, STARTED, RETRY])
  69. EXCEPTION_STATES = frozenset([RETRY, FAILURE, REVOKED])
  70. PROPAGATE_STATES = frozenset([FAILURE, REVOKED])
  71. ALL_STATES = frozenset([PENDING, RECEIVED, STARTED,
  72. SUCCESS, FAILURE, RETRY, REVOKED])