|
1 | 1 | #!/bin/bash |
2 | 2 | set -e |
3 | 3 |
|
4 | | -echo " ProStaff API - Starting..." |
5 | | - |
6 | | -# Skip DATABASE_URL encoding - handled by Rails initializer |
7 | | -# The initializer (config/initializers/database_url_override.rb) will detect |
8 | | -# special characters and parse manually in database.yml |
9 | | -echo " Database configuration will be handled by Rails initializer" |
| 4 | +echo "========================================" >&2 |
| 5 | +echo "ProStaff API - Docker Entrypoint" >&2 |
| 6 | +echo "========================================" >&2 |
10 | 7 |
|
11 | 8 | # Remove any pre-existing server PID file |
| 9 | +echo "[1/4] Removing stale PID files..." >&2 |
12 | 10 | rm -f /app/tmp/pids/server.pid |
13 | 11 |
|
14 | 12 | # Wait for database to be ready |
15 | | -echo " Waiting for database..." |
16 | | -# Use SUPABASE_DB_URL or DATABASE_URL, extract host for pg_isready |
| 13 | +echo "[2/4] Checking database connection..." >&2 |
17 | 14 | DB_URL="${SUPABASE_DB_URL:-$DATABASE_URL}" |
18 | 15 | if [ -n "$DB_URL" ]; then |
19 | 16 | # Extract host and port from URL (format: postgresql://user:pass@host:port/db) |
20 | 17 | # Parse from right to left to handle @ in password |
21 | 18 | DB_HOST=$(echo "$DB_URL" | sed -E 's|.*@([^@/]+):[0-9]+/.*|\1|') |
22 | 19 | DB_PORT=$(echo "$DB_URL" | sed -E 's|.*@[^@/]+:([0-9]+)/.*|\1|') |
23 | 20 |
|
24 | | - echo " Checking connection to ${DB_HOST}:${DB_PORT}..." |
| 21 | + echo " → Host: ${DB_HOST}:${DB_PORT}" >&2 |
| 22 | + |
| 23 | + MAX_RETRIES=30 |
| 24 | + RETRY_COUNT=0 |
25 | 25 | until pg_isready -h "$DB_HOST" -p "$DB_PORT" > /dev/null 2>&1; do |
26 | | - echo " Database is unavailable - sleeping" |
| 26 | + RETRY_COUNT=$((RETRY_COUNT + 1)) |
| 27 | + if [ $RETRY_COUNT -ge $MAX_RETRIES ]; then |
| 28 | + echo " ✗ Database connection timeout after ${MAX_RETRIES} attempts" >&2 |
| 29 | + exit 1 |
| 30 | + fi |
| 31 | + echo " ⏳ Waiting for database (attempt ${RETRY_COUNT}/${MAX_RETRIES})..." >&2 |
27 | 32 | sleep 2 |
28 | 33 | done |
| 34 | + echo " ✓ Database is ready" >&2 |
29 | 35 | else |
30 | | - until PGPASSWORD=$POSTGRES_PASSWORD psql -h "${POSTGRES_HOST:-postgres}" -U "$POSTGRES_USER" -d "$POSTGRES_DB" -c '\q' 2>/dev/null; do |
31 | | - echo " Database is unavailable - sleeping" |
32 | | - sleep 2 |
33 | | - done |
| 36 | + echo " ⚠ No DATABASE_URL configured, skipping database check" >&2 |
34 | 37 | fi |
35 | | -echo " Database is ready" |
36 | 38 |
|
37 | 39 | # Run database migrations |
38 | | -echo " Running database migrations..." |
39 | | -bundle exec rails db:migrate 2>/dev/null || { |
40 | | - echo " Migration failed, attempting to create database..." |
41 | | - bundle exec rails db:create |
42 | | - bundle exec rails db:migrate |
43 | | -} |
44 | | - |
45 | | -# Preload app for better performance |
46 | | -if [ "$RAILS_ENV" = "production" ]; then |
47 | | - echo " Preloading application..." |
48 | | - bundle exec rails runner 'Rails.application.eager_load!' |
| 40 | +echo "[3/4] Running database migrations..." >&2 |
| 41 | +if bundle exec rails db:migrate 2>&1 | tee /tmp/migration.log >&2; then |
| 42 | + echo " ✓ Migrations completed" >&2 |
| 43 | +else |
| 44 | + echo " ⚠ Migration failed, check output above" >&2 |
| 45 | + echo " → Attempting to create database..." >&2 |
| 46 | + bundle exec rails db:create 2>&1 | tee -a /tmp/migration.log >&2 |
| 47 | + bundle exec rails db:migrate 2>&1 | tee -a /tmp/migration.log >&2 |
49 | 48 | fi |
50 | 49 |
|
51 | | -echo " Application ready!" |
52 | | - |
53 | | -echo " Starting application server..." |
| 50 | +# Skip preload in production - Puma will handle it |
| 51 | +echo "[4/4] Starting application server..." >&2 |
| 52 | +echo "========================================" >&2 |
54 | 53 |
|
55 | 54 | # Execute the main command |
56 | 55 | exec "$@" |
0 commit comments