Files
gochat/backend/scripts/health_check.sh
T
Rogeeandrogee 798ea43c2f HH-442: isolate runtime processes and harden shutdown (#90)
* HH-442: isolate runtime processes and harden shutdown

* HH-442: harden worker shutdown races

* HH-442: gate dependency shutdown on active handlers

---------

Co-authored-by: Rogee <rogee@ipao.vip>
2026-08-22 02:38:15 +08:00

104 lines
3.3 KiB
Bash
Executable File

#!/bin/bash
# Exit codes: 0 healthy, 1 degraded, 2 unhealthy/invalid invocation.
set -uo pipefail
GOCHAT_HOST="${GOCHAT_HOST:-localhost}"
GOCHAT_PORT="${GOCHAT_PORT:-3000}"
GOCHAT_METRICS_PORT="${GOCHAT_METRICS_PORT:-${GOCHAT_PORT}}"
TIMEOUT="${TIMEOUT:-5}"
FULL_CHECK=false
JSON_OUTPUT=false
RESULTS=()
CRITICAL_FAIL=0
DEGRADED_FAIL=0
for arg in "$@"; do
[[ "${arg}" == "--json" ]] && JSON_OUTPUT=true
done
usage_error() {
if [[ "${JSON_OUTPUT}" == "true" ]]; then
printf '{"error":"%s","checks":[],"summary":{"critical_failures":1,"degraded_failures":0}}\n' "$1"
else
printf 'health_check: %s\n' "$1" >&2
fi
exit 2
}
while [[ $# -gt 0 ]]; do
case "$1" in
--full) FULL_CHECK=true; shift ;;
--json) JSON_OUTPUT=true; shift ;;
--timeout)
[[ $# -ge 2 && "$2" =~ ^[1-9][0-9]*$ ]] || usage_error "--timeout requires a positive integer"
TIMEOUT="$2"
shift 2
;;
--help)
if [[ "${JSON_OUTPUT}" == "true" ]]; then
printf '{"usage":"health_check.sh [--full] [--json] [--timeout SECONDS]"}\n'
else
printf 'Usage: %s [--full] [--json] [--timeout SECONDS]\n' "$0"
fi
exit 0
;;
*) usage_error "unknown option" ;;
esac
done
BASE_URL="http://${GOCHAT_HOST}:${GOCHAT_PORT}"
METRICS_URL="http://${GOCHAT_HOST}:${GOCHAT_METRICS_PORT}"
check_result() {
local name="$1" status="$2" detail="$3"
RESULTS+=("${name}|${status}|${detail}")
[[ "${status}" == "CRITICAL_FAIL" ]] && CRITICAL_FAIL=$((CRITICAL_FAIL + 1))
[[ "${status}" == "DEGRADED" ]] && DEGRADED_FAIL=$((DEGRADED_FAIL + 1))
}
check_url() {
local name="$1" severity="$2" url="$3" ok_detail="$4" fail_detail="$5"
if curl -fsS --max-time "${TIMEOUT}" -o /dev/null "${url}"; then
check_result "${name}" "OK" "${ok_detail}"
else
check_result "${name}" "${severity}" "${fail_detail}"
fi
}
check_url "liveness" "CRITICAL_FAIL" "${BASE_URL}/live" "process is alive" "liveness endpoint unreachable"
check_url "readiness" "CRITICAL_FAIL" "${BASE_URL}/ready" "dependencies are ready" "readiness endpoint failed"
if [[ "${FULL_CHECK}" == "true" ]]; then
check_url "database" "CRITICAL_FAIL" "${BASE_URL}/health?check=database" "database is healthy" "database check failed"
check_url "redis" "CRITICAL_FAIL" "${BASE_URL}/health?check=redis" "redis is healthy" "redis check failed"
else
check_result "database" "SKIPPED" "not in full mode"
check_result "redis" "SKIPPED" "not in full mode"
fi
check_url "metrics" "DEGRADED" "${METRICS_URL}/metrics" "metrics endpoint is responding" "metrics endpoint unreachable"
if [[ "${JSON_OUTPUT}" == "true" ]]; then
printf '{"checks":['
for i in "${!RESULTS[@]}"; do
IFS='|' read -r name status detail <<< "${RESULTS[$i]}"
[[ $i -gt 0 ]] && printf ','
printf '{"name":"%s","status":"%s","detail":"%s"}' "${name}" "${status}" "${detail}"
done
printf '],"summary":{"critical_failures":%d,"degraded_failures":%d}}\n' "${CRITICAL_FAIL}" "${DEGRADED_FAIL}"
else
printf '=== GoChat Health Check Report ===\n'
for result in "${RESULTS[@]}"; do
IFS='|' read -r name status detail <<< "${result}"
printf ' %s: %s — %s\n' "${name}" "${status}" "${detail}"
done
printf 'Critical failures: %d | Degraded: %d\n' "${CRITICAL_FAIL}" "${DEGRADED_FAIL}"
fi
if [[ ${CRITICAL_FAIL} -gt 0 ]]; then
exit 2
elif [[ ${DEGRADED_FAIL} -gt 0 ]]; then
exit 1
fi