Skip to content

Commit 21e3d1e

Browse files
committed
Make wait_for_service more robust by checking HTTP response
wait_for_service just checked to see if the remote service was started, not that it was returning data. This caused problems when the service was behind a proxy because the proxy would respond quickly but the service may not have fully started. Wait for a non-503 HTTP response code and non-7 exit code (connection error) from curl Return an error if a successful connection cannot be made. Change-Id: I059a12b1b920f703f28aca0e2f352714118dee97
1 parent dbc6a37 commit 21e3d1e

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

functions

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -381,12 +381,24 @@ CURL_GET="${CURL_GET:-curl -g}"
381381

382382
# Wait for an HTTP server to start answering requests
383383
# wait_for_service timeout url
384+
#
385+
# If the service we want is behind a proxy, the proxy may be available
386+
# before the service. Compliant proxies will return a 503 in this case
387+
# Loop until we get something else.
388+
# Also check for the case where there is no proxy and the service just
389+
# hasn't started yet. curl returns 7 for Failed to connect to host.
384390
function wait_for_service {
385391
local timeout=$1
386392
local url=$2
393+
local rval=0
387394
time_start "wait_for_service"
388-
timeout $timeout sh -c "while ! $CURL_GET -k --noproxy '*' -s $url >/dev/null; do sleep 1; done"
395+
timeout $timeout bash -x <<EOF || rval=$?
396+
while [[ \$( ${CURL_GET} -k --noproxy '*' -s -o /dev/null -w '%{http_code}' ${url} ) == 503 || \$? -eq 7 ]]; do
397+
sleep 1
398+
done
399+
EOF
389400
time_stop "wait_for_service"
401+
return $rval
390402
}
391403

392404

0 commit comments

Comments
 (0)