| 1 |
# Copyright (C) 2012-2013 Yahoo! Inc. All Rights Reserved. |
| 2 |
# |
| 3 |
# Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 4 |
# not use this file except in compliance with the License. You may obtain |
| 5 |
# a copy of the License at |
| 6 |
# |
| 7 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
# |
| 9 |
# Unless required by applicable law or agreed to in writing, software |
| 10 |
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 11 |
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 12 |
# License for the specific language governing permissions and limitations |
| 13 |
# under the License. |
| 14 |
|
| 15 |
from taskflow.engines.action_engine.actions import base |
| 16 |
from taskflow import retry as retry_atom |
| 17 |
from taskflow import states |
| 18 |
from taskflow.types import failure |
| 19 |
|
| 20 |
|
| 21 |
class RetryAction(base.Action): |
| 22 |
"""An action that handles executing, state changes, ... of retry atoms.""" |
| 23 |
|
| 24 |
def __init__(self, storage, notifier, retry_executor): |
| 25 |
super().__init__(storage, notifier) |
| 26 |
self._retry_executor = retry_executor |
| 27 |
|
| 28 |
def _get_retry_args(self, retry, revert=False, addons=None): |
| 29 |
if revert: |
| 30 |
arguments = self._storage.fetch_mapped_args( |
| 31 |
retry.revert_rebind, |
| 32 |
atom_name=retry.name, |
| 33 |
optional_args=retry.revert_optional |
| 34 |
) |
| 35 |
else: |
| 36 |
arguments = self._storage.fetch_mapped_args( |
| 37 |
retry.rebind, |
| 38 |
atom_name=retry.name, |
| 39 |
optional_args=retry.optional |
| 40 |
) |
| 41 |
history = self._storage.get_retry_history(retry.name) |
| 42 |
arguments[retry_atom.EXECUTE_REVERT_HISTORY] = history |
| 43 |
if addons: |
| 44 |
arguments.update(addons) |
| 45 |
return arguments |
| 46 |
|
| 47 |
def change_state(self, retry, state, result=base.Action.NO_RESULT): |
| 48 |
old_state = self._storage.get_atom_state(retry.name) |
| 49 |
if state in self.SAVE_RESULT_STATES: |
| 50 |
save_result = None |
| 51 |
if result is not self.NO_RESULT: |
| 52 |
save_result = result |
| 53 |
self._storage.save(retry.name, save_result, state) |
| 54 |
# TODO(harlowja): combine this with the save to avoid a call |
| 55 |
# back into the persistence layer... |
| 56 |
if state == states.REVERTED: |
| 57 |
self._storage.cleanup_retry_history(retry.name, state) |
| 58 |
else: |
| 59 |
if state == old_state: |
| 60 |
# NOTE(imelnikov): nothing really changed, so we should not |
| 61 |
# write anything to storage and run notifications. |
| 62 |
return |
| 63 |
self._storage.set_atom_state(retry.name, state) |
| 64 |
retry_uuid = self._storage.get_atom_uuid(retry.name) |
| 65 |
details = { |
| 66 |
'retry_name': retry.name, |
| 67 |
'retry_uuid': retry_uuid, |
| 68 |
'old_state': old_state, |
| 69 |
} |
| 70 |
if result is not self.NO_RESULT: |
| 71 |
details['result'] = result |
| 72 |
self._notifier.notify(state, details) |
| 73 |
|
| 74 |
def schedule_execution(self, retry): |
| 75 |
self.change_state(retry, states.RUNNING) |
| 76 |
return self._retry_executor.execute_retry( |
| 77 |
retry, self._get_retry_args(retry)) |
| 78 |
|
| 79 |
def complete_reversion(self, retry, result): |
| 80 |
if isinstance(result, failure.Failure): |
| 81 |
self.change_state(retry, states.REVERT_FAILURE, result=result) |
| 82 |
else: |
| 83 |
self.change_state(retry, states.REVERTED, result=result) |
| 84 |
|
| 85 |
def complete_execution(self, retry, result): |
| 86 |
if isinstance(result, failure.Failure): |
| 87 |
self.change_state(retry, states.FAILURE, result=result) |
| 88 |
else: |
| 89 |
self.change_state(retry, states.SUCCESS, result=result) |
| 90 |
|
| 91 |
def schedule_reversion(self, retry): |
| 92 |
self.change_state(retry, states.REVERTING) |
| 93 |
arg_addons = { |
| 94 |
retry_atom.REVERT_FLOW_FAILURES: self._storage.get_failures(), |
| 95 |
} |
| 96 |
return self._retry_executor.revert_retry( |
| 97 |
retry, self._get_retry_args(retry, addons=arg_addons, revert=True)) |
| 98 |
|
| 99 |
def on_failure(self, retry, atom, last_failure): |
| 100 |
self._storage.save_retry_failure(retry.name, atom.name, last_failure) |
| 101 |
arguments = self._get_retry_args(retry) |
| 102 |
return retry.on_failure(**arguments) |