Created
October 9, 2018 14:52
-
-
Save rosskevin/d4ce47d790bde9a6c2c2e478eb55b210 to your computer and use it in GitHub Desktop.
swarm services wait for start
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# This function checks if the service is in Running state | |
check_service_is_running() { | |
local SERVICE_NAME=$1 | |
local STATE=$(docker service ps --format '{{json .CurrentState}}' $SERVICE_NAME) | |
if [[ $STATE = \"Running* ]]; then | |
echo 1 | |
else | |
echo 0 | |
fi | |
} | |
# This function waits for the service to become available. | |
# Retries for 10 times and 3 second interval (hard coded for now) | |
wait_for_service_to_start() { | |
local n=1 | |
local max=20 | |
local delay=3 | |
local SERVICE_NAME=$1 | |
local SERVICE_IS_RUNNING=0 | |
while [ "$SERVICE_IS_RUNNING" -eq 0 ]; do | |
if [[ $n -gt $max ]]; then | |
echo "ERROR: Retried $(($n-1)) times but $SERVICE_NAME didn't start. Exiting" >&2 | |
docker service ls | |
echo "Logs for app_web:" | |
docker service logs app_web | |
echo "Logs for failed $SERVICE_NAME:" | |
docker service logs $SERVICE_NAME | |
exit 1 | |
fi | |
SERVICE_IS_RUNNING=$(check_service_is_running $SERVICE_NAME) | |
echo "Waiting for $SERVICE_NAME to start" | |
n=$[$n+1] | |
sleep $delay | |
done | |
echo "$SERVICE_NAME is Running" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment