diff --git a/start-mongodb.sh b/start-mongodb.sh index ca9337c..000ce66 100644 --- a/start-mongodb.sh +++ b/start-mongodb.sh @@ -8,6 +8,24 @@ MONGODB_DB=$4 MONGODB_USERNAME=$5 MONGODB_PASSWORD=$6 +wait_for_mongodb () { + echo "::group::Waiting for MongoDB to accept connections" + sleep 1 + TIMER=0 + + until docker exec --tty mongodb $MONGO_CLIENT --port $MONGODB_PORT --eval "db.serverStatus()" # &> /dev/null + do + sleep 1 + echo "." + TIMER=$((TIMER + 1)) + + if [[ $TIMER -eq 20 ]]; then + echo "MongoDB did not initialize within 20 seconds. Exiting." + exit 2 + fi + done + echo "::endgroup::" +} if [ -z "$MONGODB_VERSION" ]; then echo "" @@ -45,6 +63,8 @@ if [ -z "$MONGODB_REPLICA_SET" ]; then fi echo "::endgroup::" + wait_for_mongodb + return fi @@ -63,24 +83,7 @@ if [ $? -ne 0 ]; then fi echo "::endgroup::" - -echo "::group::Waiting for MongoDB to accept connections" -sleep 1 -TIMER=0 - -until docker exec --tty mongodb $MONGO_CLIENT --port $MONGODB_PORT --eval "db.serverStatus()" # &> /dev/null -do - sleep 1 - echo "." - TIMER=$((TIMER + 1)) - - if [[ $TIMER -eq 20 ]]; then - echo "MongoDB did not initialize within 20 seconds. Exiting." - exit 2 - fi -done -echo "::endgroup::" - +wait_for_mongodb echo "::group::Initiating replica set [$MONGODB_REPLICA_SET]"