How to wait for Firestore indexes to deploy?

170 views Asked by At

When deploying with firebase deploy the index deploy phase finishes automatically but indexes can take a long time (10+ minutes) to actually be ready to use.

How can I make my deploy script wait for the indexes to be actually ready before continuing to push code that depends on the index?

1

There are 1 answers

0
Sam Stern On

This gcloud command will show operations in progress:

$ gcloud firestore operations list --format=json --filter="done:false"
[
  {
    "metadata": {
    "@type": "type.googleapis.com/google.firestore.admin.v1.IndexOperationMetadata",
      "index": "projects/foo/databases/(default)/collectionGroups/bar/indexes/CICAgJiljJcK",
      "progressDocuments": {
        "estimatedWork": "1133"
      },
      "startTime": "2023-01-18T16:05:43.603428Z",
      "state": "INITIALIZING"
    },
    "name": "projects/foo/databases/(default)/operations/S2NKamxpSmdBQ0lDDCoDIGI5ZjRjOWNiYTc0MC05MDlhLWJhYzQtM2M5MC1jMjQwMmI3NCQadGx1YWZlZAcSMXJoLXJleGVkbmktbmltZGERClIS"
  }
]

When there is nothing in progress, it will return []. We can write a bash script to wait for that:

starttime=$(date +"%s")
nowtime=$(date +"%s")
    
# Run for up to 30 minutes (1800 seconds)
while [[ $(( $nowtime - $starttime )) -le 1800 ]]
do
    response=$(gcloud --project=column-sam firestore operations list --format=json --filter="done:false")
    if [ "$response" == "[]" ]; then
      echo "Finished"
      exit 0
    fi
    
    echo "[$nowtime] Indexes still building, waiting 10 seconds"
    sleep 10
    nowtime=$(date +"%s")
done
    
echo "[$nowtime] Timed out waiting for indexes to build!"
exit 1