non-blocking logic

This commit is contained in:
2026-02-27 20:21:44 -06:00
parent 6a3fae09d0
commit 36a3299976

View File

@@ -39,41 +39,47 @@ do
this_backup_path=$(echo "$this_save_path" | sed 's|saves|archives|') this_backup_path=$(echo "$this_save_path" | sed 's|saves|archives|')
;; ;;
"Action") this_action="$value" ;; "Action") this_action="$value" ;;
"State") "State")
this_state="$value" this_state="$value"
if [ "$this_state" = "playing" ] && [ "$this_action" = "rungame" ]; then if [ "$this_state" = "playing" ] && [ "$this_action" = "rungame" ]; then
log "INFO" "Game Started: $(basename "$this_game_path")" # We wrap the whole operation in ( ) & to background the entire sequence
(
filename=$(basename "$this_save_path") log "INFO" "Game Started: $(basename "$this_game_path")"
remote_full="$REMOTE_BASE/$this_system_id/$filename"
filename=$(basename "$this_save_path")
remote_full="$REMOTE_BASE/$this_system_id/$filename"
local_size=$(stat -c %s "$this_save_path" 2>/dev/null || echo 0) local_size=$(stat -c %s "$this_save_path" 2>/dev/null || echo 0)
remote_size=$(rclone lsjson "$remote_full" 2>/dev/null | grep -o '"Size":[0-9]*' | cut -d: -f2) remote_size=$(rclone lsjson "$remote_full" 2>/dev/null | grep -o '"Size":[0-9]*' | cut -d: -f2)
: "${remote_size:=0}" : "${remote_size:=0}"
if [ "$local_size" -lt "$remote_size" ]; then if [ "$local_size" -lt "$remote_size" ]; then
log "WARN" "Cloud save LARGER. Restoring..." log "WARN" "Cloud save LARGER. Restoring..."
mkdir -p "$(dirname "$this_backup_path")" mkdir -p "$(dirname "$this_backup_path")"
# Fixed: backup-dir needs to be a directory rclone copyto "$remote_full" "$this_save_path" --backup-dir "$(dirname "$this_backup_path")"
rclone copyto "$remote_full" "$this_save_path" --backup-dir "$(dirname "$this_backup_path")" else
else log "INFO" "Local save safe. Updating..."
log "INFO" "Local save safe. Updating..." rclone update "$remote_full" "$this_save_path"
rclone update "$remote_full" "$this_save_path" fi
fi
# Signal that sync is done
# FIX: Added -m flag mosquitto_pub -h 127.0.0.1 -p 1883 -t "$TOPIC" -m "SaveContinue=0"
mosquitto_pub -h 127.0.0.1 -p 1883 -t "$TOPIC" -m "SaveContinue=0" ) &
log "DEBUG" "Sync process backgrounded for Start Game."
elif [ "$this_state" = "endgame" ]; then elif [ "$this_state" = "endgame" ]; then
log "INFO" "Game Ended. Syncing..." (
rclone update "$this_save_path" "$REMOTE_BASE/$this_system_id/" log "INFO" "Game Ended. Syncing..."
log "INFO" "Sync Complete." rclone update "$this_save_path" "$REMOTE_BASE/$this_system_id/"
log "INFO" "Sync Complete."
mosquitto_pub -h 127.0.0.1 -p 1883 -t "$TOPIC" -m "SaveContinue=0"
mosquitto_pub -h 127.0.0.1 -p 1883 -t "$TOPIC" -m "SaveContinue=0"
) &
log "DEBUG" "Sync process backgrounded for End Game."
fi fi
# Reset variables # Reset variables immediately so the loop is ready for the next line
this_system_id=""; this_game_path=""; this_save_path=""; this_action=""; this_backup_path="" this_system_id=""; this_game_path=""; this_save_path=""; this_action=""; this_backup_path=""
;; ;;
esac esac