Add continuous material polling runner

This commit is contained in:
admin committed 2026-09-05 07:55:40 +02:00
1 parent efc563512f
commit 6ddb76fd03
10 files changed
+672 -10

No files matched your search

@@ -0,0 +1,78 @@
"""Compose the configured live material polling application."""
import math
import os
import tempfile
from pathlib import Path
from urllib.parse import urlsplit
from production_analytics.calculations.config import MaterialCalculationConfig, finite_number
from production_analytics.enlyze.exploration import (
ConfigurationError,
ExplorationClient,
ExplorationSettings,
load_secret_file,
)
from production_analytics.enlyze.gateway import EnlyzeApiGateway
from production_analytics.service.material_polling import (
MaterialPollingService,
MaterialPollingState,
)
from production_analytics.service.material_runner import MaterialPollingRunner, MaterialStateError
from production_analytics.service.material_state_store import JsonMaterialStateStore
class _ReportingStateStore:
def __init__(self, store: JsonMaterialStateStore) -> None:
self.store = store
def load(self, machine_id: str, production_order: str) -> MaterialPollingState | None:
try:
return self.store.load(machine_id, production_order)
except Exception as exc:
raise MaterialStateError(f"state loading: {type(exc).__name__}") from exc
def save(
self, machine_id: str, production_order: str, state: MaterialPollingState,
) -> None:
try:
self.store.save(machine_id, production_order, state)
except Exception as exc:
raise MaterialStateError(f"state saving: {type(exc).__name__}") from exc
def build_material_runner(
calculation: MaterialCalculationConfig, *, poll_interval_seconds: float,
state_directory: Path, secrets_file: Path,
) -> MaterialPollingRunner:
finite_number(poll_interval_seconds, "poll interval", positive=True)
environment = dict(os.environ)
environment.update(load_secret_file(secrets_file))
settings = ExplorationSettings.from_environment(environment)
parsed = urlsplit(settings.base_url)
if parsed.scheme not in {"http", "https"} or not parsed.hostname or parsed.username:
raise ConfigurationError(
"ENLYZE_BASE_URL must be an HTTP(S) server URL without credentials"
)
if not math.isfinite(settings.timeout_seconds):
raise ConfigurationError("ENLYZE_HTTP_TIMEOUT_SECONDS must be finite")
try:
state_directory.mkdir(parents=True, exist_ok=True)
with tempfile.TemporaryFile(dir=state_directory):
pass
except OSError as exc:
raise ConfigurationError(
f"State directory startup check failed: {type(exc).__name__}"
) from exc
service = MaterialPollingService(
gateway=EnlyzeApiGateway(ExplorationClient(settings)),
state_store=_ReportingStateStore(JsonMaterialStateStore(state_directory)),
machine_id=calculation.machine_ref,
rate_variable_id=calculation.rate_signal_ref,
gate_variable_id=calculation.gate_signal_ref,
gate_threshold=calculation.gate_threshold,
max_sample_gap_seconds=calculation.max_sample_gap_seconds,
)
return MaterialPollingRunner(
service, machine_id=calculation.machine_ref, poll_interval_seconds=poll_interval_seconds,
)