Files
spothole/spothole.py

84 lines
2.3 KiB
Python

# Main script
import logging
import os
import signal
import sys
from core.cleanup import CLEANUP_TIMER
from core.config import LOG_LEVEL, SERVER_OWNER_CALLSIGN, TELNET_SERVER_ENABLED, TELNET_SERVER_PORT
from core.constants import SOFTWARE_VERSION
from core.data_providers import DATA_PROVIDERS
from core.data_store import DATA_STORE
from core.status_reporter import StatusReporter
from telnetserver.telnetserver import TELNET_SERVER
from webserver.webserver import WEB_SERVER
logger = logging.getLogger(__name__)
_shutdown_in_progress = False
def shutdown(_signum=None, _frame=None):
"""Shutdown function"""
# Check if this is the second time a shutdown was asked for, if so immediately kill the program.
global _shutdown_in_progress
if _shutdown_in_progress:
os._exit(1)
_shutdown_in_progress = True
logger.info("Stopping program...")
WEB_SERVER.stop()
TELNET_SERVER.stop()
DATA_PROVIDERS.stop()
CLEANUP_TIMER.stop()
DATA_STORE.close()
logger.info("Stopped.")
# Main function
if __name__ == "__main__":
# Set up logging
root = logging.getLogger()
root.setLevel(LOG_LEVEL)
handler = logging.StreamHandler(sys.stdout)
handler.setLevel(LOG_LEVEL)
formatter = logging.Formatter("%(asctime)s | %(levelname)7s | %(message)s", "%Y-%m-%d %H:%M:%S")
handler.setFormatter(formatter)
root.handlers.clear()
root.addHandler(handler)
logger.info("Starting...")
logger.info(f"This is Spothole version {SOFTWARE_VERSION}. This instance is run by {SERVER_OWNER_CALLSIGN}.")
# Shut down gracefully on SIGINT or SIGTERM
signal.signal(signal.SIGINT, shutdown)
signal.signal(signal.SIGTERM, shutdown)
# Set up data store
DATA_STORE.setup()
CLEANUP_TIMER.setup(cleanup_interval=60)
CLEANUP_TIMER.start()
# Set up and start data providers
DATA_PROVIDERS.setup()
DATA_PROVIDERS.start()
# Set up and start status reporter
status_reporter = StatusReporter(run_interval=5)
status_reporter.start()
# Run the telnet server
if TELNET_SERVER_ENABLED:
TELNET_SERVER.start(port=TELNET_SERVER_PORT)
# Set up the web server
WEB_SERVER.setup()
# Run the web server
WEB_SERVER.start()
# Block the main thread until a termination signal arrives and shutdown() is running.
while not _shutdown_in_progress:
signal.pause()