From 1ef7f88b0a6999488473791b0eb4557dc530f98a Mon Sep 17 00:00:00 2001 From: denshooter Date: Sun, 19 Oct 2025 22:02:11 +0200 Subject: [PATCH] feat: add diagnostic and health check scripts - Add comprehensive health check script - Add connection issue diagnostic script - Improve health check reliability - Better error handling and reporting --- scripts/fix-connection.sh | 138 ++++++++++++++++++++++++++++++++++++++ scripts/health-check.sh | 137 +++++++++++++++++++++++++++++++++++++ 2 files changed, 275 insertions(+) create mode 100755 scripts/fix-connection.sh create mode 100755 scripts/health-check.sh diff --git a/scripts/fix-connection.sh b/scripts/fix-connection.sh new file mode 100755 index 0000000..8822269 --- /dev/null +++ b/scripts/fix-connection.sh @@ -0,0 +1,138 @@ +#!/bin/bash + +# Fix Connection Issues Script +# This script diagnoses and fixes common connection issues + +set -e + +# Colors for output +RED='\033[0;31m' +GREEN='\033[0;32m' +YELLOW='\033[1;33m' +BLUE='\033[0;34m' +NC='\033[0m' # No Color + +log() { + echo -e "${BLUE}[$(date +'%Y-%m-%d %H:%M:%S')]${NC} $1" +} + +error() { + echo -e "${RED}[ERROR]${NC} $1" >&2 +} + +success() { + echo -e "${GREEN}[SUCCESS]${NC} $1" +} + +warning() { + echo -e "${YELLOW}[WARNING]${NC} $1" +} + +log "🔧 Diagnosing and fixing connection issues..." + +# Check if containers are running +if ! docker ps | grep -q portfolio-app; then + error "Portfolio app container is not running" + log "Starting containers..." + docker-compose up -d + sleep 30 +fi + +# Check container logs for errors +log "📋 Checking container logs for errors..." +if docker logs portfolio-app --tail 20 | grep -i error; then + warning "Found errors in application logs" + docker logs portfolio-app --tail 50 +fi + +# Check if port 3000 is accessible +log "🔍 Checking port 3000 accessibility..." + +# Method 1: Check from inside container +log "Testing from inside container..." +if docker exec portfolio-app curl -f http://localhost:3000/api/health > /dev/null 2>&1; then + success "Application responds from inside container" +else + error "Application not responding from inside container" + docker logs portfolio-app --tail 20 +fi + +# Method 2: Check port binding +log "Checking port binding..." +if docker port portfolio-app 3000; then + success "Port 3000 is properly bound" +else + error "Port 3000 is not bound" +fi + +# Method 3: Check if application is listening +log "Checking if application is listening..." +if docker exec portfolio-app netstat -tlnp | grep -q ":3000"; then + success "Application is listening on port 3000" +else + error "Application is not listening on port 3000" + docker exec portfolio-app netstat -tlnp +fi + +# Method 4: Try external connection +log "Testing external connection..." +if timeout 5 curl -f http://localhost:3000/api/health > /dev/null 2>&1; then + success "External connection successful" +else + warning "External connection failed - this might be normal if behind reverse proxy" + + # Check if there's a reverse proxy running + if netstat -tlnp | grep -q ":80\|:443"; then + log "Reverse proxy detected - this is expected behavior" + success "Application is running behind reverse proxy" + else + error "No reverse proxy detected and external connection failed" + + # Try to restart the container + log "Attempting to restart portfolio container..." + docker restart portfolio-app + sleep 10 + + if timeout 5 curl -f http://localhost:3000/api/health > /dev/null 2>&1; then + success "External connection successful after restart" + else + error "External connection still failing after restart" + fi + fi +fi + +# Check network configuration +log "🌐 Checking network configuration..." +docker network ls | grep portfolio || { + warning "Portfolio network not found" + log "Creating portfolio network..." + docker network create portfolio_net +} + +# Check if containers are on the right network +if docker inspect portfolio-app | grep -q portfolio_net; then + success "Container is on portfolio network" +else + warning "Container might not be on portfolio network" +fi + +# Final verification +log "🔍 Final verification..." +if docker exec portfolio-app curl -f http://localhost:3000/api/health > /dev/null 2>&1; then + success "✅ Application is healthy and responding" + + # Show final status + log "📊 Final container status:" + docker ps --format "table {{.Names}}\t{{.Status}}\t{{.Ports}}" | grep portfolio + + log "🌐 Application endpoints:" + log " - Health: http://localhost:3000/api/health" + log " - Main: http://localhost:3000/" + log " - Admin: http://localhost:3000/manage" + + success "🎉 Connection issues resolved!" +else + error "❌ Application is still not responding" + log "Please check the logs: docker logs portfolio-app" + exit 1 +fi diff --git a/scripts/health-check.sh b/scripts/health-check.sh new file mode 100755 index 0000000..8573764 --- /dev/null +++ b/scripts/health-check.sh @@ -0,0 +1,137 @@ +#!/bin/bash + +# Comprehensive Health Check Script for Portfolio Application +# This script checks both internal container health and external accessibility + +set -e + +# Colors for output +RED='\033[0;31m' +GREEN='\033[0;32m' +YELLOW='\033[1;33m' +BLUE='\033[0;34m' +NC='\033[0m' # No Color + +# Logging functions +log() { + echo -e "${BLUE}[$(date +'%Y-%m-%d %H:%M:%S')]${NC} $1" +} + +error() { + echo -e "${RED}[ERROR]${NC} $1" >&2 +} + +success() { + echo -e "${GREEN}[SUCCESS]${NC} $1" +} + +warning() { + echo -e "${YELLOW}[WARNING]${NC} $1" +} + +log "🔍 Running comprehensive health checks..." + +# Check if Docker is running +if ! docker info > /dev/null 2>&1; then + error "Docker is not running" + exit 1 +fi + +# Check container status +log "📊 Container status:" +docker ps --format "table {{.Names}}\t{{.Image}}\t{{.Command}}\t{{.Status}}\t{{.Ports}}" | grep portfolio || { + error "No portfolio containers found" + exit 1 +} + +# Check if containers are running +if ! docker ps | grep -q portfolio-app; then + error "Portfolio app container is not running" + exit 1 +fi + +if ! docker ps | grep -q portfolio-postgres; then + error "PostgreSQL container is not running" + exit 1 +fi + +if ! docker ps | grep -q portfolio-redis; then + error "Redis container is not running" + exit 1 +fi + +# Check application health from inside the container +log "🏥 Checking application container..." +HEALTH_RESPONSE=$(docker exec portfolio-app curl -s http://localhost:3000/api/health 2>/dev/null || echo "FAILED") + +if [[ "$HEALTH_RESPONSE" == "FAILED" ]]; then + error "Application health check failed - container not responding" + exit 1 +fi + +# Parse health response +if echo "$HEALTH_RESPONSE" | grep -q '"status":"healthy"'; then + success "Application health check passed!" + echo "$HEALTH_RESPONSE" +else + error "Application health check failed - unhealthy status" + echo "$HEALTH_RESPONSE" + exit 1 +fi + +# Check external accessibility +log "🌐 Checking external accessibility..." + +# Try multiple methods to check if the app is accessible +EXTERNAL_ACCESSIBLE=false + +# Method 1: Check if port 3000 is bound and accessible +if netstat -tlnp 2>/dev/null | grep -q ":3000 "; then + log "Port 3000 is bound" + EXTERNAL_ACCESSIBLE=true +fi + +# Method 2: Try to connect to the application +if curl -f http://localhost:3000/api/health > /dev/null 2>&1; then + log "Application is accessible via localhost:3000" + EXTERNAL_ACCESSIBLE=true +fi + +# Method 3: Check Docker port mapping +if docker port portfolio-app 3000 > /dev/null 2>&1; then + log "Docker port mapping is active" + EXTERNAL_ACCESSIBLE=true +fi + +if [ "$EXTERNAL_ACCESSIBLE" = true ]; then + success "✅ Main page is accessible!" +else + warning "⚠️ Main page accessibility check inconclusive" + log "This might be normal if running behind a reverse proxy" +fi + +# Check database connectivity +log "🗄️ Checking database connectivity..." +if docker exec portfolio-postgres pg_isready -U portfolio_user -d portfolio_db > /dev/null 2>&1; then + success "Database is healthy" +else + error "Database health check failed" + exit 1 +fi + +# Check Redis connectivity +log "🔴 Checking Redis connectivity..." +if docker exec portfolio-redis redis-cli ping > /dev/null 2>&1; then + success "Redis is healthy" +else + error "Redis health check failed" + exit 1 +fi + +# Final status +success "🎉 All health checks passed!" +log "Application is running and healthy" +log "Container status:" +docker ps --format "table {{.Names}}\t{{.Status}}\t{{.Ports}}" | grep portfolio + +exit 0