File: /var/www/indoadvisory_new/web/webapp/ecosystem.config.js
// PM2 Production Configuration for VPS Deployment
// Indo Advisory - Indonesian Private Equity Firm
module.exports = {
  apps: [{
    // Application Configuration
    name: 'indo-advisory',
    script: 'server.js',
    
    // Environment Variables
    env: {
      NODE_ENV: 'development',
      PORT: 3000
    },
    env_production: {
      NODE_ENV: 'production',
      PORT: 3000
    },
    
    // PM2 Settings
    instances: 'max', // Use all available CPU cores
    exec_mode: 'cluster', // Enable cluster mode for better performance
    
    // Restart Policy
    watch: false, // Disable file watching in production
    ignore_watch: ['node_modules', 'logs', 'uploads', '.git'],
    
    // Auto-restart settings
    max_memory_restart: '500M', // Restart if memory exceeds 500MB
    restart_delay: 4000, // Wait 4 seconds before restart
    
    // Logging
    log_file: 'logs/combined.log',
    out_file: 'logs/out.log',
    error_file: 'logs/error.log',
    log_date_format: 'YYYY-MM-DD HH:mm:ss Z',
    merge_logs: true,
    
    // Performance Monitoring
    pmx: true,
    
    // Advanced Settings
    kill_timeout: 5000, // Wait 5 seconds before force killing
    listen_timeout: 8000, // Wait 8 seconds for app to be ready
    
    // Source Control Integration
    post_update: ['npm install', 'npm run migrate', 'npm run seed'],
    
    // Health Check
    health_check_grace_period: 10000, // 10 seconds grace period
    
    // Node.js Options
    node_args: [
      '--max-old-space-size=1024', // Limit memory usage
      '--optimize-for-size' // Optimize for memory usage
    ],
    
    // Environment-specific overrides
    env_development: {
      NODE_ENV: 'development',
      PORT: 3000,
      watch: true,
      ignore_watch: ['node_modules', 'logs', 'uploads'],
      instances: 1,
      exec_mode: 'fork'
    }
  }],
  
  // Deployment Configuration
  deploy: {
    production: {
      user: 'root', // Update with your VPS user
      host: ['your-vps-ip'], // Update with your VPS IP
      ref: 'origin/main',
      repo: 'https://github.com/your-username/indo-advisory.git', // Update with your repo
      path: '/var/www/indo-advisory',
      'pre-deploy-local': '',
      'post-deploy': 'npm install && npm run migrate && npm run seed && pm2 reload ecosystem.config.js --env production',
      'pre-setup': 'apt update && apt install git -y'
    },
    
    staging: {
      user: 'root',
      host: ['staging-server-ip'],
      ref: 'origin/staging',
      repo: 'https://github.com/your-username/indo-advisory.git',
      path: '/var/www/indo-advisory-staging',
      'post-deploy': 'npm install && npm run migrate && pm2 reload ecosystem.config.js --env staging'
    }
  }
};