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,7 +1,6 @@
import logging
from datetime import datetime
from threading import Timer, Thread
from time import sleep
from threading import Thread, Event
import pytz
import requests
@@ -18,22 +17,25 @@ class HTTPAlertProvider(AlertProvider):
super().__init__(provider_config)
self.url = url
self.poll_interval = poll_interval
self.poll_timer = None
self._stop_event = Event()
def start(self):
# Fire off a one-shot thread to run poll() for the first time, just to ensure start() returns immediately and
# the application can continue starting. The thread itself will then die, and the timer will kick in on its own
# thread.
# Fire off the polling thread. It will poll immediately on startup, then sleep for poll_interval between
# subsequent polls, so start() returns immediately and the application can continue starting.
logging.info("Set up query of " + self.name + " alert API every " + str(self.poll_interval) + " seconds.")
thread = Thread(target=self.poll)
thread.daemon = True
thread.start()
self._thread = Thread(target=self._run, daemon=True)
self._thread.start()
def stop(self):
if self.poll_timer:
self.poll_timer.cancel()
self._stop_event.set()
def poll(self):
def _run(self):
while True:
self._poll()
if self._stop_event.wait(timeout=self.poll_interval):
break
def _poll(self):
try:
# Request data from API
logging.debug("Polling " + self.name + " alert API...")
@@ -51,10 +53,8 @@ class HTTPAlertProvider(AlertProvider):
except Exception as e:
self.status = "Error"
logging.exception("Exception in HTTP JSON Alert Provider (" + self.name + ")")
sleep(1)
self.poll_timer = Timer(self.poll_interval, self.poll)
self.poll_timer.start()
# Brief pause on error before the next poll, but still respond promptly to stop()
self._stop_event.wait(timeout=1)
# Convert an HTTP response returned by the API into alert data. The whole response is provided here so the subclass
# implementations can check for HTTP status codes if necessary, and handle the response as JSON, XML, text, whatever