fmartingr
/
jeeves
Archived
1
0
Fork 0
This repository has been archived on 2021-02-14. You can view files and clone it, but cannot push or open issues or pull requests.
jeeves/jeeves/core/executor.py

45 lines
1.4 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()
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):
for step in self._execution.steps:
self.execute_step(step)
self._execution.success = step.result.success