| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263 |
- #!/bin/sh
- set -e
- echo "Starting Market Data Service API container..."
- # Wait for database to be ready
- if [ -f /usr/local/bin/wait-for-db.sh ]; then
- /usr/local/bin/wait-for-db.sh
- else
- echo "Waiting for database using pg_isready..."
- host="${DB_HOST:-db}"
- port="${DB_PORT:-5432}"
- user="${DB_USER:-postgres}"
- max_attempts=30
- attempt=0
-
- while [ $attempt -lt $max_attempts ]; do
- if PGPASSWORD="${DB_PASSWORD}" pg_isready -h "$host" -p "$port" -U "$user" >/dev/null 2>&1; then
- echo "Database is ready!"
- break
- fi
- attempt=$((attempt + 1))
- echo "Waiting for database... ($attempt/$max_attempts)"
- sleep 2
- done
-
- if [ $attempt -eq $max_attempts ]; then
- echo "ERROR: Database failed to become ready"
- exit 1
- fi
- fi
- # Sync models first to create tables (if they don't exist)
- echo "Syncing database models..."
- node -e "
- const { sequelize } = require('./src/models');
- sequelize.sync({ alter: true, force: false })
- .then(() => {
- console.log('Database models synced successfully.');
- process.exit(0);
- })
- .catch((error) => {
- console.error('Error syncing models:', error);
- process.exit(1);
- });
- " || {
- echo "WARNING: Model sync failed, continuing with migrations..."
- }
- # Run database migrations
- echo "Running database migrations..."
- npx sequelize-cli db:migrate || {
- echo "ERROR: Database migration failed"
- exit 1
- }
- echo "Migrations completed successfully"
- # Start the application
- echo "Starting Node.js application..."
- exec "$@"
|