Refactor health check server implementation in POE project. Renamed HealthCheckHandler to SimpleHealthHandler for clarity, improved error handling in health response and sensor status retrieval, and added default health response for root path. Enhanced server startup process with retry logic and introduced a SimpleTCPHealthServer as a fallback. Updated main.py to initialize health server non-blocking and ensure graceful shutdown. Adjusted Nomad job configuration for health check parameters and removed unnecessary health check definitions to prevent unhealthy issues.

This commit is contained in:
Naab2k3
2025-06-24 08:27:29 +07:00
parent 9b0f4f6236
commit 9f8ac5b5c2
4 changed files with 229 additions and 123 deletions

30
main.py
View File

@ -27,18 +27,40 @@ if __name__ == "__main__":
logging.info("POE Sensor Bridge starting up...")
try:
# Import after logging is setup to avoid circular import issues
from sensor_bridge import main_loop
# Start health check server first (non-blocking)
try:
from health_check import create_health_server
health_server = create_health_server()
if health_server:
logging.info("Health check server started successfully")
else:
logging.warning("Health check server failed to start, continuing without it")
except Exception as e:
logging.warning(f"Health check setup failed: {e}, continuing without it")
health_server = None
# Give the system a moment to initialize
# Give health server time to initialize
time.sleep(2)
# Import and start main loop
from sensor_bridge import main_loop
logging.info("Starting main sensor loop...")
main_loop()
except ImportError as e:
logging.error(f"Import error: {e}")
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)
sys.exit(1)
finally:
# Cleanup health server if it exists
try:
if 'health_server' in locals() and health_server:
health_server.stop()
except:
pass
logging.info("POE Sensor Bridge shutdown complete")