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

View File

@ -10,28 +10,29 @@ job "poe-sensor" {
group "sensor-bridge" {
count = 1
# Network configuration - using host mode for Modbus access
# Network configuration - using bridge mode with port mapping for better isolation
network {
mode = "host"
mode = "bridge"
port "health" {
static = 8080
to = 8080
}
}
# Restart policy
# Restart policy - more lenient for startup issues
restart {
attempts = 3
attempts = 5
interval = "30m"
delay = "15s"
delay = "30s"
mode = "fail"
}
# Update strategy
update {
max_parallel = 1
min_healthy_time = "60s"
healthy_deadline = "5m"
progress_deadline = "10m"
min_healthy_time = "120s" # Increased from 60s
healthy_deadline = "10m" # Increased from 5m
progress_deadline = "15m" # Increased from 10m
auto_revert = true
canary = 0
}
@ -50,12 +51,12 @@ job "poe-sensor" {
check {
type = "http"
path = "/health"
interval = "60s"
timeout = "15s"
initial_status = "passing"
interval = "30s" # Reduced frequency
timeout = "30s" # Increased timeout
initial_status = "critical" # Start as critical until proven healthy
check_restart {
limit = 2
grace = "15s"
limit = 3
grace = "30s" # More time for graceful shutdown
}
}
}
@ -68,7 +69,21 @@ job "poe-sensor" {
command = "/bin/bash"
args = [
"-c",
"cd local/poe-sensor && apt-get update -qq && apt-get install -y procps && python -m pip install --upgrade pip && python -m pip install -r requirements.txt && python -c 'import pymodbus, paho.mqtt.client; print(\"Dependencies installed successfully\")' && python main.py"
<<EOF
cd local/poe-sensor &&
echo "Starting POE Sensor installation..." &&
apt-get update -qq &&
apt-get install -y procps curl &&
python -m pip install --upgrade pip &&
echo "Installing Python dependencies..." &&
python -m pip install -r requirements.txt &&
echo "Testing dependencies..." &&
python -c 'import pymodbus, paho.mqtt.client; print("Dependencies installed successfully")' &&
echo "Starting health check server..." &&
python -c 'from health_check import HealthCheckServer; import time; server = HealthCheckServer(); server.start(); time.sleep(2); print("Health check server started")' &
echo "Starting main application..." &&
python main.py
EOF
]
}
@ -94,12 +109,15 @@ job "poe-sensor" {
MQTT_PORT = "1883"
MQTT_USERNAME = "relay"
MQTT_PASSWORD = "Sey@K9c&Q4^"
# Health check configuration
HEALTH_CHECK_ENABLED = "true"
HEALTH_CHECK_PORT = "8080"
}
# Resource allocation
# Resource allocation - increased for stability
resources {
cpu = 256
memory = 512
cpu = 512 # Increased from 256
memory = 1024 # Increased from 512
}
# Logs configuration