mirror of
https://git.ianrenton.com/ian/spothole.git
synced 2025-10-27 08:49:27 +00:00
Implemented old spot cleanup thread
This commit is contained in:
46
core/cleanup.py
Normal file
46
core/cleanup.py
Normal file
@@ -0,0 +1,46 @@
|
||||
# Provides a timed cleanup of the spot list.
|
||||
import logging
|
||||
from datetime import datetime, timedelta
|
||||
from threading import Timer
|
||||
from time import sleep
|
||||
|
||||
import pytz
|
||||
|
||||
|
||||
class CleanupTimer:
|
||||
|
||||
# Constructor
|
||||
def __init__(self, spot_list, cleanup_interval, max_spot_age):
|
||||
self.spot_list = spot_list
|
||||
self.cleanup_interval = cleanup_interval
|
||||
self.max_spot_age = max_spot_age
|
||||
self.cleanup_timer = None
|
||||
self.last_cleanup_time = datetime.min.replace(tzinfo=pytz.UTC)
|
||||
self.status = "Starting"
|
||||
|
||||
# Start the cleanup timer
|
||||
def start(self):
|
||||
self.cleanup()
|
||||
|
||||
# Stop any threads and prepare for application shutdown
|
||||
def stop(self):
|
||||
self.cleanup_timer.cancel()
|
||||
|
||||
# Perform cleanup and reschedule next timer
|
||||
def cleanup(self):
|
||||
try:
|
||||
# Perform cleanup
|
||||
for spot in self.spot_list:
|
||||
if not spot.time or spot.time < datetime.now(pytz.UTC) - timedelta(seconds=self.max_spot_age):
|
||||
self.spot_list.remove(spot)
|
||||
|
||||
self.status = "OK"
|
||||
self.last_cleanup_time = datetime.now(pytz.UTC)
|
||||
|
||||
except Exception as e:
|
||||
self.status = "Error"
|
||||
logging.exception("Exception in Cleanup thread")
|
||||
sleep(1)
|
||||
|
||||
self.cleanup_timer = Timer(self.cleanup_interval, self.cleanup)
|
||||
self.cleanup_timer.start()
|
||||
@@ -3,7 +3,10 @@ from data.band import Band
|
||||
# General software
|
||||
SOFTWARE_NAME = "Metaspot by M0TRT"
|
||||
SOFTWARE_VERSION = "0.1"
|
||||
|
||||
# Todo make configurable
|
||||
SERVER_OWNER_CALLSIGN = "M0TRT"
|
||||
MAX_SPOT_AGE_SEC = 3600
|
||||
|
||||
# Modes
|
||||
CW_MODES = ["CW"]
|
||||
|
||||
@@ -22,7 +22,8 @@ def infer_mode_family_from_mode(mode):
|
||||
elif mode.upper() in DATA_MODES:
|
||||
return "DATA"
|
||||
else:
|
||||
print("Found an unrecognised mode: " + mode + ". Developer should categorise this.")
|
||||
if mode.upper() != "OTHER":
|
||||
print("Found an unrecognised mode: " + mode + ". Developer should categorise this.")
|
||||
return None
|
||||
|
||||
# Infer a band from a frequency in kHz
|
||||
|
||||
Reference in New Issue
Block a user