Move some of the ordered flow helper classes to utils.
Allow other flow types easier access to these helper classes by moving them to the shared utils file. Change-Id: I001754efc25b025e8070493d5861ffaedacafc7b
This commit is contained in:
@@ -17,11 +17,9 @@
|
||||
# under the License.
|
||||
|
||||
import abc
|
||||
import collections
|
||||
import copy
|
||||
import functools
|
||||
import logging
|
||||
import sys
|
||||
|
||||
from taskflow.openstack.common import excutils
|
||||
|
||||
@@ -32,32 +30,6 @@ from taskflow import utils
|
||||
LOG = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class FlowFailure(object):
|
||||
"""When a task failure occurs the following object will be given to revert
|
||||
and can be used to interrogate what caused the failure."""
|
||||
|
||||
def __init__(self, task, flow, exception):
|
||||
self.task = task
|
||||
self.flow = flow
|
||||
self.exc = exception
|
||||
self.exc_info = sys.exc_info()
|
||||
|
||||
|
||||
class RollbackTask(object):
|
||||
def __init__(self, context, task, result):
|
||||
self.task = task
|
||||
self.result = result
|
||||
self.context = context
|
||||
|
||||
def __str__(self):
|
||||
return str(self.task)
|
||||
|
||||
def __call__(self, cause):
|
||||
if ((hasattr(self.task, "revert") and
|
||||
isinstance(self.task.revert, collections.Callable))):
|
||||
self.task.revert(self.context, self.result, cause)
|
||||
|
||||
|
||||
class Flow(object):
|
||||
"""A set tasks that can be applied as one unit or rolled back as one
|
||||
unit using an ordered arrangements of said tasks where reversion is by
|
||||
@@ -149,7 +121,7 @@ class Flow(object):
|
||||
# Add the task to be rolled back *immediately* so that even if
|
||||
# the task fails while producing results it will be given a
|
||||
# chance to rollback.
|
||||
rb = RollbackTask(context, task, result=None)
|
||||
rb = utils.RollbackTask(context, task, result=None)
|
||||
self._accumulator.add(rb)
|
||||
if not simulate_run:
|
||||
inputs = self._fetch_task_inputs(task)
|
||||
@@ -191,7 +163,7 @@ class Flow(object):
|
||||
self.results.append((task, result_copy))
|
||||
self._on_task_finish(context, task, result_copy)
|
||||
except Exception as e:
|
||||
cause = FlowFailure(task, self, e)
|
||||
cause = utils.FlowFailure(task, self, e)
|
||||
with excutils.save_and_reraise_exception():
|
||||
try:
|
||||
self._on_task_error(context, task, e)
|
||||
|
||||
@@ -16,8 +16,10 @@
|
||||
# License for the specific language governing permissions and limitations
|
||||
# under the License.
|
||||
|
||||
import collections
|
||||
import contextlib
|
||||
import logging
|
||||
import sys
|
||||
import threading
|
||||
import time
|
||||
|
||||
@@ -66,6 +68,35 @@ def await(check_functor, timeout=None):
|
||||
return True
|
||||
|
||||
|
||||
class FlowFailure(object):
|
||||
"""When a task failure occurs the following object will be given to revert
|
||||
and can be used to interrogate what caused the failure."""
|
||||
|
||||
def __init__(self, task, flow, exception):
|
||||
self.task = task
|
||||
self.flow = flow
|
||||
self.exc = exception
|
||||
self.exc_info = sys.exc_info()
|
||||
|
||||
|
||||
class RollbackTask(object):
|
||||
"""A helper task that on being called will call the underlying callable
|
||||
tasks revert method (if said method exists)"""
|
||||
|
||||
def __init__(self, context, task, result):
|
||||
self.task = task
|
||||
self.result = result
|
||||
self.context = context
|
||||
|
||||
def __str__(self):
|
||||
return str(self.task)
|
||||
|
||||
def __call__(self, cause):
|
||||
if ((hasattr(self.task, "revert") and
|
||||
isinstance(self.task.revert, collections.Callable))):
|
||||
self.task.revert(self.context, self.result, cause)
|
||||
|
||||
|
||||
class RollbackAccumulator(object):
|
||||
"""A utility class that can help in organizing 'undo' like code
|
||||
so that said code be rolled back on failure (automatically or manually)
|
||||
|
||||
Reference in New Issue
Block a user