Refactor configuration and health check server in POE project. Updated config.py to use environment variables for MQTT and health check settings, enhancing flexibility. Improved health check server in health_check.py with readiness endpoint and better error handling. Main application in main.py now includes graceful shutdown handling and health server verification. Adjusted Nomad job configuration for improved resource allocation and health check parameters.

This commit is contained in:
Naab2k3
2025-06-23 13:42:58 +07:00
parent c515b6d2e9
commit 8991a02bf5
4 changed files with 249 additions and 62 deletions

65
main.py
View File

@ -13,14 +13,67 @@ Author: POE Project
import time
import logging
import sys
import signal
from health_check import HealthCheckServer
from sensor_bridge import main_loop
# Global health server for cleanup
health_server = None
def signal_handler(signum, frame):
"""Handle shutdown signals gracefully"""
global health_server
logging.info(f"Received signal {signum}, shutting down gracefully...")
if health_server:
health_server.stop()
sys.exit(0)
if __name__ == "__main__":
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
# Setup logging
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(levelname)s - %(message)s',
handlers=[
logging.StreamHandler(sys.stdout),
]
)
# Setup signal handlers
signal.signal(signal.SIGTERM, signal_handler)
signal.signal(signal.SIGINT, signal_handler)
logging.info("POE Sensor Bridge starting up...")
# Give the system a moment to initialize
time.sleep(2)
logging.info("Starting main sensor loop...")
main_loop()
try:
# Start health check server first
logging.info("Starting health check server...")
health_server = HealthCheckServer()
if health_server.start():
logging.info("Health check server started successfully")
# Give health check server time to be ready
time.sleep(3)
# Verify health check is working
if health_server.is_running():
logging.info("Health check server verified as running")
logging.info("Starting main sensor loop...")
main_loop()
else:
logging.error("Health check server failed to start properly")
sys.exit(1)
else:
logging.error("Failed to start health check server")
sys.exit(1)
except KeyboardInterrupt:
logging.info("Received keyboard interrupt, shutting down...")
except Exception as e:
logging.error(f"Fatal error: {e}", exc_info=True)
sys.exit(1)
finally:
if health_server:
health_server.stop()
logging.info("POE Sensor Bridge shutdown complete")