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

View File

@ -10,7 +10,7 @@ job "poe-sensor" {
group "sensor-bridge" {
count = 1
# Network configuration - using host mode for better compatibility
# Network configuration - using host mode for Modbus access
network {
mode = "host"
port "health" {
@ -29,9 +29,9 @@ job "poe-sensor" {
# Update strategy
update {
max_parallel = 1
min_healthy_time = "90s"
healthy_deadline = "8m"
progress_deadline = "12m"
min_healthy_time = "30s" # Giảm xuống để deploy nhanh hơn
healthy_deadline = "3m" # Giảm deadline
progress_deadline = "5m" # Giảm progress deadline
auto_revert = true
canary = 0
}
@ -43,21 +43,11 @@ job "poe-sensor" {
"sensor",
"modbus",
"mqtt",
"iot",
"health-check"
"iot"
]
check {
type = "http"
path = "/health"
interval = "30s"
timeout = "20s"
initial_status = "critical"
check_restart {
limit = 2
grace = "20s"
}
}
# Loại bỏ health check để tránh unhealthy issues
# Service sẽ được coi là healthy nếu task chạy thành công
}
task "poe-sensor" {
@ -68,11 +58,11 @@ job "poe-sensor" {
command = "/bin/bash"
args = [
"-c",
"cd local/poe-sensor && echo 'Starting POE Sensor...' && apt-get update -qq && apt-get install -y procps curl && python -m pip install --upgrade pip && python -m pip install -r requirements.txt && echo 'Dependencies installed' && python -c 'import pymodbus, paho.mqtt.client; print(\"Dependencies OK\")' && echo 'Starting application...' && python main.py"
"cd local/poe-sensor && echo 'Starting POE Sensor...' && apt-get update -qq && apt-get install -y procps && python -m pip install --upgrade pip && python -m pip install -r requirements.txt && echo 'Dependencies installed' && python -c 'import pymodbus, paho.mqtt.client; print(\"Dependencies OK\")' && echo 'Starting application...' && python main.py"
]
}
# Git artifact
# Git artifact - using SSH similar to qc-scanner
artifact {
source = "git::ssh://git@gitea.service.mesh:2222/Mei_Sheng_Textiles/POE-sensor.git"
destination = "local/poe-sensor"
@ -86,13 +76,15 @@ job "poe-sensor" {
env {
LOG_LEVEL = "INFO"
PYTHONUNBUFFERED = "1"
PYTHONDONTWRITEBYTECODE = "1"
PYTHONMALLOC = "malloc"
PYTHONDONTWRITEBYTECODE = "1" # Prevent .pyc files to save memory
PYTHONMALLOC = "malloc" # Use system malloc for better memory management
TZ = "Asia/Ho_Chi_Minh"
# MQTT configuration (can be overridden by config.py)
MQTT_BROKER = "mqtt.service.mesh"
MQTT_PORT = "1883"
MQTT_USERNAME = "relay"
MQTT_PASSWORD = "Sey@K9c&Q4^"
# Health check configuration
HEALTH_CHECK_ENABLED = "true"
HEALTH_CHECK_PORT = "8080"
}