module.exports = {
  apps: [{
    name: 'ems-backend',
    script: './index.js',
    instances: 1, // Only one instance to prevent port conflicts
    exec_mode: 'fork',
    watch: false,
    max_memory_restart: '500M',
    error_file: './logs/pm2-error.log',
    out_file: './logs/pm2-out.log',
    log_date_format: 'YYYY-MM-DD HH:mm:ss Z',
    merge_logs: true,
    autorestart: true,
    max_restarts: 5, // Limit restarts to prevent infinite loops
    min_uptime: '30s', // Must run for 30 seconds to be considered stable
    restart_delay: 10000, // Wait 10 seconds before restarting (increased backoff)
    exp_backoff_restart_delay: 100, // Exponential backoff starting at 100ms
    kill_timeout: 5000,
    wait_ready: false,
    listen_timeout: 10000,
    shutdown_with_message: true,
    env: {
      NODE_ENV: 'production',
      PORT: 4000
    }
  }]
};

