|
| 1 | +#!/bin/sh |
| 2 | +# wait-for-db.sh - Wait for database to be ready before starting application services |
| 3 | +set -e |
| 4 | + |
| 5 | +DB_TYPE="${DB_TYPE:-postgres}" |
| 6 | +MAX_RETRIES=60 |
| 7 | +RETRY_DELAY=1 |
| 8 | + |
| 9 | +echo "Waiting for $DB_TYPE database to be ready..." |
| 10 | + |
| 11 | +case "$DB_TYPE" in |
| 12 | + postgres|postgresql) |
| 13 | + DB_HOST="${DB_HOST:-localhost}" |
| 14 | + DB_PORT="${DB_PORT:-5432}" |
| 15 | + DB_NAME="${DB_NAME:-peekaping}" |
| 16 | + DB_USER="${DB_USER:-postgres}" |
| 17 | + |
| 18 | + for i in $(seq 1 $MAX_RETRIES); do |
| 19 | + if PGPASSWORD="$DB_PASS" psql -h "$DB_HOST" -p "$DB_PORT" -U "$DB_USER" -d "$DB_NAME" -c "SELECT 1" >/dev/null 2>&1; then |
| 20 | + echo "PostgreSQL is ready!" |
| 21 | + exit 0 |
| 22 | + fi |
| 23 | + echo "Attempt $i/$MAX_RETRIES: PostgreSQL is not ready yet, waiting ${RETRY_DELAY}s..." |
| 24 | + sleep $RETRY_DELAY |
| 25 | + done |
| 26 | + echo "ERROR: PostgreSQL failed to become ready after $MAX_RETRIES attempts" |
| 27 | + exit 1 |
| 28 | + ;; |
| 29 | + |
| 30 | + mysql) |
| 31 | + DB_HOST="${DB_HOST:-localhost}" |
| 32 | + DB_PORT="${DB_PORT:-3306}" |
| 33 | + DB_NAME="${DB_NAME:-peekaping}" |
| 34 | + DB_USER="${DB_USER:-root}" |
| 35 | + |
| 36 | + for i in $(seq 1 $MAX_RETRIES); do |
| 37 | + if mysql -h "$DB_HOST" -P "$DB_PORT" -u "$DB_USER" -p"$DB_PASS" -e "SELECT 1" "$DB_NAME" >/dev/null 2>&1; then |
| 38 | + echo "MySQL is ready!" |
| 39 | + exit 0 |
| 40 | + fi |
| 41 | + echo "Attempt $i/$MAX_RETRIES: MySQL is not ready yet, waiting ${RETRY_DELAY}s..." |
| 42 | + sleep $RETRY_DELAY |
| 43 | + done |
| 44 | + echo "ERROR: MySQL failed to become ready after $MAX_RETRIES attempts" |
| 45 | + exit 1 |
| 46 | + ;; |
| 47 | + |
| 48 | + sqlite) |
| 49 | + DB_NAME="${DB_NAME:-/app/data/peekaping.db}" |
| 50 | + |
| 51 | + for i in $(seq 1 $MAX_RETRIES); do |
| 52 | + if [ -f "$DB_NAME" ] && sqlite3 "$DB_NAME" "SELECT 1" >/dev/null 2>&1; then |
| 53 | + echo "SQLite is ready!" |
| 54 | + exit 0 |
| 55 | + fi |
| 56 | + echo "Attempt $i/$MAX_RETRIES: SQLite is not ready yet, waiting ${RETRY_DELAY}s..." |
| 57 | + sleep $RETRY_DELAY |
| 58 | + done |
| 59 | + echo "ERROR: SQLite failed to become ready after $MAX_RETRIES attempts" |
| 60 | + exit 1 |
| 61 | + ;; |
| 62 | + |
| 63 | + mongo|mongodb) |
| 64 | + DB_HOST="${DB_HOST:-localhost}" |
| 65 | + DB_PORT="${DB_PORT:-27017}" |
| 66 | + |
| 67 | + for i in $(seq 1 $MAX_RETRIES); do |
| 68 | + if mongosh --host "$DB_HOST" --port "$DB_PORT" --eval "db.adminCommand('ping')" >/dev/null 2>&1; then |
| 69 | + echo "MongoDB is ready!" |
| 70 | + exit 0 |
| 71 | + fi |
| 72 | + echo "Attempt $i/$MAX_RETRIES: MongoDB is not ready yet, waiting ${RETRY_DELAY}s..." |
| 73 | + sleep $RETRY_DELAY |
| 74 | + done |
| 75 | + echo "ERROR: MongoDB failed to become ready after $MAX_RETRIES attempts" |
| 76 | + exit 1 |
| 77 | + ;; |
| 78 | + |
| 79 | + *) |
| 80 | + echo "ERROR: Unsupported database type: $DB_TYPE" |
| 81 | + exit 1 |
| 82 | + ;; |
| 83 | +esac |
| 84 | + |
0 commit comments