From 4733fdce5be621a22cc3b806b65092cda3780d46 Mon Sep 17 00:00:00 2001 From: im-vedant <194vedantgutpa@gmail.com> Date: Wed, 1 Jan 2025 18:24:45 +0530 Subject: [PATCH 1/7] Refactor health checks in GitHub Actions workflow --- .github/workflows/pull-request.yml | 50 +++----------------- .github/workflows/scripts/health_check_no.sh | 24 ++++++++++ 2 files changed, 30 insertions(+), 44 deletions(-) create mode 100644 .github/workflows/scripts/health_check_no.sh diff --git a/.github/workflows/pull-request.yml b/.github/workflows/pull-request.yml index a209d514e0..75f5e26885 100644 --- a/.github/workflows/pull-request.yml +++ b/.github/workflows/pull-request.yml @@ -340,20 +340,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/health_check.sh + .github/workflows/scripts/health_check.sh 4321 120 - name: Stop Production App run: | if [ -f .pidfile_prod ]; then @@ -365,20 +353,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/health_check_no.sh + .github/workflows/scripts/health_check_no.sh 4321 120 - name: Stop Development App if: always() run: | @@ -414,22 +390,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/health_check.sh + .github/workflows/scripts/health_check.sh 4321 120 true - name: Stop Docker Container if: always() run: | diff --git a/.github/workflows/scripts/health_check_no.sh b/.github/workflows/scripts/health_check_no.sh new file mode 100644 index 0000000000..e53b5d8c5a --- /dev/null +++ b/.github/workflows/scripts/health_check_no.sh @@ -0,0 +1,24 @@ +# .github/workflows/scripts/health_check.sh +#!/bin/bash + +port="$1" +timeout="${2:-120}" +is_docker_test="${3:-false}" +echo "Starting health check with ${timeout}s timeout" +while ! nc -z localhost "${port}" && [ "${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" + if [ "${is_docker_test}" = "true" ]; then + echo "Fetching Docker container logs..." + docker logs talawa-admin-app-container + fi + exit 1 +fi +echo "App started successfully on port ${port}" From 29e7fbb505b4463f12a64c888584c4accf437e79 Mon Sep 17 00:00:00 2001 From: im-vedant <194vedantgutpa@gmail.com> Date: Wed, 1 Jan 2025 18:52:49 +0530 Subject: [PATCH 2/7] corrected script name --- .github/workflows/pull-request.yml | 8 ++++---- .github/workflows/scripts/health_check_no.sh | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/.github/workflows/pull-request.yml b/.github/workflows/pull-request.yml index 75f5e26885..decdd26b6c 100644 --- a/.github/workflows/pull-request.yml +++ b/.github/workflows/pull-request.yml @@ -340,8 +340,8 @@ jobs: echo $! > .pidfile_prod - name: Check if Production App is running run: | - chmod +x .github/workflows/scripts/health_check.sh - .github/workflows/scripts/health_check.sh 4321 120 + chmod +x .github/workflows/scripts/health_check_no.sh + .github/workflows/scripts/health_check_no.sh 4321 120 - name: Stop Production App run: | if [ -f .pidfile_prod ]; then @@ -390,8 +390,8 @@ jobs: echo "Docker container started successfully" - name: Check if Talawa Admin App is running run: | - chmod +x .github/workflows/scripts/health_check.sh - .github/workflows/scripts/health_check.sh 4321 120 true + chmod +x .github/workflows/scripts/health_check_no.sh + .github/workflows/scripts/health_check_no.sh 4321 120 true - name: Stop Docker Container if: always() run: | diff --git a/.github/workflows/scripts/health_check_no.sh b/.github/workflows/scripts/health_check_no.sh index e53b5d8c5a..e0f851ae51 100644 --- a/.github/workflows/scripts/health_check_no.sh +++ b/.github/workflows/scripts/health_check_no.sh @@ -1,4 +1,4 @@ -# .github/workflows/scripts/health_check.sh +# .github/workflows/scripts/health_check_no.sh #!/bin/bash port="$1" From 960a4b9d9da73bb8d0f13c28a55af21bd4161cf5 Mon Sep 17 00:00:00 2001 From: im-vedant <194vedantgutpa@gmail.com> Date: Wed, 1 Jan 2025 19:15:22 +0530 Subject: [PATCH 3/7] corrected port number --- .github/workflows/pull-request.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/pull-request.yml b/.github/workflows/pull-request.yml index decdd26b6c..14f4251709 100644 --- a/.github/workflows/pull-request.yml +++ b/.github/workflows/pull-request.yml @@ -341,7 +341,7 @@ jobs: - name: Check if Production App is running run: | chmod +x .github/workflows/scripts/health_check_no.sh - .github/workflows/scripts/health_check_no.sh 4321 120 + .github/workflows/scripts/health_check_no.sh 4173 120 - name: Stop Production App run: | if [ -f .pidfile_prod ]; then From 3597602a4a6eb9bedaeb8efa1d9680d1d3a0f439 Mon Sep 17 00:00:00 2001 From: im-vedant <194vedantgutpa@gmail.com> Date: Wed, 1 Jan 2025 23:45:47 +0530 Subject: [PATCH 4/7] Add comments and change the file name --- .github/workflows/pull-request.yml | 12 ++--- .github/workflows/scripts/app_health_check.sh | 45 +++++++++++++++++++ .github/workflows/scripts/health_check_no.sh | 24 ---------- 3 files changed, 51 insertions(+), 30 deletions(-) create mode 100644 .github/workflows/scripts/app_health_check.sh delete mode 100644 .github/workflows/scripts/health_check_no.sh diff --git a/.github/workflows/pull-request.yml b/.github/workflows/pull-request.yml index 14f4251709..dc273ed9df 100644 --- a/.github/workflows/pull-request.yml +++ b/.github/workflows/pull-request.yml @@ -340,8 +340,8 @@ jobs: echo $! > .pidfile_prod - name: Check if Production App is running run: | - chmod +x .github/workflows/scripts/health_check_no.sh - .github/workflows/scripts/health_check_no.sh 4173 120 + 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 @@ -353,8 +353,8 @@ jobs: echo $! > .pidfile_dev - name: Check if Development App is running run: | - chmod +x .github/workflows/scripts/health_check_no.sh - .github/workflows/scripts/health_check_no.sh 4321 120 + 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: | @@ -390,8 +390,8 @@ jobs: echo "Docker container started successfully" - name: Check if Talawa Admin App is running run: | - chmod +x .github/workflows/scripts/health_check_no.sh - .github/workflows/scripts/health_check_no.sh 4321 120 true + 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: | diff --git a/.github/workflows/scripts/app_health_check.sh b/.github/workflows/scripts/app_health_check.sh new file mode 100644 index 0000000000..9c6d713fd1 --- /dev/null +++ b/.github/workflows/scripts/app_health_check.sh @@ -0,0 +1,45 @@ +# .github/workflows/scripts/app_health_check.sh +#!/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}" + +echo "Starting health check with ${timeout}s timeout" +while ! nc -z localhost "${port}" && [ "${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" + if [ "${is_docker_test}" = "true" ]; then + echo "Fetching Docker container logs..." + docker logs talawa-admin-app-container + fi + exit 1 +fi +echo "App started successfully on port ${port}" + diff --git a/.github/workflows/scripts/health_check_no.sh b/.github/workflows/scripts/health_check_no.sh deleted file mode 100644 index e0f851ae51..0000000000 --- a/.github/workflows/scripts/health_check_no.sh +++ /dev/null @@ -1,24 +0,0 @@ -# .github/workflows/scripts/health_check_no.sh -#!/bin/bash - -port="$1" -timeout="${2:-120}" -is_docker_test="${3:-false}" -echo "Starting health check with ${timeout}s timeout" -while ! nc -z localhost "${port}" && [ "${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" - if [ "${is_docker_test}" = "true" ]; then - echo "Fetching Docker container logs..." - docker logs talawa-admin-app-container - fi - exit 1 -fi -echo "App started successfully on port ${port}" From 089a4fda2cdbb39e9f49ec1053e6cabae447ff7d Mon Sep 17 00:00:00 2001 From: im-vedant <194vedantgutpa@gmail.com> Date: Wed, 1 Jan 2025 23:49:25 +0530 Subject: [PATCH 5/7] removed unnecessary code --- .github/workflows/scripts/app_health_check.sh | 1 - 1 file changed, 1 deletion(-) diff --git a/.github/workflows/scripts/app_health_check.sh b/.github/workflows/scripts/app_health_check.sh index 9c6d713fd1..6680fb67ba 100644 --- a/.github/workflows/scripts/app_health_check.sh +++ b/.github/workflows/scripts/app_health_check.sh @@ -1,4 +1,3 @@ -# .github/workflows/scripts/app_health_check.sh #!/bin/bash # This script performs a health check to ensure an application is running on a specified port. From 862d2d4526d4fb199d40d60a8d1acff486ceba76 Mon Sep 17 00:00:00 2001 From: im-vedant <194vedantgutpa@gmail.com> Date: Thu, 2 Jan 2025 03:04:32 +0530 Subject: [PATCH 6/7] add code rabbit suggestions --- .github/workflows/scripts/app_health_check.sh | 39 +++++++++++++++++-- 1 file changed, 35 insertions(+), 4 deletions(-) diff --git a/.github/workflows/scripts/app_health_check.sh b/.github/workflows/scripts/app_health_check.sh index 6680fb67ba..6e88bf8cf7 100644 --- a/.github/workflows/scripts/app_health_check.sh +++ b/.github/workflows/scripts/app_health_check.sh @@ -23,20 +23,51 @@ 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 ! nc -z localhost "${port}" && [ "${timeout}" -gt 0 ]; do +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 "Still waiting for app to start... ${timeout}s remaining" + 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 "Timeout waiting for application to start" + 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..." - docker logs talawa-admin-app-container + 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 From e26074fc156adbb8c28e64f9c1eb6df04d2ed219 Mon Sep 17 00:00:00 2001 From: im-vedant <194vedantgutpa@gmail.com> Date: Thu, 2 Jan 2025 03:10:57 +0530 Subject: [PATCH 7/7] fix validation regex patterns --- .github/workflows/scripts/app_health_check.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/scripts/app_health_check.sh b/.github/workflows/scripts/app_health_check.sh index 6e88bf8cf7..8d102347a0 100644 --- a/.github/workflows/scripts/app_health_check.sh +++ b/.github/workflows/scripts/app_health_check.sh @@ -25,13 +25,13 @@ is_docker_test="${3:-false}" # Validate required port parameter -if [ -z "${port}" ] || ! [[ "${port}" =~ ^[0-9]$ ]] || [ "${port}" -lt 1 ] || [ "${port}" -gt 65535 ]; then +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 +if ! [[ "${timeout}" =~ ^[0-9]+$ ]] || [ "${timeout}" -lt 1 ]; then echo "Error: Invalid timeout value. Must be a positive integer" exit 1 fi