-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathentrypoint.sh
More file actions
executable file
·213 lines (191 loc) · 6.94 KB
/
Copy pathentrypoint.sh
File metadata and controls
executable file
·213 lines (191 loc) · 6.94 KB
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#!/bin/bash
# Set up error handling
set -e
echo "=== Starting ${SERVICE_NAME:-unknown} service ==="
# Ensure environment variables are loaded
if [ -f /app/.env ]; then
echo "Re-loading environment variables from .env"
set -a
source /app/.env
set +a
echo "Current DB_NAME: $DB_NAME"
echo "Current DB_HOST: $DB_HOST"
echo "Current DB_USER: $DB_USER"
fi
# Function to wait for service
wait_for_service() {
local host=$1
local port=$2
local service_name=$3
local max_attempts=30
local attempt=1
echo "Waiting for $service_name to be ready..."
while ! nc -z $host $port; do
if [ $attempt -eq $max_attempts ]; then
echo "ERROR: $service_name not ready after $max_attempts attempts"
exit 1
fi
echo "Attempt $attempt/$max_attempts: $service_name not ready, waiting..."
sleep 2
attempt=$((attempt + 1))
done
echo "$service_name is ready!"
}
# Wait for Redis first (all services need it)
wait_for_service ${REDIS_HOST:-redis} ${REDIS_PORT:-6379} "Redis"
# Only download model for web service or if explicitly needed
if [ "$SERVICE_NAME" = "web" ] || [ "$INITIALIZE_XRAY_MODEL" = "True" ]; then
echo "Checking for DenseNet121 model weights..."
if [ ! -f "/app/data/models/densenet_xray.pth" ] && [ ! -f "/app/data/models/densenet_xray_fixed.pth" ]; then
echo "Downloading DenseNet121 model weights..."
for i in {1..3}; do
echo "Attempt $i to download model weights..."
if python -m api.utils.download_model; then
echo "Model weights downloaded successfully!"
break
else
echo "Download failed, retrying..."
sleep 10
fi
done
if [ ! -f "/app/data/models/densenet_xray.pth" ]; then
echo "Warning: Failed to download model weights after multiple attempts."
touch /app/data/models/densenet_xray.pth.failed
fi
else
echo "Model weights already exist"
fi
# Fix model if needed
if [ -f "/app/data/models/densenet_xray.pth" ] && [ ! -f "/app/data/models/densenet_xray_fixed.pth" ]; then
echo "Fixing model architecture..."
python -c "
try:
from api.utils.model_fix import fix_densenet_model
fix_densenet_model()
print('Model fixed successfully')
except Exception as e:
print(f'Error fixing model: {e}')
" || echo "Model fix failed, continuing..."
fi
fi
# Apply database migrations only for web service
if [ "$SHOULD_MIGRATE" = "true" ]; then
echo "Applying database migrations..."
echo "Using database: $DB_NAME on host: $DB_HOST"
# Test database connection first
python -c "
import os
import django
from django.conf import settings
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'praxia_backend.settings')
django.setup()
from django.db import connections
try:
db = connections['default']
cursor = db.cursor()
cursor.execute('SELECT 1')
print(f'Database connection successful to: {settings.DATABASES[\"default\"][\"NAME\"]}')
print(f'Host: {settings.DATABASES[\"default\"][\"HOST\"]}')
print(f'User: {settings.DATABASES[\"default\"][\"USER\"]}')
except Exception as e:
print(f'Database connection failed: {e}')
print(f'Trying to connect to: {settings.DATABASES[\"default\"][\"NAME\"]}')
print(f'Host: {settings.DATABASES[\"default\"][\"HOST\"]}')
exit(1)
"
if [ $? -eq 0 ]; then
python manage.py migrate --noinput
# Create superuser only once
echo "Creating superuser if needed..."
if [ "$DJANGO_SUPERUSER_USERNAME" ] && [ "$DJANGO_SUPERUSER_EMAIL" ] && [ "$DJANGO_SUPERUSER_PASSWORD" ]; then
python manage.py shell -c "
from django.contrib.auth import get_user_model
User = get_user_model()
try:
if not User.objects.filter(username='$DJANGO_SUPERUSER_USERNAME').exists():
User.objects.create_superuser('$DJANGO_SUPERUSER_USERNAME', '$DJANGO_SUPERUSER_EMAIL', '$DJANGO_SUPERUSER_PASSWORD')
print('Superuser created successfully')
else:
print('Superuser already exists')
except Exception as e:
print(f'Error creating superuser: {e}')
" || echo "Superuser creation failed, continuing..."
fi
# Collect static files
echo "Collecting static files..."
python manage.py collectstatic --noinput
# Run health check
echo "Running initial health check..."
python manage.py shell -c "
try:
from api.AI.ai_healthcheck import startup_health_check
result = startup_health_check()
print('Health check completed successfully')
except Exception as e:
print(f'Health check failed: {e}')
" || echo "Health check failed, continuing..."
else
echo "Database connection failed, exiting..."
exit 1
fi
else
echo "Skipping database migrations for this service..."
fi
# For non-web services, wait for web service to be ready
if [ "$SERVICE_NAME" != "web" ]; then
echo "Waiting for web service to be ready..."
max_attempts=60
attempt=1
while ! curl -f http://web:8000/api/health/ >/dev/null 2>&1; do
if [ $attempt -eq $max_attempts ]; then
echo "WARNING: Web service not ready after $max_attempts attempts, continuing anyway..."
break
fi
echo "Attempt $attempt/$max_attempts: Web service not ready, waiting..."
sleep 5
attempt=$((attempt + 1))
done
echo "Web service is ready (or timeout reached)!"
fi
# For Celery services, ensure they can connect to the broker
if [[ "$SERVICE_NAME" == celery* ]]; then
echo "Testing Celery broker connection..."
python -c "
import redis
import sys
try:
r = redis.Redis(host='${REDIS_HOST:-redis}', port=${REDIS_PORT:-6379}, db=1)
r.ping()
print('Celery broker connection successful')
except Exception as e:
print(f'Celery broker connection failed: {e}')
sys.exit(1)
" || exit 1
echo "Clearing Celery broker database..."
python -c "
import redis
try:
r = redis.Redis(host='${REDIS_HOST:-redis}', port=${REDIS_PORT:-6379}, db=1)
r.flushdb()
print('Cleared Celery broker database')
except Exception as e:
print(f'Could not clear Celery database: {e}')
" || echo "Failed to clear Celery database, continuing..."
fi
# Create necessary directories
mkdir -p /app/media/profile_pics /app/media/xrays /app/data/models /app/prometheus
# Clear Celery queues for celery services
if [[ "$SERVICE_NAME" == celery* ]]; then
echo "Clearing Celery broker database..."
python -c "
import redis
try:
r = redis.Redis(host='${REDIS_HOST:-redis}', port=${REDIS_PORT:-6379}, db=1)
r.flushdb()
print('Cleared Celery broker database')
except Exception as e:
print(f'Could not clear Celery database: {e}')
" || echo "Failed to clear Celery database, continuing..."
fi
echo "=== Starting Daphne server for ${SERVICE_NAME:-unknown} ==="
exec daphne -b 0.0.0.0 -p 8000 praxia_backend.asgi:application