50 lines
1.2 KiB
Bash

#!/bin/bash
# Run Valkey command
vcli() {
valkey_cli -h 127.0.0.1 -P "${VALKEY_PORT}" "$@"
return $?
}
# Run Sentinel command
vscli() {
valkey-cli -h "$VALKEY_SERVICE" -p "$VALKEY_SENTINEL_PORT" sentinel "$@"
return $?
}
sentinelFailOverFinished() {
# Check if the failover is finished
local failoverStatus
primaryInfo=$(vscli get-primary-by-name "mymaster")
primaryHost=${primaryInfo[0]}
fullPrimaryHostname="${primaryHost}.${HEADLESS_SERVICE}"
[[ "${fullPrimaryHostname}" == "${HOSTNAME}}" ]]
}
if [ "${VALKEY_ROLE}" = "replication"]; then
echo "Stopping replication"
ROLE=$(vcli role | head 1)
if [ "${ROLE}" = "master" ]; then
#Pausing write connections to avoid data loss"
vcli CLIENT PAUSE "22000"
echo "Failover in progress"
vscli failover "mymaster"
i=0
while true; do
sentinelFailOverFinished
if [ $? -eq 0 ]; then
echo "Failover finished"
break
fi
sleep 1
i=$((i + 1))
if [ $i -gt 60 ]; then
echo "Failover timed out"
exit 1
fi
done
else
exit 0
fi
fi