Skip to content

Commit f15526b

Browse files
author
Alan Christie
committed
feat: Launcher now used job decoder (and variables)
1 parent 37c7154 commit f15526b

File tree

11 files changed

+299
-164
lines changed

11 files changed

+299
-164
lines changed

poetry.lock

Lines changed: 211 additions & 108 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ packages = [
1414
[tool.poetry.dependencies]
1515
python = "^3.12"
1616
im-protobuf = "^7.1.0"
17+
im-data-manager-job-decoder = "^2.1.0"
1718
jsonschema = "^4.21.1"
1819
pyyaml = ">= 5.3.1, < 7.0"
1920

tests/api_adapter.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,7 @@ def get_job(
276276
) -> Optional[Dict[str, Any]]:
277277
assert collection == _JOB_DEFINITIONS["collection"]
278278
assert job in _JOB_DEFINITIONS["jobs"]
279+
assert version
279280

280281
jd = _JOB_DEFINITIONS["jobs"][job]
281282
response = {"command": jd["command"]}

tests/instance_launcher.py

Lines changed: 39 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1+
import json
12
import os
23
import subprocess
34
from datetime import datetime, timezone
45
from subprocess import CompletedProcess
56
from typing import Any, Dict, List
67

8+
from decoder import decoder as job_decoder
9+
from decoder.decoder import TextEncoding
710
from informaticsmatters.protobuf.datamanager.pod_message_pb2 import PodMessage
811

912
from tests.api_adapter import UnitTestAPIAdapter
@@ -44,14 +47,16 @@ def launch(
4447
self,
4548
*,
4649
project_id: str,
47-
workflow_id: str,
50+
running_workflow_id: str,
4851
running_workflow_step_id: str,
49-
workflow_definition: Dict[str, Any],
50-
step_specification: Dict[str, Any],
52+
step_specification: str,
53+
variables: Dict[str, Any],
5154
) -> LaunchResult:
5255
assert project_id
53-
assert workflow_id
56+
assert running_workflow_id
5457
assert step_specification
58+
assert isinstance(step_specification, str)
59+
assert isinstance(variables, dict)
5560

5661
assert project_id == TEST_PROJECT_ID
5762

@@ -73,17 +78,36 @@ def launch(
7378
execution_directory = f"project-root/{project_id}"
7479
os.makedirs(execution_directory, exist_ok=True)
7580

76-
# Just run the Python module that matched the 'job' in the step specification.
77-
# Don't care about 'version' or 'collection'. It will be relative to the
78-
# execution directory.
79-
job: str = step_specification["job"]
80-
job_module = f"{_JOB_DIRECTORY}/{job}.py"
81-
assert os.path.isfile(job_module)
82-
83-
job_cmd: List[str] = ["python", job_module]
84-
print(f"Running job command: {job_module}")
81+
# Apply variables to the step's Job command.
82+
step_specification_map = json.loads(step_specification)
83+
job = self._api_adapter.get_job(
84+
collection=step_specification_map["collection"],
85+
job=step_specification_map["job"],
86+
version="do-not-care",
87+
)
88+
assert job
89+
90+
# Now apply the variables to the command
91+
decoded_command, status = job_decoder.decode(
92+
job["command"],
93+
variables,
94+
running_workflow_step_id,
95+
TextEncoding.JINJA2_3_0,
96+
)
97+
print(f"Decoded command: {decoded_command}")
98+
print(f"Status: {status}")
99+
assert status
100+
101+
# Now run the decoded command, which will be in the _JOB_DIRECTORY
102+
command = f"{_JOB_DIRECTORY}/{decoded_command}"
103+
command_list = command.split()
104+
module = command_list[0]
105+
print(f"Module: {module}")
106+
assert os.path.isfile(module)
107+
subprocess_cmd: List[str] = ["python"] + command_list
108+
print(f"Subprocess command: {subprocess_cmd}")
85109
completed_process: CompletedProcess = subprocess.run(
86-
job_cmd, check=False, cwd=execution_directory
110+
subprocess_cmd, check=False, cwd=execution_directory
87111
)
88112

89113
# Simulate a PodMessage (that will contain the instance ID),
@@ -103,5 +127,5 @@ def launch(
103127
error_msg=None,
104128
instance_id=instance_id,
105129
task_id=task_id,
106-
command=" ".join(job_cmd),
130+
command=" ".join(subprocess_cmd),
107131
)

tests/job-definitions/job-definitions.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ jobs:
3131
3232
smiles-to-file:
3333
command: >-
34-
string-to-file.py --smiles {{ smiles }} --output {{ outputFile }}
34+
smiles-to-file.py --smiles {{ smiles }} --output {{ outputFile }}
3535
variables:
3636
outputs:
3737
type: object

tests/test_test_instance_launcher.py

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# Tests for the decoder package.
2+
import json
23

34
import pytest
45

@@ -40,10 +41,12 @@ def test_launch_nop(basic_launcher):
4041
# Act
4142
result = launcher.launch(
4243
project_id=TEST_PROJECT_ID,
43-
workflow_id="workflow-00000000-0000-0000-0000-000000000001",
44+
running_workflow_id="r-workflow-00000000-0000-0000-0000-000000000001",
4445
running_workflow_step_id=rwfsid,
45-
workflow_definition={},
46-
step_specification={"job": "nop"},
46+
step_specification=json.dumps(
47+
{"collection": "workflow-engine-unit-test-jobs", "job": "nop"}
48+
),
49+
variables={},
4750
)
4851

4952
# Assert
@@ -70,10 +73,12 @@ def test_launch_nop_fail(basic_launcher):
7073
# Act
7174
result = launcher.launch(
7275
project_id=TEST_PROJECT_ID,
73-
workflow_id="workflow-00000000-0000-0000-0000-000000000001",
76+
running_workflow_id="r-workflow-00000000-0000-0000-0000-000000000001",
7477
running_workflow_step_id=rwfsid,
75-
workflow_definition={},
76-
step_specification={"job": "nop-fail"},
78+
step_specification=json.dumps(
79+
{"collection": "workflow-engine-unit-test-jobs", "job": "nop-fail"}
80+
),
81+
variables={},
7782
)
7883

7984
# Assert
@@ -100,16 +105,20 @@ def test_launch_smiles_to_file(basic_launcher):
100105
# Act
101106
result = launcher.launch(
102107
project_id=TEST_PROJECT_ID,
103-
workflow_id="workflow-00000000-0000-0000-0000-000000000001",
108+
running_workflow_id="r-workflow-00000000-0000-0000-0000-000000000001",
104109
running_workflow_step_id=rwfsid,
105-
workflow_definition={},
106-
step_specification={
107-
"job": "smiles-to-file",
108-
"variables": {"smiles": "C1=CC=CC=C1", "outputFile": "output.smi"},
109-
},
110+
step_specification=json.dumps(
111+
{
112+
"collection": "workflow-engine-unit-test-jobs",
113+
"job": "smiles-to-file",
114+
}
115+
),
116+
variables={"smiles": "C1=CC=CC=C1", "outputFile": "output.smi"},
110117
)
111118

112119
# Assert
113120
assert result.error == 0
114121
assert result.command.startswith("python ")
115-
assert result.command.endswith("tests/jobs/smiles-to-file.py")
122+
assert result.command.endswith(
123+
"tests/jobs/smiles-to-file.py --smiles C1=CC=CC=C1 --output output.smi"
124+
)

tests/workflow-definitions/example-nop-fail.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ steps:
88
- name: step-1
99
specification: >-
1010
{
11-
'collection': 'workflow-engine-unit-test-jobs',
12-
'job': 'nop-fail',
13-
'version': '1.0.0'
11+
"collection": "workflow-engine-unit-test-jobs",
12+
"job": "nop-fail",
13+
"version": "1.0.0"
1414
}

tests/workflow-definitions/example-two-step-nop.yaml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@ steps:
99
- name: step-1
1010
specification: >-
1111
{
12-
'collection': 'workflow-engine-unit-test-jobs',
13-
'job': 'nop',
14-
'version': '1.0.0'
12+
"collection": "workflow-engine-unit-test-jobs",
13+
"job": "nop",
14+
"version": "1.0.0"
1515
}
1616
- name: step-2
1717
specification: >-
1818
{
19-
'collection': 'workflow-engine-unit-test-jobs',
20-
'job': 'nop',
21-
'version': '1.0.0'
19+
"collection": "workflow-engine-unit-test-jobs",
20+
"job": "nop",
21+
"version": "1.0.0"
2222
}

tests/workflow-definitions/minimal.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@ kind-version: "2024.1"
44
name: workflow-minimal
55
steps:
66
- name: step-1
7-
specification: "{}"
7+
specification: >-
8+
{}

workflow/workflow_abc.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,10 @@ def launch(
2828
self,
2929
*,
3030
project_id: str,
31-
workflow_id: str,
31+
running_workflow_id: str,
3232
running_workflow_step_id: str,
33-
workflow_definition: Dict[str, Any],
34-
step_specification: Dict[str, Any],
33+
step_specification: str,
34+
variables: Dict[str, Any],
3535
) -> LaunchResult:
3636
"""Launch a (Job) Instance"""
3737

0 commit comments

Comments
 (0)