forked from Dushyant-rahangdale/OpsKnight
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdocker-entrypoint.sh
More file actions
74 lines (60 loc) · 1.99 KB
/
Copy pathdocker-entrypoint.sh
File metadata and controls
74 lines (60 loc) · 1.99 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
#!/bin/sh
set -e
echo "🚀 OpsKnight Startup"
echo "======================"
# ---------------------------
# Fault-tolerant Migration Logic
# ---------------------------
echo "🔄 Running database migrations..."
# Function to run migrations
run_migrations() {
if [ -f "node_modules/prisma/build/index.js" ]; then
node node_modules/prisma/build/index.js migrate deploy
return $?
else
echo "⚠️ Prisma not found, skipping migrations"
return 0
fi
}
# Function to run auto-recovery
run_auto_recovery() {
echo "🔧 Attempting auto-recovery..."
# Check for compiled JS version (Production)
if [ -f "scripts/dist/auto-recover-migrations.js" ]; then
node scripts/dist/auto-recover-migrations.js
# Fallback to TS version (Development w/ ts-node)
elif [ -f "scripts/auto-recover-migrations.ts" ] && [ -f "node_modules/.bin/ts-node" ]; then
node --loader ts-node/esm scripts/auto-recover-migrations.ts
else
echo "ℹ️ No executable recovery script found. Relying on app startup recovery."
fi
}
MAX_RETRIES=3
RETRY_COUNT=0
MIGRATION_SUCCESS=0
# Retry loop with backoff
while [ $RETRY_COUNT -lt $MAX_RETRIES ]; do
RETRY_COUNT=$((RETRY_COUNT + 1))
if run_migrations; then
echo "✅ Migrations completed successfully"
MIGRATION_SUCCESS=1
break
else
EXIT_CODE=$?
echo "⚠️ Migration attempt $RETRY_COUNT failed (exit code: $EXIT_CODE)"
if [ $RETRY_COUNT -lt $MAX_RETRIES ]; then
# Try recovery before next attempt
run_auto_recovery
echo "⏳ Waiting 5s before retrying..."
sleep 5
fi
fi
done
if [ $MIGRATION_SUCCESS -eq 0 ]; then
echo "❌ All migration attempts failed."
echo "⚡ Starting application anyway (Instrumentation will attempt final recovery)..."
else
echo "✅ Database is ready."
fi
echo "🚀 Starting application..."
exec node server.js