Skip to content

Instantly share code, notes, and snippets.

@calexandrepcjr
Created November 12, 2019 22:56
Show Gist options
  • Save calexandrepcjr/681c2bfa1ca648fcea378059fbd0c733 to your computer and use it in GitHub Desktop.
Save calexandrepcjr/681c2bfa1ca648fcea378059fbd0c733 to your computer and use it in GitHub Desktop.
Script to help pipelines detect postgres database containers ready
#!/bin/bash
triesLimit=20 # Tries for 10s
isDatabaseReady() {
local lastContainerName=$(docker ps -lq)
local databaseReadyLogMessage="database system is ready to accept connections"
echo $(docker logs $lastContainerName --tail 1 | grep -c "$databaseReadyLogMessage")
}
waitForDatabase() {
local tries=0
until [ $tries -gt $triesLimit -o $(isDatabaseReady) -gt 0 ]
do
sleep .5s
((tries++))
done
}
waitForDatabase
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment