states.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. .. _statesets:
  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. >>> from celery.states import state, PENDING, SUCCESS
  60. >>> state(PENDING) < state(SUCCESS)
  61. True
  62. Any custom state is considered to be lower than :state:`FAILURE` and
  63. :state:`SUCCESS`, but higher than any of the other built-in states::
  64. >>> state('PROGRESS') > state(STARTED)
  65. True
  66. >>> state('PROGRESS') > state('SUCCESS')
  67. False
  68. """
  69. def compare(self, other, fun):
  70. return fun(precedence(self), precedence(other))
  71. def __gt__(self, other):
  72. return self.compare(other, lambda a, b: a < b)
  73. def __ge__(self, other):
  74. return self.compare(other, lambda a, b: a <= b)
  75. def __lt__(self, other):
  76. return self.compare(other, lambda a, b: a > b)
  77. def __le__(self, other):
  78. return self.compare(other, lambda a, b: a >= b)
  79. #: Task state is unknown (assumed pending since you know the id).
  80. PENDING = 'PENDING'
  81. #: Task was received by a worker.
  82. RECEIVED = 'RECEIVED'
  83. #: Task was started by a worker (:setting:`CELERY_TRACK_STARTED`).
  84. STARTED = 'STARTED'
  85. #: Task succeeded
  86. SUCCESS = 'SUCCESS'
  87. #: Task failed
  88. FAILURE = 'FAILURE'
  89. #: Task was revoked.
  90. REVOKED = 'REVOKED'
  91. #: Task is waiting for retry.
  92. RETRY = 'RETRY'
  93. IGNORED = 'IGNORED'
  94. READY_STATES = frozenset([SUCCESS, FAILURE, REVOKED])
  95. UNREADY_STATES = frozenset([PENDING, RECEIVED, STARTED, RETRY])
  96. EXCEPTION_STATES = frozenset([RETRY, FAILURE, REVOKED])
  97. PROPAGATE_STATES = frozenset([FAILURE, REVOKED])
  98. ALL_STATES = frozenset([PENDING, RECEIVED, STARTED,
  99. SUCCESS, FAILURE, RETRY, REVOKED])