Updated so I win the race

This commit is contained in:
2026-02-27 20:43:01 -06:00
parent e6f55965d8
commit 36f78fb8b1

View File

@@ -1,97 +1,52 @@
#!/bin/ash
# shellcheck shell=ash
# shellcheck shell=dash
# -- Load Configs ---
# 1. Load Configs - Use absolute paths
. /recalbox/share/system/configs/savesync/savesync.conf
# --- Logger Function ---
# 2. Setup Variables
EVENT_FILE="/tmp/es_state.inf"
RESPONSE_TOPIC="recalbox/savesync/response"
TIMEOUT_SEC=10
log() {
local level="$1"
local msg="$2"
local timestamp
timestamp=$(date '+%Y-%m-%d %H:%M:%S')
local log_line="[$timestamp] [$level] $msg"
# Handle local emergency error log immediately
if [ "$level" = "ERROR" ]; then
mkdir -p "$(dirname "$ERROR_LOG")"
printf "%s\n" "$log_line" >>"$ERROR_LOG"
fi
# 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"
# Ensure port is 1883
mosquitto_pub -h 127.0.0.1 -p 1883 -q 0 -t "$LOG_TOPIC" -m "SaveLog=[$(date '+%H:%M:%S')] [$1] $2" 2>/dev/null
}
# --- Exit function ---
call_exit() {
value="$1"
if [ "$value" -eq 0 ]; then
log "WARN" "pub_event exited normally"
else
log "INFO" "pub_event exited with warnings"
fi
exit "$value"
}
# 3. Check Payload
[ ! -s "$EVENT_FILE" ] && exit 0
PAYLOAD=$(cat "$EVENT_FILE")
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."
# 4. Check Connection quickly (1 second max)
if ! nc -z -w 1 "$RCLONE_ENDPOINT" "$RCLONE_PORT"; then
log "ERROR" "Remote repo offline. Skipping sync to avoid hang."
exit 1
fi
# 2. Read the payload safely
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
done
# Step 4: Publish
# Added -m flag and removed the [ "$( ... )" ] wrapper
# 5. Publish Request
if mosquitto_pub -h 127.0.0.1 -p 1883 -t "$TOPIC" -m "$PAYLOAD"; then
log "INFO" "Successfully published event."
log "INFO" "Event published. Waiting for Daemon..."
else
log "ERROR" "Failed to connect to mosquitto broker."
log "ERROR" "MQTT Broker unreachable."
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
# 6. The Wait (With Timeout)
# We listen for the daemon to say "SaveContinue=0"
(
sleep 10
sleep "$TIMEOUT_SEC"
mosquitto_pub -h 127.0.0.1 -t "$RESPONSE_TOPIC" -m "SaveSync=timeout"
) &
TIMEOUT_PID=$!
TIMER_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
# Wait for exactly 1 message
RESPONSE=$(mosquitto_sub -h 127.0.0.1 -p 1883 -t "$RESPONSE_TOPIC" -C 1)
kill "$TIMER_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
;;
case "$RESPONSE" in
*"timeout"*) log "ERROR" "Daemon timed out. Starting game with local save." ;;
*) log "INFO" "Daemon finished. Proceeding." ;;
esac
done
exit 0