Speedy yet?

This commit is contained in:
2026-02-27 21:19:38 -06:00
parent 0028a864cc
commit ddd9df5b36
2 changed files with 65 additions and 81 deletions

View File

@@ -1,51 +1,48 @@
#!/bin/ash
# shellcheck shell=dash
# 1. Load Configs - Use absolute paths
. /recalbox/share/system/configs/savesync/savesync.conf
# 2. Setup Variables
# Constants
EVENT_FILE="/tmp/es_state.inf"
RESPONSE_TOPIC="recalbox/savesync/response"
TIMEOUT_SEC=10
log() {
# 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
mosquitto_pub -h 127.0.0.1 -p 1883 -q 0 -t "$LOG_TOPIC" -m "SaveLog=[$(date '+%H:%M:%S')] [PUB] $1" 2>/dev/null
}
# 3. Check Payload
# 1. Validation
[ ! -s "$EVENT_FILE" ] && exit 0
PAYLOAD=$(cat "$EVENT_FILE")
# 4. Check Connection quickly (1 second max)
#nc -z -w 1 "$RCLONE_ENDPOINT" "$RCLONE_PORT" || { log "ERROR" "Offline"; exit 1; }
log "DEBUG" "Checking connection to: ${RCLONE_ENDPOINT}:${RCLONE_PORT}"
nc -zv -w 1 "$RCLONE_ENDPOINT" "$RCLONE_PORT" 2>&1 | log "DEBUG" "$(cat)"
# 2. Network Check (Short-circuit exit)
nc -z -w 1 "$RCLONE_ENDPOINT" "$RCLONE_PORT" || {
log "Offline: Skipping sync"
exit 0
}
# 5. Publish Request
if mosquitto_pub -h 127.0.0.1 -p 1883 -t "$TOPIC" -m "$PAYLOAD"; then
log "INFO" "Event published. Waiting for Daemon..."
else
log "ERROR" "MQTT Broker unreachable."
# 3. Publish Event
mosquitto_pub -h 127.0.0.1 -p 1883 -t "$TOPIC" -m "$PAYLOAD" || {
log "MQTT Fail"
exit 1
fi
}
# 6. The Wait (With Timeout)
# We listen for the daemon to say "SaveContinue=0"
# 4. Wait for Response (Crucial: Using -C 1 and the same Response Key)
(
sleep "$TIMEOUT_SEC"
mosquitto_pub -h 127.0.0.1 -t "$RESPONSE_TOPIC" -m "SaveSync=timeout"
) &
TIMER_PID=$!
# Wait for exactly 1 message
log "Waiting for Daemon response..."
# Listen for the specific "SaveSync" key
RESPONSE=$(mosquitto_sub -h 127.0.0.1 -p 1883 -t "$RESPONSE_TOPIC" -C 1)
kill "$TIMER_PID" 2>/dev/null
case "$RESPONSE" in
*"timeout"*) log "ERROR" "Daemon timed out. Starting game with local save." ;;
*) log "INFO" "Daemon finished. Proceeding." ;;
*"timeout"*) log "WARN: Daemon timed out. Starting game anyway." ;;
*"0"*) log "INFO: Sync Complete. Launching game." ;;
*) log "INFO: Received response: $RESPONSE" ;;
esac
exit 0