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