#!/bin/bash # # Health check script for innercontext services # Should be run via cron every 5 minutes: # */5 * * * * /opt/innercontext/scripts/healthcheck.sh >> /opt/innercontext/healthcheck.log 2>&1 # set -euo pipefail BACKEND_URL="http://127.0.0.1:8000/health-check" FRONTEND_URL="http://127.0.0.1:3000/" TIMEOUT=10 TIMESTAMP=$(date '+%Y-%m-%d %H:%M:%S') # Color codes RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' NC='\033[0m' # No Color log() { echo "[$TIMESTAMP] $1" } check_service() { local service_name=$1 local url=$2 local allow_redirect=${3:-false} if systemctl is-active --quiet "$service_name"; then local curl_opts="-s --max-time $TIMEOUT" if [ "$allow_redirect" = false ]; then curl_opts="$curl_opts -f" fi if curl $curl_opts "$url" > /dev/null 2>&1; then log "${GREEN}✓${NC} $service_name is healthy" return 0 else # If allow_redirect is true, we check if it's a 302 if [ "$allow_redirect" = true ]; then local status=$(curl -s -o /dev/null -w "%{http_code}" --max-time "$TIMEOUT" "$url") if [ "$status" = "302" ] || [ "$status" = "303" ] || [ "$status" = "307" ] || [ "$status" = "200" ]; then log "${GREEN}✓${NC} $service_name is healthy (status $status)" return 0 fi fi log "${YELLOW}⚠${NC} $service_name is running but not responding correctly at $url" return 1 fi else log "${RED}✗${NC} $service_name is not running" return 1 fi } # Check all services backend_ok=0 frontend_ok=0 worker_ok=0 # Backend health-check is public and should return 200 check_service "innercontext" "$BACKEND_URL" || backend_ok=1 # Frontend root may redirect to login (302) check_service "innercontext-node" "$FRONTEND_URL" true || frontend_ok=1 # Worker doesn't have HTTP endpoint, just check if it's running if systemctl is-active --quiet "innercontext-pricing-worker"; then log "${GREEN}✓${NC} innercontext-pricing-worker is running" else log "${RED}✗${NC} innercontext-pricing-worker is not running" worker_ok=1 fi # If any service is unhealthy, exit with error code if [ $backend_ok -ne 0 ] || [ $frontend_ok -ne 0 ] || [ $worker_ok -ne 0 ]; then log "${RED}Health check failed${NC}" exit 1 else log "${GREEN}All services healthy${NC}" exit 0 fi