From 0328a43814e0a6040cace80ec7acd5d37c308512 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcus=20P=C3=B6hls?= Date: Sun, 30 Jul 2023 16:47:41 +0200 Subject: [PATCH] wait for mongodb --- start-mongodb.sh | 39 +++++++++++++++++++++------------------ 1 file changed, 21 insertions(+), 18 deletions(-) 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]"