Merge "Add state info for synchronous actions run from CLI"
This commit is contained in:
commit
17671f2857
@ -200,7 +200,7 @@ class PythonAction(Action):
|
||||
else:
|
||||
self.action_ex.state = states.ERROR
|
||||
|
||||
self.action_ex.output = self._prepare_output(result)
|
||||
self.action_ex.output = self._prepare_output(result).to_dict()
|
||||
self.action_ex.accepted = True
|
||||
|
||||
self._log_result(prev_state, result)
|
||||
@ -294,10 +294,9 @@ class PythonAction(Action):
|
||||
def _prepare_output(self, result):
|
||||
"""Template method to do manipulations with action result.
|
||||
|
||||
Python action just wraps action result into dict that can
|
||||
be stored in DB.
|
||||
Python action doesn't do anything specific with result.
|
||||
"""
|
||||
return _get_action_output(result) if result else None
|
||||
return result
|
||||
|
||||
def _prepare_runtime_context(self, index, safe_rerun):
|
||||
"""Template method to prepare action runtime context.
|
||||
@ -389,7 +388,7 @@ class AdHocAction(PythonAction):
|
||||
error=result.error
|
||||
)
|
||||
|
||||
return _get_action_output(result) if result else None
|
||||
return result
|
||||
|
||||
def _prepare_runtime_context(self, index, safe_rerun):
|
||||
ctx = super(AdHocAction, self)._prepare_runtime_context(
|
||||
@ -515,20 +514,7 @@ def _run_existing_action(action_ex_id, target):
|
||||
safe_rerun=action_ex.runtime_context.get('safe_rerun', False)
|
||||
)
|
||||
|
||||
return _get_action_output(result) if result else None
|
||||
|
||||
|
||||
def _get_action_output(result):
|
||||
"""Returns action output.
|
||||
|
||||
:param result: ActionResult instance or ActionResult dict
|
||||
:return: dict containing result.
|
||||
"""
|
||||
if isinstance(result, dict):
|
||||
result = wf_utils.Result(result.get('data'), result.get('error'))
|
||||
|
||||
return ({'result': result.data}
|
||||
if result.is_success() else {'result': result.error})
|
||||
return result.to_dict() if result else None
|
||||
|
||||
|
||||
def resolve_action_definition(action_spec_name, wf_name=None,
|
||||
|
@ -24,6 +24,7 @@ from mistral.engine import action_handler
|
||||
from mistral.engine import base
|
||||
from mistral.engine import workflow_handler as wf_handler
|
||||
from mistral import utils as u
|
||||
from mistral.workflow import states
|
||||
|
||||
LOG = logging.getLogger(__name__)
|
||||
|
||||
@ -71,13 +72,16 @@ class DefaultEngine(base.Engine, coordination.Service):
|
||||
|
||||
output = action.run(action_input, target, save=save)
|
||||
|
||||
state = states.SUCCESS if output.is_success() else states.ERROR
|
||||
|
||||
# Action execution is not created but we need to return similar
|
||||
# object to a client anyway.
|
||||
return db_models.ActionExecution(
|
||||
name=action_name,
|
||||
description=description,
|
||||
input=action_input,
|
||||
output=output
|
||||
output=output.to_dict(),
|
||||
state=state
|
||||
)
|
||||
|
||||
@u.log_exec(LOG)
|
||||
|
@ -96,6 +96,7 @@ class RunActionEngineTest(base.EngineTestCase):
|
||||
action_ex = self.engine.start_action('std.echo', {'output': 'Hello!'})
|
||||
|
||||
self.assertEqual('Hello!', action_ex.output['result'])
|
||||
self.assertEqual(states.SUCCESS, action_ex.state)
|
||||
|
||||
@mock.patch.object(
|
||||
std_actions.EchoAction,
|
||||
@ -108,6 +109,7 @@ class RunActionEngineTest(base.EngineTestCase):
|
||||
|
||||
self.assertIsNotNone(action_ex.output)
|
||||
self.assertIn('some error', action_ex.output['result'])
|
||||
self.assertEqual(states.ERROR, action_ex.state)
|
||||
|
||||
def test_run_action_save_result(self):
|
||||
# Start action.
|
||||
@ -262,7 +264,7 @@ class RunActionEngineTest(base.EngineTestCase):
|
||||
'scope': 'public'
|
||||
})
|
||||
def_mock.return_value = action_def
|
||||
run_mock.return_value = {'result': 'Hello'}
|
||||
run_mock.return_value = wf_utils.Result(data='Hello')
|
||||
|
||||
class_ret = mock.MagicMock()
|
||||
class_mock.return_value = class_ret
|
||||
|
@ -47,6 +47,10 @@ class Result(object):
|
||||
self.cancel == other.cancel
|
||||
)
|
||||
|
||||
def to_dict(self):
|
||||
return ({'result': self.data}
|
||||
if self.is_success() else {'result': self.error})
|
||||
|
||||
|
||||
class ResultSerializer(serializers.Serializer):
|
||||
def serialize(self, entity):
|
||||
|
Loading…
x
Reference in New Issue
Block a user