states.py 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. # -*- coding: utf-8 -*-
  2. """
  3. celery.states
  4. =============
  5. Built-in Task States.
  6. .. _states:
  7. States
  8. ------
  9. See :ref:`task-states`.
  10. Sets
  11. ----
  12. .. state:: READY_STATES
  13. READY_STATES
  14. ~~~~~~~~~~~~
  15. Set of states meaning the task result is ready (has been executed).
  16. .. state:: UNREADY_STATES
  17. UNREADY_STATES
  18. ~~~~~~~~~~~~~~
  19. Set of states meaning the task result is not ready (has not been executed).
  20. .. state:: EXCEPTION_STATES
  21. EXCEPTION_STATES
  22. ~~~~~~~~~~~~~~~~
  23. Set of states meaning the task returned an exception.
  24. .. state:: PROPAGATE_STATES
  25. PROPAGATE_STATES
  26. ~~~~~~~~~~~~~~~~
  27. Set of exception states that should propagate exceptions to the user.
  28. .. state:: ALL_STATES
  29. ALL_STATES
  30. ~~~~~~~~~~
  31. Set of all possible states.
  32. Misc.
  33. -----
  34. """
  35. from __future__ import absolute_import
  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])