fix: improve leadership detection using 'litefs status' to prevent redundant Consul registrations
All checks were successful
Build and Push Docker Image / build-and-push (push) Successful in 47s

This commit is contained in:
2026-04-27 08:22:43 -07:00
parent 538ee01b72
commit a8e02ae063

View File

@@ -11,6 +11,37 @@ NODE_IP="${ADVERTISE_IP}"
DB_LOCK_FILE="/litefs/.primary"
NAVIDROME_PID=0
# --- Helper Functions ---
# Check if this node is the LiteFS Primary
check_primary() {
# Role: primary or Role: replica
local role=$(litefs status 2>/dev/null | grep "Role" | awk '{print $2}' || echo "unknown")
if [ "$role" = "primary" ]; then
return 0 # Success, we are primary
fi
return 1 # Not primary
}
# Wait for LiteFS to be "Ready" (connected to cluster)
wait_for_litefs() {
echo "Waiting for LiteFS to initialize..."
local timeout=60
local count=0
while [ $count -lt $timeout ]; do
# Check if LiteFS is ready by looking for any role in status
if litefs status 2>/dev/null | grep -q "Role"; then
echo "LiteFS is initialized."
return 0
fi
sleep 2
count=$((count + 2))
echo -n "."
done
echo "ERROR: LiteFS failed to initialize after ${timeout}s"
return 1
}
# Tags for the Primary service (Traefik enabled)
PRIMARY_TAGS='["navidrome","web","traefik.enable=true","urlprefix-/navidrome","tools","traefik.http.routers.navidromelan.rule=Host(`navidrome.service.dc1.consul`)","traefik.http.routers.navidromewan.rule=Host(`m.fbleagh.duckdns.org`)","traefik.http.routers.navidromewan.middlewares=dex@consulcatalog","traefik.http.routers.navidromewan.tls=true"]'
@@ -122,15 +153,15 @@ echo "Starting Supervisor. Waiting for leadership settle..."
echo "Node IP: $NODE_IP"
echo "Consul: $CONSUL_HTTP_ADDR"
# Small sleep to let LiteFS settle and leadership election complete
sleep 5
# Wait for LiteFS to be fully ready before making decisions
wait_for_litefs || exit 1
LAST_BACKUP_TIME=0
BACKUP_INTERVAL=86400 # 24 hours
while true; do
# In LiteFS 0.5, .primary file exists ONLY on replicas.
if [ ! -f "$DB_LOCK_FILE" ]; then
# Use explicit LiteFS status check instead of file existence
if check_primary; then
# === WE ARE PRIMARY ===
# 1. If App is not running, start it and register