Skip to content

Commit

Permalink
I check to see if they are active, so I can pass ACTIVE=1
Browse files Browse the repository at this point in the history
  • Loading branch information
CamDavidsonPilon committed Oct 3, 2024
1 parent 2162279 commit 079debe
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 8 deletions.
16 changes: 10 additions & 6 deletions pioreactorui/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,9 +171,11 @@ def run_job_on_unit_in_experiment(
# we can do better: make sure the worker is active, too
workers = query_app_db(
"""
SELECT pioreactor_unit as worker
FROM experiment_worker_assignments
WHERE experiment = ?
SELECT a.pioreactor_unit as worker
FROM experiment_worker_assignments a
JOIN workers w
on w.pioreactor_unit = a.pioreactor_unit
WHERE experiment = ? and w.is_active = 1
""",
(experiment,),
)
Expand All @@ -186,8 +188,10 @@ def run_job_on_unit_in_experiment(
okay = query_app_db(
"""
SELECT count(1) as count
FROM experiment_worker_assignments
WHERE experiment = ? AND pioreactor_unit = ?
FROM experiment_worker_assignments a
JOIN workers w
on w.pioreactor_unit = a.pioreactor_unit
WHERE a.experiment = ? AND w.pioreactor_unit = ? AND w.is_active = 1
""",
(experiment, pioreactor_unit),
one=True,
Expand All @@ -202,7 +206,7 @@ def run_job_on_unit_in_experiment(
return Response(status=404)

# and we can include experiment in the env since we know these workers are in the experiment!
json.env = {"EXPERIMENT": experiment}
json.env = {"EXPERIMENT": experiment, "ACTIVE": "1"}

t = tasks.multicast_post_across_cluster(
f"/unit_api/jobs/run/job_name/{job}", assigned_workers, json=json
Expand Down
1 change: 1 addition & 0 deletions pioreactorui/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
"ACTIVE",
"PIO_VERSION",
"FIRMWARE",
"ACTIVE",
)


Expand Down
4 changes: 4 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

import pytest
from flask import g
from msgspec import Struct
from msgspec import to_builtins
from pioreactor.mureq import get
from pioreactor.mureq import Response

Expand Down Expand Up @@ -73,6 +75,8 @@ def mock_request(method, url, **kwargs):
headers = kwargs.get("headers")
body = kwargs.get("body", None)
json = kwargs.get("json", None)
if isinstance(json, Struct):
json = to_builtins(json)
bucket.append(CapturedRequest(method, url, headers, body, json))
# Return a mock response object
return Response(url, 200, {}, b'{"mocked": "response"}')
Expand Down
20 changes: 18 additions & 2 deletions tests/test_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,11 @@ def test_broadcast_in_manage_all(client):
)
assert len(bucket) == 2
assert bucket[0].path == "/unit_api/jobs/run/job_name/stirring"
assert bucket[0].json == {"options": {"target_rpm": 10}, "env": {"EXPERIMENT": "exp1"}}
assert bucket[0].json == {
"args": [],
"options": {"target_rpm": 10},
"env": {"EXPERIMENT": "exp1", "ACTIVE": "1"},
}

# Remove unit2 from exp1
client.delete("/api/experiments/exp1/workers/unit2")
Expand All @@ -293,7 +297,11 @@ def test_run_job(client):
)
assert len(bucket) == 1
assert bucket[0].path == "/unit_api/jobs/run/job_name/stirring"
assert bucket[0].json == {"options": {"target_rpm": 10}, "env": {"EXPERIMENT": "exp1"}}
assert bucket[0].json == {
"args": [],
"options": {"target_rpm": 10},
"env": {"EXPERIMENT": "exp1", "ACTIVE": "1"},
}

# wrong experiment!
with capture_requests() as bucket:
Expand All @@ -303,6 +311,14 @@ def test_run_job(client):
)
assert len(bucket) == 0

# not active!
with capture_requests() as bucket:
client.post(
"/api/workers/unit4/jobs/run/job_name/stirring/experiments/exp3",
json={"options": {"target_rpm": 10}},
)
assert len(bucket) == 0


def test_run_job_response(client):
# regression test
Expand Down

0 comments on commit 079debe

Please sign in to comment.