Development of an internal social media platform with personalised dashboards for students
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

states.py 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. __all__ = ['PENDING', 'RECEIVED', 'STARTED', 'SUCCESS', 'FAILURE',
  38. 'REVOKED', 'RETRY', 'IGNORED', 'READY_STATES', 'UNREADY_STATES',
  39. 'EXCEPTION_STATES', 'PROPAGATE_STATES', 'precedence', 'state']
  40. #: State precedence.
  41. #: None represents the precedence of an unknown state.
  42. #: Lower index means higher precedence.
  43. PRECEDENCE = ['SUCCESS',
  44. 'FAILURE',
  45. None,
  46. 'REVOKED',
  47. 'STARTED',
  48. 'RECEIVED',
  49. 'RETRY',
  50. 'PENDING']
  51. #: Hash lookup of PRECEDENCE to index
  52. PRECEDENCE_LOOKUP = dict(zip(PRECEDENCE, range(0, len(PRECEDENCE))))
  53. NONE_PRECEDENCE = PRECEDENCE_LOOKUP[None]
  54. def precedence(state):
  55. """Get the precedence index for state.
  56. Lower index means higher precedence.
  57. """
  58. try:
  59. return PRECEDENCE_LOOKUP[state]
  60. except KeyError:
  61. return NONE_PRECEDENCE
  62. class state(str):
  63. """State is a subclass of :class:`str`, implementing comparison
  64. methods adhering to state precedence rules::
  65. >>> from celery.states import state, PENDING, SUCCESS
  66. >>> state(PENDING) < state(SUCCESS)
  67. True
  68. Any custom state is considered to be lower than :state:`FAILURE` and
  69. :state:`SUCCESS`, but higher than any of the other built-in states::
  70. >>> state('PROGRESS') > state(STARTED)
  71. True
  72. >>> state('PROGRESS') > state('SUCCESS')
  73. False
  74. """
  75. def compare(self, other, fun):
  76. return fun(precedence(self), precedence(other))
  77. def __gt__(self, other):
  78. return precedence(self) < precedence(other)
  79. def __ge__(self, other):
  80. return precedence(self) <= precedence(other)
  81. def __lt__(self, other):
  82. return precedence(self) > precedence(other)
  83. def __le__(self, other):
  84. return precedence(self) >= precedence(other)
  85. #: Task state is unknown (assumed pending since you know the id).
  86. PENDING = 'PENDING'
  87. #: Task was received by a worker.
  88. RECEIVED = 'RECEIVED'
  89. #: Task was started by a worker (:setting:`CELERY_TRACK_STARTED`).
  90. STARTED = 'STARTED'
  91. #: Task succeeded
  92. SUCCESS = 'SUCCESS'
  93. #: Task failed
  94. FAILURE = 'FAILURE'
  95. #: Task was revoked.
  96. REVOKED = 'REVOKED'
  97. #: Task is waiting for retry.
  98. RETRY = 'RETRY'
  99. IGNORED = 'IGNORED'
  100. REJECTED = 'REJECTED'
  101. READY_STATES = frozenset([SUCCESS, FAILURE, REVOKED])
  102. UNREADY_STATES = frozenset([PENDING, RECEIVED, STARTED, RETRY])
  103. EXCEPTION_STATES = frozenset([RETRY, FAILURE, REVOKED])
  104. PROPAGATE_STATES = frozenset([FAILURE, REVOKED])
  105. ALL_STATES = frozenset([PENDING, RECEIVED, STARTED,
  106. SUCCESS, FAILURE, RETRY, REVOKED])