Skip to content

Commit f70ac1d

Browse files
committed
Modify upon review
Signed-off-by: Alina Buzachis <[email protected]>
1 parent 2f0e533 commit f70ac1d

File tree

10 files changed

+226
-185
lines changed

10 files changed

+226
-185
lines changed

core/task_runner.py

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import json
22
import logging
33
import os
4+
from contextlib import closing
45

56
from core.utils.controller import assign_execute_roles
67
from core.utils.controller import build_collection_uri
@@ -9,6 +10,7 @@
910
from core.utils.controller import create_labels
1011
from core.utils.controller import create_project
1112
from core.utils.controller import download_collection
13+
from core.utils.controller import get_http_session
1214
from core.utils.controller import save_instance_state
1315

1416
from .models import Pattern
@@ -74,19 +76,23 @@ def run_pattern_instance_task(instance_id: int, task_id: int) -> None:
7476
if not pattern_def:
7577
raise ValueError("Pattern definition is missing.")
7678

77-
task.mark_running({"info": "Creating controller project"})
78-
project_id = create_project(instance, pattern, pattern_def)
79-
task.mark_running({"info": "Creating execution environment"})
80-
ee_id = create_execution_environment(instance, pattern_def)
81-
task.mark_running({"info": "Creating labels"})
82-
labels = create_labels(instance, pattern_def)
83-
task.mark_running({"info": "Creating job templates"})
84-
automations = create_job_templates(instance, pattern_def, project_id, ee_id)
85-
task.mark_running({"info": "Saving instance"})
86-
save_instance_state(instance, project_id, ee_id, labels, automations)
87-
task.mark_running({"info": "Assigning roles"})
88-
assign_execute_roles(instance.executors, automations)
89-
task.mark_completed({"info": "PatternInstance processed"})
79+
# Create a single session for all AAP calls
80+
with closing(get_http_session()) as session:
81+
task.mark_running({"info": "Creating controller project"})
82+
project_id = create_project(session, instance, pattern)
83+
task.mark_running({"info": "Creating execution environment"})
84+
ee_id = create_execution_environment(session, instance, pattern_def)
85+
task.mark_running({"info": "Creating labels"})
86+
labels = create_labels(session, instance, pattern_def)
87+
task.mark_running({"info": "Creating job templates"})
88+
automations = create_job_templates(
89+
session, instance, pattern_def, project_id, ee_id
90+
)
91+
task.mark_running({"info": "Saving instance"})
92+
save_instance_state(instance, project_id, ee_id, labels, automations)
93+
task.mark_running({"info": "Assigning roles"})
94+
assign_execute_roles(session, instance.executors, automations)
95+
task.mark_completed({"info": "PatternInstance processed"})
9096
except Exception as e:
9197
logger.exception("Failed to process PatternInstance.")
9298
task.mark_failed({"error": str(e)})

core/tests/test_controller_client.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ def test_post_success(mock_get_http_session):
3434
session.post.return_value = _fake_response(201, {"id": 99})
3535
mock_get_http_session.return_value = session
3636

37-
assert cc.post("/labels/", {"name": "foo"}) == {"id": 99}
37+
assert cc.post(session, "/labels/", {"name": "foo"}) == {"id": 99}
3838
session.post.assert_called_once()
3939

4040

@@ -47,7 +47,7 @@ def test_post_duplicate_name(mock_get_http_session):
4747
)
4848
mock_get_http_session.return_value = session
4949

50-
out = cc.post("/labels/", {"name": "foo"})
50+
out = cc.post(session, "/labels/", {"name": "foo"})
5151
assert out == {"id": 42, "name": "foo"}
5252

5353
session.get.assert_called_once()
@@ -61,7 +61,7 @@ def test_post_non_400_error_is_propagated(mock_get_http_session):
6161
mock_get_http_session.return_value = session
6262

6363
with pytest.raises(requests.HTTPError):
64-
cc.post("/labels/", {"name": "foo"})
64+
cc.post(session, "/labels/", {"name": "foo"})
6565

6666
# Ensure we don't try to deduplicate
6767
session.get.assert_not_called()
@@ -76,7 +76,7 @@ def test_post_400_dedupe_no_results_raises(mock_get_http_session):
7676
mock_get_http_session.return_value = session
7777

7878
with pytest.raises(requests.HTTPError) as exc:
79-
cc.post("/labels/", {"name": "foo", "organization": 1})
79+
cc.post(session, "/labels/", {"name": "foo", "organization": 1})
8080

8181
assert isinstance(exc.value, requests.HTTPError)
8282
assert exc.value.response.status_code == 400
@@ -95,7 +95,7 @@ def test_post_400_dedupe_lookup_http_error_still_raises(mock_get_http_session):
9595
mock_get_http_session.return_value = session
9696

9797
with pytest.raises(requests.HTTPError):
98-
cc.post("/labels/", {"name": "foo"})
98+
cc.post(session, "/labels/", {"name": "foo"})
9999

100100
session.get.assert_called_once()
101101

@@ -108,7 +108,7 @@ def test_post_400_dedupe_params_subset_when_missing_org(mock_get_http_session):
108108
mock_get_http_session.return_value = session
109109

110110
with pytest.raises(requests.HTTPError):
111-
cc.post("/labels/", {"name": "foo"})
111+
cc.post(session, "/labels/", {"name": "foo"})
112112

113113
# Only 'name' present in params, no 'organization' since it wasn’t provided
114114
_, kwargs = session.get.call_args
@@ -124,6 +124,7 @@ def test_post_400_with_custom_dedupe_keys(mock_get_http_session):
124124

125125
with pytest.raises(requests.HTTPError):
126126
cc.post(
127+
session,
127128
"/labels/",
128129
{"name": "foo", "organization": 1, "extra": "x"},
129130
dedupe_keys=("name", "extra"),
@@ -148,7 +149,7 @@ def test_post_400_error_json_fallback_to_text(mock_get_http_session):
148149
mock_get_http_session.return_value = session
149150

150151
with pytest.raises(requests.HTTPError) as exc:
151-
cc.post("/labels/", {"name": "foo"})
152+
cc.post(session, "/labels/", {"name": "foo"})
152153

153154
# Error message should include the fallback text
154155
assert "not json" in str(exc.value)

0 commit comments

Comments
 (0)