pub(crate) async fn run(
config: Tier2Config,
pool: PgPool,
) -> Result<(), Box<dyn Error + Send + Sync>>Expand description
Runs the Tier 2 XLX monitoring loop.
Manages a pool of UDP JSON monitor connections, connecting and disconnecting based on Tier 1 activity data. Runs until cancelled.
§Startup behavior
On startup, queries the database for XLX reflectors with
tier2_available = true and recent activity (within activity_threshold_secs).
Connects to up to max_concurrent_monitors of the most recently active
reflectors.
§Main loop
The main loop uses tokio::select! to multiplex across:
-
Monitor recv: each active monitor’s
recv()future is polled. When a message arrives, it is dispatched by type:Nodes: upserts toconnected_nodestable.Stations: inserts observations toactivity_log.OnAir/OffAir: logged via tracing (potential Tier 3 trigger point).Reflector: logged once on connect, otherwise ignored.Unknown: logged at debug level for diagnostics.
-
Refresh timer: every 60 seconds, re-queries the database for newly eligible reflectors and connects any that are not already monitored.
§Error handling
Individual monitor failures (recv timeout, parse errors) are logged and the monitor is removed from the pool. The orchestrator continues running with the remaining monitors. Only a fatal error (e.g., database pool closed) causes the function to return.
§Errors
Returns an error if a fatal, non-retryable failure occurs (e.g., the database pool is closed or initial reflector query fails).