Browse code

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

Rob Crittenden authored on 2016/05/07 01:35:22
Showing 1 changed files
... ...
@@ -381,12 +381,24 @@ CURL_GET="${CURL_GET:-curl -g}"
381 381
 
382 382
 # Wait for an HTTP server to start answering requests
383 383
 # 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.
384 390
 function wait_for_service {
385 391
     local timeout=$1
386 392
     local url=$2
393
+    local rval=0
387 394
     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
389 400
     time_stop "wait_for_service"
401
+    return $rval
390 402
 }
391 403
 
392 404