Skip to content

Instantly share code, notes, and snippets.

@jonico
Created October 4, 2021 21:36
Show Gist options
  • Save jonico/b4b4bd4493a417dd51e8b7c4590dc0d3 to your computer and use it in GitHub Desktop.
Save jonico/b4b4bd4493a417dd51e8b7c4590dc0d3 to your computer and use it in GitHub Desktop.
shell script that waits for a PlanetScale branch to be ready for use and increases retry times exponentially
#!/bin/sh
# shell script that waits for a PlanetScale branch to be ready for use and increases retry times exponentially
# usage: wait-for-ps-branch-readiness.sh <db name> <branch name> <max retries>
function wait_for_branch_readiness {
local retries=$1
local db=$2
local branch=$3
local count=0
echo "Checking if branch $branch is ready for use..."
while true; do
local raw_output=`pscale branch list $db --format json`
# check return code, if not 0 then error
if [ $? -ne 0 ]; then
echo "Error: pscale branch list returned non-zero exit code $?: $raw_output"
return 1
fi
local output=`echo $raw_output | jq ".[] | select(.name == \"$branch\") | .ready"`
# test whether output is false, if so, increase wait timeout exponentially
if [ "$output" == "false" ]; then
local wait=$((2**$count))
echo "Branch $branch is not ready yet. Retrying in $wait seconds..."
sleep $wait
count=$((count+1))
if [ $retries -eq $count ]; then
echo "Branch $branch is not ready after $retries retries. Exiting..."
return 2
fi
elif [ "$output" == "true" ]; then
echo "Branch $branch is ready for use."
return 0
else
echo "Branch $branch in unknown status: $raw_output"
return 3
fi
done
}
wait_for_branch_readiness $3 $1 $2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment