Skip to content

Commit

Permalink
small refactor to encounter missing logs
Browse files Browse the repository at this point in the history
  • Loading branch information
david-piiano committed Dec 11, 2023
1 parent e1e746f commit e2f3bb0
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 7 deletions.
8 changes: 1 addition & 7 deletions test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -88,18 +88,12 @@ check_log_for_pattern()
done

echo "Pattern not found in $service_name after $max_attempts attempts. Bailing out..."
set -x
curl --no-progress-meter http://localhost:8123/api/pvlt/1.0/ctl/info/health
docker compose logs
return 1
}

wait_until_vault_is_up()
{
check_log_for_pattern "vault-demo-piiano-vault-1" "Serving HTTP on 0.0.0.0:8123"
if [ "$?" -eq 1 ] ; then
exit 1
fi
./wait-for-service.sh http://localhost:8123/api/pvlt/1.0/ctl/info/health
}

check_vault_health()
Expand Down
46 changes: 46 additions & 0 deletions wait-for-service.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#!/bin/bash
IFS=$'\n\t'

# First argument is the service URL that defaults to "http://localhost:3000"
service_url="${1:-http://localhost:3000}"

# Second optional parameter - max attempts
max_attempts="${2:-10}"

# Third, optional argument - PID to follow. If it dies, exit
PID="${3:-0}"

SLEEP_BETWEEN_ATTEMPTS_SECS=3

echo "Wait for service on ${service_url} for ${max_attempts} and PID=${PID}"

# Poll the service until it's up or until the maximum number of attempts is reached
attempt_counter=0
while [ "$attempt_counter" -lt "$max_attempts" ]; do
response=$(curl --write-out '%{http_code}' --silent --output /dev/null "$service_url" || true)

if [ "$response" -eq 200 ]; then
echo "Service is up and returned HTTP 200"
exit 0
else
# print only every 10 messages - do not flood the console
if [ $((attempt_counter % 10)) -eq 0 ]; then
echo "Service returned HTTP $response. Retrying ${attempt_counter}..."
fi
((attempt_counter++))
if [ "$attempt_counter" -eq "$max_attempts" ]; then
echo "Max attempts reached. Exiting."
exit 1
fi
if [ ${PID} -ne 0 ] ; then
if ! ps -p ${PID} > /dev/null 2>&1 ; then
echo "Process ${PID} has died, exit."
exit 1
fi
fi
sleep ${SLEEP_BETWEEN_ATTEMPTS_SECS}
fi
done

echo "Service is not up after timeout. Give up"
exit 1

0 comments on commit e2f3bb0

Please sign in to comment.