diff --git a/app.py b/app.py index a99297e..f2ce152 100644 --- a/app.py +++ b/app.py @@ -76,9 +76,21 @@ def create_app(): @app.route('/health') def health_check(): try: - # Check database connection + # Check database connection with a timeout db.session.execute(text('SELECT 1')) db.session.commit() + + # Check if we can actually query a table + try: + User.query.first() + except Exception as e: + app.logger.error(f"Database query failed: {str(e)}") + return jsonify({ + 'status': 'unhealthy', + 'error': f"Database query failed: {str(e)}", + 'timestamp': datetime.utcnow().isoformat() + }), 500 + return jsonify({ 'status': 'healthy', 'database': 'connected', diff --git a/entrypoint.sh b/entrypoint.sh index 3d97414..52871b6 100644 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -38,67 +38,56 @@ until PGPASSWORD=$POSTGRES_PASSWORD psql -h db -U $POSTGRES_USER -d $POSTGRES_DB done echo "PostgreSQL is up - executing command" -# Run migrations -echo "Running database migrations..." -flask db upgrade - -# Create events table -echo "Creating events table..." -python3 -c " -from migrations.add_events_table import upgrade -from app import create_app -app = create_app() -with app.app_context(): - try: - upgrade() - print('Events table created successfully') - except Exception as e: - print(f'Error creating events table: {e}') -" - -# Create notifs table -echo "Creating notifs table..." -python3 -c " -from migrations.add_notifs_table import upgrade -from app import create_app -app = create_app() -with app.app_context(): - try: - upgrade() - print('Notifs table created successfully') - except Exception as e: - print(f'Error creating notifs table: {e}') -" - -# Create default site settings if they don't exist -echo "Creating default site settings..." +# Run all initialization in a single Python script to avoid multiple Flask instances +echo "Running initialization..." python3 -c " from app import create_app from models import SiteSettings, db +from migrations.add_events_table import upgrade as upgrade_events +from migrations.add_notifs_table import upgrade as upgrade_notifs +from init_admin import init_admin +from utils.email_templates import create_default_templates + app = create_app() with app.app_context(): + # Run migrations + print('Running database migrations...') + from flask_migrate import upgrade + upgrade() + + # Create events table + print('Creating events table...') + try: + upgrade_events() + print('Events table created successfully') + except Exception as e: + print(f'Error creating events table: {e}') + + # Create notifs table + print('Creating notifs table...') + try: + upgrade_notifs() + print('Notifs table created successfully') + except Exception as e: + print(f'Error creating notifs table: {e}') + + # Create default site settings + print('Creating default site settings...') try: settings = SiteSettings.get_settings() print('Default site settings created successfully') except Exception as e: print(f'Error creating site settings: {e}') + + # Initialize admin user + print('Initializing admin user...') + init_admin() + + # Create default templates + print('Creating default templates...') + create_default_templates() " -# Initialize admin user -echo "Initializing admin user..." -python3 -c " -from init_admin import init_admin -init_admin() -" - -# Create admin user -echo "Creating admin user..." -flask create-admin - -# Create default templates -echo "Creating default templates..." -flask create-default-templates - # Start the application echo "Starting application..." exec gunicorn --bind 0.0.0.0:5000 app:app \ No newline at end of file