Skip to content

Commit 6a6aa4a

Browse files
author
Alan Christie
committed
fix: Better doc and minor logic rework
1 parent 6cbd96f commit 6a6aa4a

File tree

1 file changed

+24
-17
lines changed

1 file changed

+24
-17
lines changed

workflow/workflow_engine.py

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@
1717

1818
class WorkflowEngine:
1919
"""The workflow engine. An event-driven engine that manages the execution
20-
of workflow instances. The engine is responsible for launching instances or
21-
reporting failures and conclusions.
20+
of validated workflow definitions.
2221
"""
2322

2423
def __init__(
@@ -32,20 +31,24 @@ def __init__(
3231
self._instance_launcher = instance_launcher
3332

3433
def handle_message(self, msg: Message) -> None:
35-
"""Given a Pod Message, we use it to identify the Pod (Instance) exit code,
36-
workflow and step and decide what to do next.
34+
"""Expect Pod and Workflow messages.
35+
36+
WorkflowMessages signal the need to start (or stop) validated workflows -
37+
i.e. take a workflow and create a running workflows (and its steps).
38+
39+
PodMessages signal the completion of a Pod (an Instance/Job) that is part
40+
of a running workflow. Given a Pod Message, we use it to identify the
41+
Instance exit code, running workflow, and step and decide what to do next,
42+
which might be to run the next step in the workflow or abandon the workflow.
3743
3844
Only pod messages relating to workflow instances will be delivered to this method.
3945
The Pod message has an 'instance' property that provides the UUID of
40-
the instance that was run. This can be used to correlate the instance with the
46+
the instance that was run. This is used to correlate the instance with the
4147
running workflow step.
42-
43-
Additionally we will encounter WorkflowMessages that signal the need to
44-
start and stop workflows.
4548
"""
4649
assert msg
4750

48-
_LOGGER.info("> WE.handle_message() : GOT WorkflowMessage:\n%s", str(msg))
51+
_LOGGER.debug("> WE.handle_message() : GOT WorkflowMessage:\n%s", str(msg))
4952

5053
# Is this a WorkflowMessage or a PodMessage?
5154
if isinstance(msg, PodMessage):
@@ -66,7 +69,8 @@ def _handle_workflow_message(self, msg: WorkflowMessage) -> None:
6669
# THE STEPS HAVE NO INPUTS OR OUTPUTS.
6770
# THIS FUNCTION PROBABLY NEEDS TO BE A LOT MORE SOPHISTICATED!
6871

69-
_LOGGER.info("WE> WorkflowMessage:\n%s", str(msg))
72+
_LOGGER.debug("WE> WorkflowMessage:\n%s", str(msg))
73+
7074
if msg.action == "START":
7175
# Using the running workflow get the workflow definition
7276
response = self._api_adapter.get_running_workflow(
@@ -75,11 +79,11 @@ def _handle_workflow_message(self, msg: WorkflowMessage) -> None:
7579
assert "running_workflow" in response
7680
running_workflow = response["running_workflow"]
7781
_LOGGER.info("RunningWorkflow: %s", running_workflow)
78-
project_id = running_workflow["project_id"]
82+
7983
workflow_id = running_workflow["workflow"]["id"]
80-
variables = running_workflow["variables"]
8184
response = self._api_adapter.get_workflow(workflow_id=workflow_id)
8285
assert "workflow" in response
86+
8387
workflow = response["workflow"]
8488
# Now find the first step
8589
# and create a RunningWorkflowStep record prior to launching the instance
@@ -92,6 +96,8 @@ def _handle_workflow_message(self, msg: WorkflowMessage) -> None:
9296
# The step specification is a string here - pass it directly to the launcher
9397
# which will get the Job command and apply the provided variables to it.
9498
step = workflow["steps"][0]
99+
project_id = running_workflow["project_id"]
100+
variables = running_workflow["variables"]
95101
self._instance_launcher.launch(
96102
project_id=project_id,
97103
running_workflow_id=msg.running_workflow,
@@ -117,7 +123,7 @@ def _handle_pod_message(self, msg: PodMessage) -> None:
117123
assert msg
118124

119125
# The PodMessage has a 'instance', 'has_exit_code', and 'exit_code' values.
120-
_LOGGER.info("WE> PodMessage:\n%s", str(msg))
126+
_LOGGER.debug("WE> PodMessage:\n%s", str(msg))
121127

122128
# ALL THIS CODE ADDED SIMPLY TO DEMONSTRATE THE USE OF THE API ADAPTER
123129
# AND THE INSTANCE LAUNCHER FOR THE SIMPLEST OF WORKFLOWS: -
@@ -133,7 +139,7 @@ def _handle_pod_message(self, msg: PodMessage) -> None:
133139

134140
instance_id: str = msg.instance
135141
exit_code: int = msg.exit_code
136-
_LOGGER.info(
142+
_LOGGER.debug(
137143
"WE> PodMessage: instance=%s, exit_code=%d", instance_id, exit_code
138144
)
139145

@@ -161,9 +167,8 @@ def _handle_pod_message(self, msg: PodMessage) -> None:
161167
response = self._api_adapter.get_running_workflow(
162168
running_workflow_id=running_workflow_id
163169
)
164-
project_id = response["running_workflow"]["project_id"]
165-
workflow_id = response["running_workflow"]["workflow"]["id"]
166-
variables = response["running_workflow"]["variables"]
170+
running_workflow = response["running_workflow"]
171+
workflow_id = running_workflow["workflow"]["id"]
167172
assert workflow_id
168173
response = self._api_adapter.get_workflow(workflow_id=workflow_id)
169174

@@ -186,6 +191,8 @@ def _handle_pod_message(self, msg: PodMessage) -> None:
186191
)
187192
assert "id" in response
188193
running_workflow_step_id = response["id"]
194+
project_id = running_workflow["project_id"]
195+
variables = running_workflow["variables"]
189196
self._instance_launcher.launch(
190197
project_id=project_id,
191198
running_workflow_id=running_workflow_id,

0 commit comments

Comments
 (0)