Attempt to fix CPU utilisation bug by preventing the heartbeat callback leak in the SSE stream handlers and replacing Timer-based with Event-based threads. Also compiled regexes in advance for DXCC callsign lookups for efficiency, and fixed my misunderstanding of what Queue.empty() does

This commit is contained in:
Ian Renton
2026-02-27 08:28:43 +00:00
parent e6c9bb1853
commit 068c732796
19 changed files with 107 additions and 94 deletions

View File

@@ -1,6 +1,6 @@
import os
from datetime import datetime
from threading import Timer
from threading import Thread, Event
import psutil
import pytz
@@ -24,22 +24,30 @@ class StatusReporter:
self.spot_providers = spot_providers
self.alerts = alerts
self.alert_providers = alert_providers
self.run_timer = None
self._stop_event = Event()
self.startup_time = datetime.now(pytz.UTC)
self.status_data["software-version"] = SOFTWARE_VERSION
self.status_data["server-owner-callsign"] = SERVER_OWNER_CALLSIGN
# Start the cleanup timer
# Start the reporter thread
def start(self):
self.run()
self._thread = Thread(target=self._run, daemon=True)
self._thread.start()
# Stop any threads and prepare for application shutdown
def stop(self):
self.run_timer.cancel()
self._stop_event.set()
# Write status information and reschedule next timer
def run(self):
# Thread entry point: report immediately on startup, then on each interval until stopped
def _run(self):
while True:
self._report()
if self._stop_event.wait(timeout=self.run_interval):
break
# Write status information
def _report(self):
self.status_data["uptime"] = (datetime.now(pytz.UTC) - self.startup_time).total_seconds()
self.status_data["mem_use_mb"] = round(psutil.Process(os.getpid()).memory_info().rss / (1024 * 1024), 3)
self.status_data["num_spots"] = len(self.spots)
@@ -73,7 +81,4 @@ class StatusReporter:
# Update Prometheus metrics
memory_use_gauge.set(psutil.Process(os.getpid()).memory_info().rss * 1024)
spots_gauge.set(len(self.spots))
alerts_gauge.set(len(self.alerts))
self.run_timer = Timer(self.run_interval, self.run)
self.run_timer.start()
alerts_gauge.set(len(self.alerts))