Skip to content

Commit

Permalink
Add health_check_no.sh script for Docker and non-Docker health checks (
Browse files Browse the repository at this point in the history
…#3108)

* Refactor health checks in GitHub Actions workflow

* corrected script name

* corrected port number

* Add comments and change the file name

* removed unnecessary code

* add code rabbit suggestions

* fix validation regex patterns

---------

Co-authored-by: im-vedant <194vedantgutpa@gmail.com>
  • Loading branch information
im-vedant and im-vedant authored Jan 1, 2025
1 parent 565b216 commit 96a343e
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 44 deletions.
50 changes: 6 additions & 44 deletions .github/workflows/pull-request.yml
Original file line number Diff line number Diff line change
Expand Up @@ -341,20 +341,8 @@ jobs:
echo $! > .pidfile_prod
- name: Check if Production App is running
run: |
timeout=120
echo "Starting production health check with ${timeout}s timeout"
while ! nc -z localhost 4173 && [ $timeout -gt 0 ]; do
sleep 1
timeout=$((timeout-1))
if [ $((timeout % 10)) -eq 0 ]; then
echo "Still waiting for production app to start... ${timeout}s remaining"
fi
done
if [ $timeout -eq 0 ]; then
echo "Timeout waiting for production application to start"
exit 1
fi
echo "Production app started successfully"
chmod +x .github/workflows/scripts/app_health_check.sh
.github/workflows/scripts/app_health_check.sh 4173 120
- name: Stop Production App
run: |
if [ -f .pidfile_prod ]; then
Expand All @@ -366,20 +354,8 @@ jobs:
echo $! > .pidfile_dev
- name: Check if Development App is running
run: |
timeout=120
echo "Starting development health check with ${timeout}s timeout"
while ! nc -z localhost 4321 && [ $timeout -gt 0 ]; do
sleep 1
timeout=$((timeout-1))
if [ $((timeout % 10)) -eq 0 ]; then
echo "Still waiting for development app to start... ${timeout}s remaining"
fi
done
if [ $timeout -eq 0 ]; then
echo "Timeout waiting for development application to start"
exit 1
fi
echo "Development app started successfully"
chmod +x .github/workflows/scripts/app_health_check.sh
.github/workflows/scripts/app_health_check.sh 4321 120
- name: Stop Development App
if: always()
run: |
Expand Down Expand Up @@ -415,22 +391,8 @@ jobs:
echo "Docker container started successfully"
- name: Check if Talawa Admin App is running
run: |
timeout="${HEALTH_CHECK_TIMEOUT:-120}"
echo "Starting health check with ${timeout}s timeout"
while ! nc -z localhost 4321 && [ $timeout -gt 0 ]; do
sleep 1
timeout=$((timeout-1))
if [ $((timeout % 10)) -eq 0 ]; then
echo "Still waiting for app to start... ${timeout}s remaining"
fi
done
if [ $timeout -eq 0 ]; then
echo "Timeout waiting for application to start"
echo "Container logs:"
docker logs talawa-admin-app-container
exit 1
fi
echo "Port check passed, verifying health endpoint..."
chmod +x .github/workflows/scripts/app_health_check.sh
.github/workflows/scripts/app_health_check.sh 4321 120 true
- name: Stop Docker Container
if: always()
run: |
Expand Down
75 changes: 75 additions & 0 deletions .github/workflows/scripts/app_health_check.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
#!/bin/bash

# This script performs a health check to ensure an application is running on a specified port.
# The script uses netcat (nc) to check if the port is open, with a configurable timeout.
# It also includes optional logic to fetch Docker container logs if the health check fails during a Docker-based test.

# Variables:
# port="$1" - The port to check (passed as the first argument to the script).
# timeout="${2:-120}" - The maximum time in seconds to wait for the application to start. Defaults to 120 seconds if not provided.
# is_docker_test="${3:-false}" - A flag to indicate whether the script is being run in a Docker-based test. Defaults to false.

# Logic:
# 1. Print a message indicating the start of the health check.
# 2. Enter a loop to repeatedly check if the port is open using `nc -z localhost "${port}"`.
# - If the port is not open and the timeout has not expired, sleep for 1 second and decrement the timeout.
# - Print a status message every 10 seconds with the remaining time.
# 3. If the timeout expires, print an error message and, if in Docker test mode, fetch Docker logs for debugging.
# 4. If the port is detected as open, print a success message and exit.

# Script:

port="$1"
timeout="${2:-120}"
is_docker_test="${3:-false}"


# Validate required port parameter
if [ -z "${port}" ] || ! [[ "${port}" =~ ^[0-9]+$ ]] || [ "${port}" -lt 1 ] || [ "${port}" -gt 65535 ]; then
echo "Error: Invalid or missing port number. Must be between 1-65535"
exit 1
fi

# Validate timeout parameter
if ! [[ "${timeout}" =~ ^[0-9]+$ ]] || [ "${timeout}" -lt 1 ]; then
echo "Error: Invalid timeout value. Must be a positive integer"
exit 1
fi

# Validate is_docker_test parameter
if [ "${is_docker_test}" != "true" ] && [ "${is_docker_test}" != "false" ]; then
echo "Error: is_docker_test must be either 'true' or 'false'"
exit 1
fi

echo "Starting health check with ${timeout}s timeout"
while [ "${timeout}" -gt 0 ]; do
if nc -z localhost "${port}" 2>/dev/null; then
break
elif [ $? -ne 1 ]; then
echo "Error: Failed to check port ${port} (nc command failed)"
exit 1
fi
sleep 1
timeout=$((timeout-1))
if [ $((timeout % 10)) -eq 0 ]; then
echo "Waiting for app to start on port ${port}... ${timeout}s remaining"
# Try to get more information about the port status
netstat -an | grep "${port}" || true
fi
done

if [ "${timeout}" -eq 0 ]; then
echo "Error: Timeout waiting for application to start on port ${port}"
echo "System port status:"
netstat -an | grep "${port}" || true
if [ "${is_docker_test}" = "true" ]; then
echo "Fetching Docker container logs..."
if ! docker logs talawa-admin-app-container; then
echo "Error: Failed to fetch logs from container talawa-admin-app-container"
fi
fi
exit 1
fi
echo "App started successfully on port ${port}"

0 comments on commit 96a343e

Please sign in to comment.