From 96e6d971a02a345d081f98342c47b437db7773e2 Mon Sep 17 00:00:00 2001 From: Joshua Harlow Date: Fri, 9 Jan 2015 16:35:19 -0800 Subject: [PATCH] Send in the prior atom state on notification of a state change When a retry atom or a task atom changes state it is quite useful to know the prior state the atom was in; to make this easier pass that information along in the details about the state transition so that observers can inspect it if they desire to. This matches more closely with the flow notification which does include the prior state (under the details key 'old_state') so this also increases the uniformity of notifications in general. Change-Id: I7df1fcc60ba178198776ddaa2681caee5811c4ff --- taskflow/engines/action_engine/actions/retry.py | 11 +++++++---- taskflow/engines/action_engine/actions/task.py | 15 +++++++++------ 2 files changed, 16 insertions(+), 10 deletions(-) diff --git a/taskflow/engines/action_engine/actions/retry.py b/taskflow/engines/action_engine/actions/retry.py index 5f6eb90c..be933ee2 100644 --- a/taskflow/engines/action_engine/actions/retry.py +++ b/taskflow/engines/action_engine/actions/retry.py @@ -51,21 +51,24 @@ class RetryAction(object): return kwargs def change_state(self, retry, state, result=None): + old_state = self._storage.get_atom_state(retry.name) if state in SAVE_RESULT_STATES: self._storage.save(retry.name, result, state) elif state == states.REVERTED: self._storage.cleanup_retry_history(retry.name, state) else: - old_state = self._storage.get_atom_state(retry.name) if state == old_state: # NOTE(imelnikov): nothing really changed, so we should not # write anything to storage and run notifications return self._storage.set_atom_state(retry.name, state) retry_uuid = self._storage.get_atom_uuid(retry.name) - details = dict(retry_name=retry.name, - retry_uuid=retry_uuid, - result=result) + details = { + 'retry_name': retry.name, + 'retry_uuid': retry_uuid, + 'result': result, + 'old_state': old_state, + } self._notifier.notify(state, details) def execute(self, retry): diff --git a/taskflow/engines/action_engine/actions/task.py b/taskflow/engines/action_engine/actions/task.py index ccf450b5..fbdc0a8f 100644 --- a/taskflow/engines/action_engine/actions/task.py +++ b/taskflow/engines/action_engine/actions/task.py @@ -39,11 +39,10 @@ class TaskAction(object): def handles(atom): return isinstance(atom, task_atom.BaseTask) - def _is_identity_transition(self, state, task, progress): + def _is_identity_transition(self, old_state, state, task, progress): if state in SAVE_RESULT_STATES: # saving result is never identity transition return False - old_state = self._storage.get_atom_state(task.name) if state != old_state: # changing state is not identity transition by definition return False @@ -58,7 +57,8 @@ class TaskAction(object): return True def change_state(self, task, state, result=None, progress=None): - if self._is_identity_transition(state, task, progress): + old_state = self._storage.get_atom_state(task.name) + if self._is_identity_transition(old_state, state, task, progress): # NOTE(imelnikov): ignore identity transitions in order # to avoid extra write to storage backend and, what's # more important, extra notifications @@ -70,9 +70,12 @@ class TaskAction(object): if progress is not None: self._storage.set_task_progress(task.name, progress) task_uuid = self._storage.get_atom_uuid(task.name) - details = dict(task_name=task.name, - task_uuid=task_uuid, - result=result) + details = { + 'task_name': task.name, + 'task_uuid': task_uuid, + 'result': result, + 'old_state': old_state, + } self._notifier.notify(state, details) if progress is not None: task.update_progress(progress)