Skip to content

Instantly share code, notes, and snippets.

@RayBB
Created July 16, 2020 14:41
Show Gist options
  • Save RayBB/0b442641ad740701ef5c96f5f9ef9dd9 to your computer and use it in GitHub Desktop.
Save RayBB/0b442641ad740701ef5c96f5f9ef9dd9 to your computer and use it in GitHub Desktop.
Wait for all Google App Engine operations to finish before continuing
#!/bin/bash
set -x
printf "%s" "$GAE_CREDENTIALS" > ~/gaecreds.json
gcloud auth activate-service-account --key-file ~/gaecreds.json
gcloud config set project "$GCLOUD_PROJECT"
DEBIAN_FRONTEND=noninteractive
# send install logs to null for cleaner logs
apt-get install jq >/dev/null
# Define your function here
WAITFORGAE () {
GAEJSON=$(gcloud app operations list --format=json --pending)
PENDING_OPERATION_COUNT=$(echo "$GAEJSON" | jq '. | length')
echo "$PENDING_OPERATION_COUNT pending operations"
if [[ $PENDING_OPERATION_COUNT -gt 0 ]]; then
# tr to remove quotes
ID=$(echo "$GAEJSON" | jq '.[0].id' | tr -d '"')=
gcloud app operations wait "$ID"
# Sleep up to 60 seconds to avoid multiple waiting jobs all starting at once and clobbering each other
sleep $(( ( RANDOM % 60 ) + 1 ))
WAITFORGAE
fi
}
# Invoke your function
WAITFORGAE
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment