diff options
| author | Joshua Harlow <harlowja@gmail.com> | 2013-10-12 18:33:04 +0000 |
|---|---|---|
| committer | Joshua Harlow <harlowja@yahoo-inc.com> | 2013-10-14 12:22:23 -0700 |
| commit | ee1d583721934cf6cec5c4d443ef66a39f9f6141 (patch) | |
| tree | 609f6cda5dc8d340a756fccbbd9376a4cd1bc610 /taskflow/states.py | |
| parent | e69e0f7cd4f62f57db4dd24886f6693ac4ad185f (diff) | |
| download | taskflow-ee1d583721934cf6cec5c4d443ef66a39f9f6141.tar.gz | |
Add task state verification
Change-Id: I85abee2a1112ce1b9bb708cb9129f06c794f83b1
Diffstat (limited to 'taskflow/states.py')
| -rw-r--r-- | taskflow/states.py | 51 |
1 files changed, 50 insertions, 1 deletions
diff --git a/taskflow/states.py b/taskflow/states.py index 07dc6e6..1ffed0b 100644 --- a/taskflow/states.py +++ b/taskflow/states.py @@ -39,9 +39,10 @@ RESUMING = 'RESUMING' # Task states. FAILURE = FAILURE -SUCCESS = SUCCESS +PENDING = PENDING REVERTED = REVERTED REVERTING = REVERTING +SUCCESS = SUCCESS # TODO(harlowja): use when we can timeout tasks?? TIMED_OUT = 'TIMED_OUT' @@ -122,3 +123,51 @@ def check_flow_transition(old_state, new_state): return True raise exc.InvalidStateException( "Flow transition from %s to %s is not allowed" % pair) + + +## Task state transitions +# https://wiki.openstack.org/wiki/TaskFlow/States_of_Task_and_Flow#Task_States + +_ALLOWED_TASK_TRANSITIONS = [ + (PENDING, RUNNING), # run it! + + (RUNNING, SUCCESS), # the task finished successfully + (RUNNING, FAILURE), # the task failed + + (FAILURE, REVERTING), # task failed, do cleanup now + (SUCCESS, REVERTING), # some other task failed, do cleanup now + + (REVERTING, REVERTED), # revert done + (REVERTING, FAILURE), # revert failed + + (REVERTED, PENDING), # try again +] +_ALLOWED_TASK_TRANSITIONS.extend( + # NOTE(harlowja): the task was 'killed' while in one of the below 'a' + # states and it is permissible to let the task to start running or + # reverting immediately + (a, b) + for a in (REVERTED, REVERTING, RUNNING) + for b in (RUNNING, REVERTING) + if a != b +) +_ALLOWED_TASK_TRANSITIONS = frozenset(_ALLOWED_TASK_TRANSITIONS) + + +def check_task_transition(old_state, new_state): + """Check that task can transition from old_state to new_state. + + If transition can be performed, it returns True. If transition + should be ignored, it returns False. If transition is not + valid, it raises InvalidStateException. + """ + if old_state == new_state: + return False + pair = (old_state, new_state) + if pair in _ALLOWED_TASK_TRANSITIONS: + return True + # TODO(harlowja): Should we check/allow for 3rd party states to be + # triggered during RUNNING by having a concept of a sub-state that we also + # verify against?? + raise exc.InvalidStateException( + "Task transition from %s to %s is not allowed" % pair) |
