#!/bin/bash
set -e
# Wait for PostgreSQL if DB_HOST is set
if [ -n "$DB_HOST" ] && [ "$DB_HOST" != "localhost" ]; then
echo "Waiting for PostgreSQL to be ready..."
if command -v pg_isready > /dev/null 2>&1; then
while ! pg_isready -h "$DB_HOST" -p "$DB_PORT" -U "$DB_USER" > /dev/null 2>&1; do
sleep 1
done
else
# Fallback: simple TCP connection check
while ! nc -z "$DB_HOST" "$DB_PORT" 2>/dev/null; do
sleep 1
done
fi
echo "PostgreSQL is ready!"
fi
# Initialize database if this is the first run (only for services that need it)
if [ "$INIT_DB" = "true" ] || [ "$1" = "python" ] && [[ "$2" == *"job_server"* ]]; then
echo "Initializing database schema..."
python -c "from database import init_db; init_db()" || echo "Database may already be initialized"
fi
# Execute the command passed to the container
exec "$@"