Formatting
This commit is contained in:
@@ -6,25 +6,25 @@
|
||||
|
||||
# --- Logger Function ---
|
||||
log() {
|
||||
local level="$1"
|
||||
local msg="$2"
|
||||
local timestamp
|
||||
|
||||
timestamp=$(date '+%Y-%m-%d %H:%M:%S')
|
||||
local log_line="[$timestamp] [$level] $msg"
|
||||
local level="$1"
|
||||
local msg="$2"
|
||||
local timestamp
|
||||
|
||||
# Handle local emergency error log immediately
|
||||
if [ "$level" = "ERROR" ]; then
|
||||
mkdir -p "$(dirname "$ERROR_LOG")"
|
||||
printf "%s\n" "$log_line" >> "$ERROR_LOG"
|
||||
fi
|
||||
timestamp=$(date '+%Y-%m-%d %H:%M:%S')
|
||||
local log_line="[$timestamp] [$level] $msg"
|
||||
|
||||
# Dispatch to the Central Logging Daemon via MQTT
|
||||
# We use -q 0 (fire and forget) so the game script doesn't wait
|
||||
mosquitto_pub -h 127.0.0.1 -p 1833 -q 0 -t "$LOG_TOPIC" -m "SaveLog=$log_line" 2>/dev/null
|
||||
# Handle local emergency error log immediately
|
||||
if [ "$level" = "ERROR" ]; then
|
||||
mkdir -p "$(dirname "$ERROR_LOG")"
|
||||
printf "%s\n" "$log_line" >>"$ERROR_LOG"
|
||||
fi
|
||||
|
||||
# Local debugging
|
||||
[ "${DEBUG_MODE:-0}" -eq 1 ] && printf "%s\n" "$log_line"
|
||||
# Dispatch to the Central Logging Daemon via MQTT
|
||||
# We use -q 0 (fire and forget) so the game script doesn't wait
|
||||
mosquitto_pub -h 127.0.0.1 -p 1833 -q 0 -t "$LOG_TOPIC" -m "SaveLog=$log_line" 2>/dev/null
|
||||
|
||||
# Local debugging
|
||||
[ "${DEBUG_MODE:-0}" -eq 1 ] && printf "%s\n" "$log_line"
|
||||
}
|
||||
|
||||
# --- Exit function ---
|
||||
@@ -42,8 +42,8 @@ eventfile="/tmp/es_state.inf"
|
||||
|
||||
# 1. Check if the file exists and is not empty
|
||||
if [ ! -s "$eventfile" ]; then
|
||||
log "ERROR" "$eventfile is missing or empty."
|
||||
exit 1
|
||||
log "ERROR" "$eventfile is missing or empty."
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# 2. Read the payload safely
|
||||
@@ -51,46 +51,47 @@ PAYLOAD=$(cat "$eventfile")
|
||||
|
||||
# Step 3: Connection Check
|
||||
for i in $(seq 5); do
|
||||
log "INFO" "Waiting on rclone endpoint... $i"
|
||||
if nc -z "$RCLONE_ENDPOINT" "$RCLONE_PORT"; then
|
||||
connect_success=1
|
||||
break
|
||||
fi
|
||||
sleep 0.5
|
||||
log "INFO" "Waiting on rclone endpoint... $i"
|
||||
if nc -z "$RCLONE_ENDPOINT" "$RCLONE_PORT"; then
|
||||
connect_success=1
|
||||
break
|
||||
fi
|
||||
sleep 0.5
|
||||
done
|
||||
|
||||
# Step 4: Publish
|
||||
# Added -m flag and removed the [ "$( ... )" ] wrapper
|
||||
if mosquitto_pub -h 127.0.0.1 -p 1883 -t "$TOPIC" -m "$PAYLOAD"; then
|
||||
log "INFO" "Successfully published event."
|
||||
log "INFO" "Successfully published event."
|
||||
else
|
||||
log "ERROR" "Failed to connect to mosquitto broker."
|
||||
exit 1
|
||||
log "ERROR" "Failed to connect to mosquitto broker."
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# 5. Wait for response from daemon
|
||||
log "INFO" "Waiting for sync confirmation on $RESPONSE_TOPIC..."
|
||||
|
||||
# Start a subshell that kills the subscriber after 10 seconds if no message arrives
|
||||
( sleep 10; mosquitto_pub -h 127.0.0.1 -t "$RESPONSE_TOPIC" -m "SaveSync=timeout" ) &
|
||||
(
|
||||
sleep 10
|
||||
mosquitto_pub -h 127.0.0.1 -t "$RESPONSE_TOPIC" -m "SaveSync=timeout"
|
||||
) &
|
||||
TIMEOUT_PID=$!
|
||||
|
||||
# -C 1 ensures we exit after receiving either the real response or the timeout message
|
||||
mosquitto_sub -h 127.0.0.1 -p 1883 -t "$RESPONSE_TOPIC" -C 1 | while IFS="=" read -r key value
|
||||
do
|
||||
# Kill the background sleep timer since we got a message
|
||||
kill "$TIMEOUT_PID" 2>/dev/null
|
||||
|
||||
case "$key" in
|
||||
"SaveSync")
|
||||
if [ "$value" = "timeout" ]; then
|
||||
log "ERROR" "Sync timed out! Daemon might be down."
|
||||
call_exit 1
|
||||
else
|
||||
log "INFO" "Sync confirmed with status: $value"
|
||||
call_exit "$value"
|
||||
fi
|
||||
;;
|
||||
esac
|
||||
done
|
||||
mosquitto_sub -h 127.0.0.1 -p 1883 -t "$RESPONSE_TOPIC" -C 1 | while IFS="=" read -r key value; do
|
||||
# Kill the background sleep timer since we got a message
|
||||
kill "$TIMEOUT_PID" 2>/dev/null
|
||||
|
||||
case "$key" in
|
||||
"SaveSync")
|
||||
if [ "$value" = "timeout" ]; then
|
||||
log "ERROR" "Sync timed out! Daemon might be down."
|
||||
call_exit 1
|
||||
else
|
||||
log "INFO" "Sync confirmed with status: $value"
|
||||
call_exit "$value"
|
||||
fi
|
||||
;;
|
||||
esac
|
||||
done
|
||||
|
||||
Reference in New Issue
Block a user