mirror of
https://git.ianrenton.com/ian/spothole.git
synced 2026-02-04 09:14:30 +00:00
First attempt at SSE backend #3
This commit is contained in:
39
spothole.py
39
spothole.py
@@ -3,11 +3,13 @@ import importlib
|
||||
import logging
|
||||
import signal
|
||||
import sys
|
||||
from time import sleep
|
||||
|
||||
from diskcache import Cache
|
||||
from gevent import monkey; monkey.patch_all()
|
||||
|
||||
from core.cleanup import CleanupTimer
|
||||
from core.config import config, WEB_SERVER_PORT, SERVER_OWNER_CALLSIGN
|
||||
from core.config import config, WEB_SERVER_PORT, SERVER_OWNER_CALLSIGN, MAX_SPOT_AGE
|
||||
from core.constants import SOFTWARE_NAME, SOFTWARE_VERSION
|
||||
from core.lookup_helper import lookup_helper
|
||||
from core.status_reporter import StatusReporter
|
||||
@@ -16,14 +18,17 @@ from server.webserver import WebServer
|
||||
# Globals
|
||||
spots = Cache('cache/spots_cache')
|
||||
alerts = Cache('cache/alerts_cache')
|
||||
web_server = None
|
||||
status_data = {}
|
||||
spot_providers = []
|
||||
alert_providers = []
|
||||
cleanup_timer = None
|
||||
run = True
|
||||
|
||||
|
||||
# Shutdown function
|
||||
def shutdown(sig, frame):
|
||||
global spot_providers, alert_providers, cleanup_timer, spots, alerts, run
|
||||
logging.info("Stopping program, this may take a few seconds...")
|
||||
for p in spot_providers:
|
||||
if p.enabled:
|
||||
@@ -35,6 +40,7 @@ def shutdown(sig, frame):
|
||||
lookup_helper.stop()
|
||||
spots.close()
|
||||
alerts.close()
|
||||
run = False
|
||||
|
||||
|
||||
# Utility method to get a spot provider based on the class specified in its config entry.
|
||||
@@ -50,6 +56,24 @@ def get_alert_provider_from_config(config_providers_entry):
|
||||
provider_class = getattr(module, config_providers_entry["class"])
|
||||
return provider_class(config_providers_entry)
|
||||
|
||||
# Utility method to add a spot, notifying the web server in case any Server-Sent Event connections need to have data
|
||||
# sent immediately. If the spot has already expired due to loading old data, it will be ignored.
|
||||
def add_spot(spot):
|
||||
global web_server
|
||||
if not spot.expired():
|
||||
spots.add(spot.id, spot, expire=MAX_SPOT_AGE)
|
||||
if web_server:
|
||||
web_server.notify_new_spot(spot)
|
||||
|
||||
# Utility method to add an alert, notifying the web server in case any Server-Sent Event connections need to have data
|
||||
# sent immediately. If the alert has already expired due to loading old data, it will be ignored.
|
||||
def add_alert(alert):
|
||||
global web_server
|
||||
if not alert.expired():
|
||||
alerts.add(alert.id, alert, expire=MAX_SPOT_AGE)
|
||||
if web_server:
|
||||
web_server.notify_new_alert(alert)
|
||||
|
||||
|
||||
# Main function
|
||||
if __name__ == '__main__':
|
||||
@@ -72,6 +96,10 @@ if __name__ == '__main__':
|
||||
# Set up lookup helper
|
||||
lookup_helper.start()
|
||||
|
||||
# Set up web server
|
||||
web_server = WebServer(spots=spots, alerts=alerts, status_data=status_data, port=WEB_SERVER_PORT)
|
||||
web_server.start()
|
||||
|
||||
# Fetch, set up and start spot providers
|
||||
for entry in config["spot-providers"]:
|
||||
spot_providers.append(get_spot_provider_from_config(entry))
|
||||
@@ -89,13 +117,9 @@ if __name__ == '__main__':
|
||||
p.start()
|
||||
|
||||
# Set up timer to clear spot list of old data
|
||||
cleanup_timer = CleanupTimer(spots=spots, alerts=alerts, cleanup_interval=60)
|
||||
cleanup_timer = CleanupTimer(spots=spots, alerts=alerts, web_server=web_server, cleanup_interval=60)
|
||||
cleanup_timer.start()
|
||||
|
||||
# Set up web server
|
||||
web_server = WebServer(spots=spots, alerts=alerts, status_data=status_data, port=WEB_SERVER_PORT)
|
||||
web_server.start()
|
||||
|
||||
# Set up status reporter
|
||||
status_reporter = StatusReporter(status_data=status_data, spots=spots, alerts=alerts, web_server=web_server,
|
||||
cleanup_timer=cleanup_timer, spot_providers=spot_providers,
|
||||
@@ -103,3 +127,6 @@ if __name__ == '__main__':
|
||||
status_reporter.start()
|
||||
|
||||
logging.info("Startup complete.")
|
||||
|
||||
while (run):
|
||||
sleep(1)
|
||||
|
||||
Reference in New Issue
Block a user