Store results by add() uuid instead of in array format.

Instead of storing the results in the previous linear
format, just store the results indexed by the uuid
returned from the add() method instead. This allows
for easier access to individual task result as well
as makes it easier to support non-linear result
insertation.

Change-Id: Id7461cb74eb1a380b05cf23643e7120df5682224
This commit is contained in:
Joshua Harlow
2013-06-28 19:18:48 -07:00
parent e8e60e884f
commit ade53f7229
2 changed files with 18 additions and 4 deletions

View File

@@ -54,8 +54,9 @@ class Flow(base.Flow):
# the contract we have with tasks that they will be given the value
# they returned if reversion is triggered.
self.result_fetcher = None
# Tasks results are stored here...
self.results = []
# Tasks results are stored here. Lookup is by the uuid that was
# returned from the add function.
self.results = {}
# The last index in the order we left off at before being
# interrupted (or failing).
self._left_off_at = 0
@@ -173,7 +174,7 @@ class Flow(base.Flow):
runner.result = result
# Alter the index we have ran at.
self._left_off_at += 1
self.results.append((runner.task, copy.deepcopy(result)))
self.results[runner.uuid] = copy.deepcopy(result)
self._on_task_finish(context, runner.task, result)
except Exception as e:
cause = utils.FlowFailure(runner.task, self, e)
@@ -225,7 +226,7 @@ class Flow(base.Flow):
@decorators.locked
def reset(self):
super(Flow, self).reset()
self.results = []
self.results = {}
self.result_fetcher = None
self._accumulator.reset()
self._left_off_at = 0

View File

@@ -57,6 +57,19 @@ class LinearFlowTest(unittest2.TestCase):
return do_interrupt
def test_result_access(self):
wf = lw.Flow("the-test-action")
@decorators.task
def do_apply1(context):
return [1, 2]
result_id = wf.add(do_apply1)
ctx = {}
wf.run(ctx)
self.assertTrue(result_id in wf.results)
self.assertEquals([1, 2], wf.results[result_id])
def test_functor_flow(self):
wf = lw.Flow("the-test-action")