diff --git a/pub_event[rungame,endgame](sync).ash b/pub_event[rungame,endgame](sync).ash index c792c2a..74bd9c3 100644 --- a/pub_event[rungame,endgame](sync).ash +++ b/pub_event[rungame,endgame](sync).ash @@ -49,30 +49,48 @@ fi # 2. Read the payload safely PAYLOAD=$(cat "$eventfile") -# 3. Wait for connection to rclone endpoint -connect_success=0 +# Step 3: Connection Check for i in $(seq 5); do - log "INFO" "Waiting on rclone endpoint... $i" - [ "$(nc -z "$RCLONE_ENDPOINT" "$RCLONE_PORT")" ] || connect_success=1 - [ "$connect_success" ] && break - 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 -[ "$connect_success" ] || ( log "ERROR" "Remote repository not accessable" && exit 1 ) - -# 4. Publish and log success/failure -if [ "$(mosquitto_pub -h 127.0.0.1 -p 1883 -t "$TOPIC" "$PAYLOAD")" ]; then +# 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." +else log "ERROR" "Failed to connect to mosquitto broker." exit 1 -else - log "INFO" "Successfully published event." fi # 5. Wait for response from daemon -mosquitto_sub -h 127.0.0.1 -p 1883 -q 0 -t "$TOPIC" | while IFS="=" read -r key value +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" ) & +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 - case "$key" in - "SaveSync") call_exit "$value";; - esac + # 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 diff --git a/savesync.conf b/savesync.conf index cd0bd2e..3368103 100644 --- a/savesync.conf +++ b/savesync.conf @@ -4,5 +4,6 @@ export LOG_FILE="/recalbox/share/system/logs/savesync_monitor.log" export ERROR_FILE="/recalbox/share/system/logs/savesync_error.log" export DEBUG_MODE=1 export REMOTE_BASE="saves:gamepi-tv" -export TOPIC="/Recalbox/EmulationStation/Event" -export LOG_TOPIC="/Recalbox/Log/Service" +export TOPIC="/Recalbox/SaveSync/Event" +export LOG_TOPIC="/Recalbox/SaveSync/Service" +export RESPONSE_TOPIC="/Recalbox/SaveSync/Response"