states.py 2.4 KB

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