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:
@ -180,11 +180,9 @@ def main_loop():
|
||||
"""Main function to connect and publish data in cycles"""
|
||||
# Import here to avoid circular import
|
||||
from sensor_tracker import get_sensor_tracker
|
||||
from health_check import HealthCheckServer
|
||||
|
||||
# Initialize components
|
||||
sensor_tracker = get_sensor_tracker()
|
||||
health_server = HealthCheckServer()
|
||||
|
||||
# Initialize MQTT client
|
||||
mqtt_client = mqtt.Client(client_id=MQTT_CLIENT_ID)
|
||||
@ -195,21 +193,13 @@ def main_loop():
|
||||
mqtt_client.username_pw_set(MQTT_USERNAME, MQTT_PASSWORD)
|
||||
|
||||
try:
|
||||
# Start health check server
|
||||
logging.info("Starting health check server...")
|
||||
if health_server.start():
|
||||
logging.info("Health check server started successfully")
|
||||
else:
|
||||
logging.warning("Health check server failed to start, continuing anyway...")
|
||||
|
||||
# Connect to MQTT broker
|
||||
logging.info(f"Connecting to MQTT broker {MQTT_BROKER}:{MQTT_PORT}...")
|
||||
mqtt_client.connect(MQTT_BROKER, MQTT_PORT, 60)
|
||||
mqtt_client.loop_start()
|
||||
|
||||
logging.info(f"Starting monitoring of {len(MODBUS_HOSTS)} sensors")
|
||||
if health_server.is_running():
|
||||
logging.info("Health check available at: http://0.0.0.0:8080/health")
|
||||
logging.info("Health check available at: http://0.0.0.0:8080/health")
|
||||
|
||||
# Main loop
|
||||
while True:
|
||||
@ -259,17 +249,12 @@ def main_loop():
|
||||
logging.error(f"Unexpected error in main loop: {e}", exc_info=True)
|
||||
finally:
|
||||
# Cleanup
|
||||
try:
|
||||
if health_server:
|
||||
health_server.stop()
|
||||
except:
|
||||
pass
|
||||
try:
|
||||
mqtt_client.loop_stop()
|
||||
mqtt_client.disconnect()
|
||||
except:
|
||||
pass
|
||||
logging.info("Shutdown complete")
|
||||
logging.info("Sensor bridge shutdown complete")
|
||||
|
||||
if __name__ == "__main__":
|
||||
main_loop()
|
||||
|
Reference in New Issue
Block a user