From 5b6aca00726437b4348668093bdebc660aab9b75 Mon Sep 17 00:00:00 2001 From: Valentin Lab Date: Mon, 17 Dec 2018 23:35:16 +0100 Subject: [PATCH] fix: db connection, wait if receiving "the database system is starting up". --- bin/compose-core | 32 ++++++++++++++++++++++++++------ 1 file changed, 26 insertions(+), 6 deletions(-) diff --git a/bin/compose-core b/bin/compose-core index 9e5e5c6..f34aacb 100755 --- a/bin/compose-core +++ b/bin/compose-core @@ -807,7 +807,7 @@ export -f wait_for_tcp_port ## Warning: requires a ``ddb`` matching current database to be checked wait_for_docker_ip() { - local name=$1 DOCKER_IP= DOCKER_NETWORK= docker_ips= docker_ip= + local name=$1 DOCKER_IP= DOCKER_NETWORK= docker_ips= docker_ip= elapsed timeout=10 docker_ip=$(wait_docker_ip "$name" 5) || return 1 IFS=: read DOCKER_NETWORK DOCKER_IP <<<"$docker_ip" if ! str_is_ipv4 "$DOCKER_IP"; then @@ -823,12 +823,32 @@ wait_for_docker_ip() { done < <(image_exposed_ports_0 "$container_id") ## Checking direct connection - if ! echo "SELECT 1;" | ddb >/dev/null; then - err "${RED}db connection error${NORMAL}:"\ + timeout=10 + start=$SECONDS + while true; do + if err=$(echo "SELECT 1;" | ddb 2>&1 >/dev/null); then + break + fi + if ! [[ "$err" == *"the database system is starting up" ]]; then + err "${RED}db connection error${NORMAL}:" \ + "Could not connect to db on $DOCKER_IP container's IP." + echo " Note: IP up, TCP ports is(are) open" >&2 + if [ "$err" ]; then + echo " Error:" >&2 + printf "%s\n" "$err" | prefix " ${RED}!${NORMAL} " >&2 + fi + return 18 + fi + debug "Got starting up error '$err'." + elapsed=$((SECONDS - start)) + if ((elapsed > timeout)); then + err "${RED}db connection error${NORMAL}:"\ "Could not connect to db on $DOCKER_IP" \ - "container's IP. (IP up, TCP ports is(are) open)" - return 18 - fi + "container's IP. (IP up, TCP ports is(are) open, sql answer after ${timeout}s)" + return 1 + fi + sleep 0.2 + done echo "${DOCKER_NETWORK}:${DOCKER_IP}" return 0 }