55 lines
1.6 KiB
Python
55 lines
1.6 KiB
Python
import traceback
|
|
|
|
from jeeves.core.objects import Flow, Result, Execution, ExecutionStep
|
|
|
|
|
|
class Executor:
|
|
def __init__(self, flow: Flow):
|
|
self.step_count = len(flow.tasks)
|
|
self._flow: Flow = flow
|
|
self._execution = Execution(flow=flow, steps=self._get_steps(flow))
|
|
|
|
@property
|
|
def steps(self):
|
|
for step in self._execution.steps:
|
|
yield step
|
|
|
|
def _get_steps(self, flow: Flow):
|
|
for task in flow.tasks:
|
|
yield ExecutionStep(task=task, result=Result())
|
|
|
|
def execute_step(self, step: ExecutionStep):
|
|
try:
|
|
step.result = step.task.action.execute(workspace=self._execution.workspace)
|
|
except Exception as error:
|
|
# Catch unhandled exceptions, mark the result as unsuccessful
|
|
# and append the error as output.
|
|
tb = traceback.format_exc()
|
|
output = "\n".join(
|
|
(
|
|
"=" * 30,
|
|
f"Uncaught exception on task {step.task.type}",
|
|
f"\t{step.task}",
|
|
f"Error: {error}",
|
|
tb,
|
|
"=" * 30,
|
|
)
|
|
)
|
|
step.result = Result(success=False, output=output)
|
|
return step.result
|
|
|
|
def start(self):
|
|
"""
|
|
Executes (sync) all the actions for the provided flow.
|
|
"""
|
|
for step in self.steps:
|
|
self.execute_step(step)
|
|
self._execution.success = step.result.success
|
|
self.post_execution()
|
|
|
|
def post_execution(self):
|
|
"""
|
|
Cleanup after a flow have been executed.
|
|
"""
|
|
self._execution.workspace.destroy()
|