Skip to content

Commit bbd2fb5

Browse files
authored
Respect explicit IBKR heartbeat services (#144)
1 parent 341dad0 commit bbd2fb5

2 files changed

Lines changed: 52 additions & 5 deletions

File tree

scripts/execution_report_heartbeat.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,12 @@ def _base_report_uris() -> list[str]:
9191

9292

9393
def _load_required_services() -> list[str]:
94+
explicit_services = _split_values(os.environ.get("RUNTIME_HEARTBEAT_REQUIRED_SERVICES"))
95+
if explicit_services:
96+
return _unique_values(explicit_services)
97+
9498
services = []
9599
for name in (
96-
"RUNTIME_HEARTBEAT_REQUIRED_SERVICES",
97100
"CLOUD_RUN_SERVICES",
98101
"CLOUD_RUN_SERVICE",
99102
):
@@ -124,12 +127,16 @@ def _load_required_services() -> list[str]:
124127
except json.JSONDecodeError:
125128
pass
126129

130+
return _unique_values(services)
131+
132+
133+
def _unique_values(values: list[str]) -> list[str]:
127134
seen = set()
128135
unique = []
129-
for service in services:
130-
if service not in seen:
131-
seen.add(service)
132-
unique.append(service)
136+
for value in values:
137+
if value not in seen:
138+
seen.add(value)
139+
unique.append(value)
133140
return unique
134141

135142

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
from __future__ import annotations
2+
3+
import json
4+
5+
from scripts import execution_report_heartbeat as heartbeat
6+
7+
8+
def test_explicit_required_services_override_target_derived_services(monkeypatch):
9+
monkeypatch.setenv("RUNTIME_HEARTBEAT_REQUIRED_SERVICES", "svc-daily-a,svc-daily-b")
10+
monkeypatch.setenv(
11+
"CLOUD_RUN_SERVICE_TARGETS_JSON",
12+
json.dumps(
13+
{
14+
"targets": [
15+
{"service": "svc-daily-a"},
16+
{"service": "svc-monthly"},
17+
]
18+
}
19+
),
20+
)
21+
22+
assert heartbeat._load_required_services() == ["svc-daily-a", "svc-daily-b"]
23+
24+
25+
def test_required_services_fall_back_to_cloud_run_targets(monkeypatch):
26+
monkeypatch.delenv("RUNTIME_HEARTBEAT_REQUIRED_SERVICES", raising=False)
27+
monkeypatch.setenv(
28+
"CLOUD_RUN_SERVICE_TARGETS_JSON",
29+
json.dumps(
30+
{
31+
"targets": [
32+
{"service": "svc-a"},
33+
{"runtime_target": {"service_name": "svc-b"}},
34+
{"service": "svc-a"},
35+
]
36+
}
37+
),
38+
)
39+
40+
assert heartbeat._load_required_services() == ["svc-a", "svc-b"]

0 commit comments

Comments
 (0)