gcp-hockey-results/docker/start.sh
2025-09-29 20:06:07 +08:00

69 lines
2.1 KiB
Bash

#!/bin/bash
# Docker startup script for Hockey Results Application
set -e
echo "🏒 Starting Hockey Results Application..."
# Function to wait for database
wait_for_database() {
local db_type=$1
local max_attempts=30
local attempt=1
echo "⏳ Waiting for $db_type database to be ready..."
case $db_type in
"postgresql")
while [ $attempt -le $max_attempts ]; do
if python -c "import psycopg2; psycopg2.connect(host='$POSTGRES_HOST', port='$POSTGRES_PORT', database='$POSTGRES_DATABASE', user='$POSTGRES_USER', password='$POSTGRES_PASSWORD')" 2>/dev/null; then
echo "✅ PostgreSQL database is ready!"
return 0
fi
echo "Attempt $attempt/$max_attempts: Database not ready, waiting..."
sleep 2
attempt=$((attempt + 1))
done
;;
"mysql"|"mariadb")
while [ $attempt -le $max_attempts ]; do
if python -c "import pymysql; pymysql.connect(host='$MYSQL_HOST', port='$MYSQL_PORT', database='$MYSQL_DATABASE', user='$MYSQL_USER', password='$MYSQL_PASSWORD')" 2>/dev/null; then
echo "✅ MySQL/MariaDB database is ready!"
return 0
fi
echo "Attempt $attempt/$max_attempts: Database not ready, waiting..."
sleep 2
attempt=$((attempt + 1))
done
;;
esac
echo "❌ Database connection timeout after $max_attempts attempts"
exit 1
}
# Initialize database if needed
init_database() {
echo "🔧 Initializing database..."
python -c "
from motm_app.database import init_database
try:
init_database()
print('✅ Database initialized successfully')
except Exception as e:
print(f'⚠️ Database initialization warning: {e}')
"
}
# Wait for database if not using SQLite
if [ "$DATABASE_TYPE" != "sqlite" ]; then
wait_for_database "$DATABASE_TYPE"
fi
# Initialize database
init_database
# Start the application
echo "🚀 Starting Flask application..."
exec python motm_app/main.py