Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions scripts/execution_report_heartbeat.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,12 @@ def _base_report_uris() -> list[str]:


def _load_required_services() -> list[str]:
explicit_services = _split_values(os.environ.get("RUNTIME_HEARTBEAT_REQUIRED_SERVICES"))
if explicit_services:
return _unique_values(explicit_services)

services = []
for name in (
"RUNTIME_HEARTBEAT_REQUIRED_SERVICES",
"CLOUD_RUN_SERVICES",
"CLOUD_RUN_SERVICE",
):
Expand Down Expand Up @@ -124,12 +127,16 @@ def _load_required_services() -> list[str]:
except json.JSONDecodeError:
pass

return _unique_values(services)


def _unique_values(values: list[str]) -> list[str]:
seen = set()
unique = []
for service in services:
if service not in seen:
seen.add(service)
unique.append(service)
for value in values:
if value not in seen:
seen.add(value)
unique.append(value)
return unique


Expand Down
40 changes: 40 additions & 0 deletions tests/test_execution_report_heartbeat.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
from __future__ import annotations

import json

from scripts import execution_report_heartbeat as heartbeat


def test_explicit_required_services_override_target_derived_services(monkeypatch):
monkeypatch.setenv("RUNTIME_HEARTBEAT_REQUIRED_SERVICES", "svc-daily-a,svc-daily-b")
monkeypatch.setenv(
"CLOUD_RUN_SERVICE_TARGETS_JSON",
json.dumps(
{
"targets": [
{"service": "svc-daily-a"},
{"service": "svc-monthly"},
]
}
),
)

assert heartbeat._load_required_services() == ["svc-daily-a", "svc-daily-b"]


def test_required_services_fall_back_to_cloud_run_targets(monkeypatch):
monkeypatch.delenv("RUNTIME_HEARTBEAT_REQUIRED_SERVICES", raising=False)
monkeypatch.setenv(
"CLOUD_RUN_SERVICE_TARGETS_JSON",
json.dumps(
{
"targets": [
{"service": "svc-a"},
{"runtime_target": {"service_name": "svc-b"}},
{"service": "svc-a"},
]
}
),
)

assert heartbeat._load_required_services() == ["svc-a", "svc-b"]