File size: 4,533 Bytes
b696561 ad8ebb5 b696561 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 |
#!/bin/bash
# Health check script for Docker container with dynamic path support
set -e
# Color codes for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color
log() {
echo -e "${GREEN}[HEALTH]${NC} $1"
}
error() {
echo -e "${RED}[HEALTH ERROR]${NC} $1" >&2
}
warning() {
echo -e "${YELLOW}[HEALTH WARNING]${NC} $1"
}
# Function to check service health
check_service() {
local service_name=$1
local url=$2
local timeout=${3:-10}
if curl -s -f --max-time "$timeout" "$url" > /dev/null 2>&1; then
log "$service_name is responding"
return 0
else
error "$service_name health check failed"
return 1
fi
}
# Function to check file using Python path manager
check_files_with_python() {
python3 -c "
import sys
import os
sys.path.append('/app')
try:
from path_config import path_manager
# Check critical files using the path manager
critical_files = [
(path_manager.get_combined_dataset_path(), 'Combined Dataset'),
(path_manager.get_model_file_path(), 'Model File'),
(path_manager.get_vectorizer_path(), 'Vectorizer File'),
(path_manager.get_metadata_path(), 'Metadata File')
]
missing_files = []
for file_path, description in critical_files:
if file_path.exists():
print(f'β
{description}: {file_path}')
else:
print(f'β {description}: {file_path}')
missing_files.append(description)
if missing_files:
print(f'Missing files: {missing_files}')
sys.exit(1)
else:
print('β
All critical files present')
except ImportError as e:
print(f'β Failed to import path_config: {e}')
sys.exit(1)
except Exception as e:
print(f'β Health check failed: {e}')
sys.exit(1)
"
return $?
}
# Function to check process
check_process() {
local process_name=$1
if pgrep -f "$process_name" > /dev/null 2>&1; then
log "$process_name process is running"
return 0
else
warning "$process_name process not found"
return 1
fi
}
# Main health check
main() {
log "Starting comprehensive health check..."
local exit_code=0
# Check FastAPI service
if ! check_service "FastAPI" "http://127.0.0.1:8000/health" 10; then
exit_code=1
fi
# Check Streamlit service
if ! check_service "Streamlit" "http://127.0.0.1:7860/_stcore/health" 10; then
# Streamlit health endpoint might not always be available, try alternative
if ! curl -s -f --max-time 10 "http://127.0.0.1:7860" > /dev/null 2>&1; then
error "Streamlit health check failed"
exit_code=1
else
log "Streamlit is responding (alternative check)"
fi
fi
# Check required files using Python path manager
log "Checking required files..."
if ! check_files_with_python; then
exit_code=1
fi
# Check critical processes (optional in some environments)
log "Checking processes..."
# Always check if Python processes are running
if ! pgrep -f "streamlit" > /dev/null 2>&1; then
warning "Streamlit process not found"
# Don't fail on this as the service might still be responding
fi
if ! pgrep -f "uvicorn" > /dev/null 2>&1; then
warning "FastAPI/Uvicorn process not found"
# Don't fail on this as the service might still be responding
fi
# Check if scheduler is running (optional for some environments)
if ! check_process "schedule_tasks.py"; then
warning "Scheduler process not running (may be normal in some environments)"
# Don't fail the health check for this
fi
# Check Python environment
log "Checking Python environment..."
if ! python3 -c "
import sys
sys.path.append('/app')
# Test critical imports
try:
import pandas
import sklearn
import streamlit
import fastapi
from path_config import path_manager
print(f'β
Python environment OK (Environment: {path_manager.environment})')
except ImportError as e:
print(f'β Python import failed: {e}')
sys.exit(1)
"; then
error "Python environment check failed"
exit_code=1
fi
# Final status
if [ $exit_code -eq 0 ]; then
log "All health checks passed β
"
else
error "Health check failed β"
fi
return $exit_code
}
# Execute main function
main "$@" |