Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

helper healthcheck methods #801

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 102 additions & 2 deletions community_images/common/scripts/bash_helper.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,14 @@ set -e
# Successive backoffs double the timeout.
#
# Beware of set -e killing your whole script!
function with_backoff {
with_backoff()
{
local max_attempts="${ATTEMPTS-9}"
local timeout="${TIMEOUT-5}"
local attempt=0
local exitCode=0

while [[ "$attempt" < "$max_attempts" ]]
while [[ "$attempt" -lt "$max_attempts" ]]
do
set +e
"$@"
Expand All @@ -43,3 +44,102 @@ function with_backoff {

return "$exitCode"
}

probe_process_running_inside_container_timeout()
{
# How to use:
# probe_process_running_inside_container_timeout "container_name" "process_name" 30

local container_name="${1}"
if [ -z "${container_name}" ]; then
echo "${container_name}" required. skipping wait
return 1
fi

local process_name="${2}"
if [ -z "${process_name}" ]; then
echo "${process_name}" required. skipping wait
return 1
fi

local timeout="${3:-30}" # default max is 30 secs
local max_attempts
local attempt=1
local itr_timeout=1
local found=false

max_attempts=$((timeout/itr_timeout))

set +e

while [ $attempt -le $max_attempts ]; do
echo "Attempt $attempt/$max_attempts: Checking for $process_name in container $container_name ..."

if docker top "$container_name" | grep -q "$process_name"; then
found=true
break
else
echo "Process $process_name is not running. Retrying in $itr_timeout seconds..."
attempt=$((attempt + 1))
sleep 1
fi
done

set -e

if $found; then
echo "$process_name is running inside container $container_name"
return 0
else
echo "$process_name is NOT running inside container $container_name"
return 1
fi
}

probe_endpoint_health()
{
# How to use:
# probe_endpoint_health "http://localhost:8080/health" 30

local url="${1}"
local timeout="${2:-30}"
if [ -z "${url}" ]; then
echo "${url}" required. skipping wait
return 1
fi

local max_attempts
local attempt=1
local itr_timeout=1
local success=false
local http_code

max_attempts=$((timeout/itr_timeout))

set +e

while [ $attempt -le $max_attempts ]; do

echo "Attempt $attempt/$max_attempts: Checking URL $url..."
http_code=$(curl --head --location --connect-timeout 5 --write-out "%{http_code}" --silent --output /dev/null "${url}")

if [ "$http_code" -eq 200 ]; then
success=true
break
else
echo "Failed: HTTP $url status code $http_code. Retrying in $itr_timeout seconds..."
attempt=$((attempt + 1))
sleep $itr_timeout
fi
done


if $success; then
echo "Success ${url} - with status $http_code"
return 0
else
echo "Error ${url} - failed with status $http_code"
return 1
fi
}

Loading