Ahmedik95316 commited on
Commit
b696561
Β·
1 Parent(s): e2d418f

Update health_check.sh

Browse files
Files changed (1) hide show
  1. health_check.sh +172 -35
health_check.sh CHANGED
@@ -1,37 +1,174 @@
1
- # Health check script for Docker container
 
 
2
  set -e
3
 
4
- # Check if FastAPI is responding
5
- if ! curl -s -f "http://127.0.0.1:8000/docs" > /dev/null; then
6
- echo "FastAPI health check failed"
7
- exit 1
8
- fi
9
-
10
- # Check if Streamlit is responding
11
- if ! curl -s -f "http://127.0.0.1:7860/_stcore/health" > /dev/null; then
12
- echo "Streamlit health check failed"
13
- exit 1
14
- fi
15
-
16
- # Check if required files exist
17
- required_files=(
18
- "/tmp/model.pkl"
19
- "/tmp/vectorizer.pkl"
20
- "/tmp/data/combined_dataset.csv"
21
- )
22
-
23
- for file in "${required_files[@]}"; do
24
- if [ ! -f "$file" ]; then
25
- echo "Required file missing: $file"
26
- exit 1
27
- fi
28
- done
29
-
30
- # Check if processes are running
31
- if ! pgrep -f "schedule_tasks.py" > /dev/null; then
32
- echo "Scheduler process not running"
33
- exit 1
34
- fi
35
-
36
- echo "All health checks passed"
37
- exit 0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 "$@"