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 #!/bin/ash
# shellcheck shell=ash # shellcheck shell=dash
# -- Load Configs --- # 1. Load Configs - Use absolute paths
. /recalbox/share/system/configs/savesync/savesync.conf . /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() { log() {
local level="$1" # Ensure port is 1883
local msg="$2" 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
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"
} }
# --- Exit function --- # 3. Check Payload
call_exit() { [ ! -s "$EVENT_FILE" ] && exit 0
value="$1" PAYLOAD=$(cat "$EVENT_FILE")
if [ "$value" -eq 0 ]; then
log "WARN" "pub_event exited normally"
else
log "INFO" "pub_event exited with warnings"
fi
exit "$value"
}
eventfile="/tmp/es_state.inf" # 4. Check Connection quickly (1 second max)
if ! nc -z -w 1 "$RCLONE_ENDPOINT" "$RCLONE_PORT"; then
# 1. Check if the file exists and is not empty log "ERROR" "Remote repo offline. Skipping sync to avoid hang."
if [ ! -s "$eventfile" ]; then
log "ERROR" "$eventfile is missing or empty."
exit 1 exit 1
fi fi
# 2. Read the payload safely # 5. Publish Request
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
if mosquitto_pub -h 127.0.0.1 -p 1883 -t "$TOPIC" -m "$PAYLOAD"; then 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 else
log "ERROR" "Failed to connect to mosquitto broker." log "ERROR" "MQTT Broker unreachable."
exit 1 exit 1
fi fi
# 5. Wait for response from daemon # 6. The Wait (With Timeout)
log "INFO" "Waiting for sync confirmation on $RESPONSE_TOPIC..." # We listen for the daemon to say "SaveContinue=0"
# Start a subshell that kills the subscriber after 10 seconds if no message arrives
( (
sleep 10 sleep "$TIMEOUT_SEC"
mosquitto_pub -h 127.0.0.1 -t "$RESPONSE_TOPIC" -m "SaveSync=timeout" 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 # Wait for exactly 1 message
mosquitto_sub -h 127.0.0.1 -p 1883 -t "$RESPONSE_TOPIC" -C 1 | while IFS="=" read -r key value; do RESPONSE=$(mosquitto_sub -h 127.0.0.1 -p 1883 -t "$RESPONSE_TOPIC" -C 1)
# Kill the background sleep timer since we got a message kill "$TIMER_PID" 2>/dev/null
kill "$TIMEOUT_PID" 2>/dev/null
case "$key" in case "$RESPONSE" in
"SaveSync") *"timeout"*) log "ERROR" "Daemon timed out. Starting game with local save." ;;
if [ "$value" = "timeout" ]; then *) log "INFO" "Daemon finished. Proceeding." ;;
log "ERROR" "Sync timed out! Daemon might be down." esac
call_exit 1
else exit 0
log "INFO" "Sync confirmed with status: $value"
call_exit "$value"
fi
;;
esac
done