← back to Designer Wallcoverings

DW-Agents/dw-agents/agent-server-uptime/ecosystem.config.js

71 lines

/**
 * PM2 Ecosystem Configuration for Server Uptime Agent
 *
 * Features:
 * - Auto-restart with exponential backoff
 * - File watching for auto-reload
 * - Unlimited restart attempts
 * - Error logging to file
 * - Crash recovery
 */

module.exports = {
  apps: [{
    name: 'dw-server-uptime',
    script: '/root/Projects/Designer-Wallcoverings/DW-Agents/node_modules/.bin/tsx',
    args: 'agent-server-uptime/server-uptime-agent.ts',
    cwd: '/root/Projects/Designer-Wallcoverings/DW-Agents',

    // Restart configuration
    autorestart: true,
    max_restarts: 0, // Unlimited - never give up!
    min_uptime: '10s', // Must run 10s to be considered started
    max_memory_restart: '500M', // Restart if memory exceeds 500MB

    // Exponential backoff for crash loops
    exp_backoff_restart_delay: 100, // Start at 100ms
    restart_delay: 1000, // Base delay between restarts (1s)

    // Watch for file changes (optional, disable in production if too aggressive)
    watch: [
      'agent-server-uptime/server-uptime-agent.ts',
      'agent-server-uptime/shared-auth.ts',
      'agent-server-uptime/shared-chat.ts'
    ],
    watch_delay: 1000, // Wait 1s after file change before restarting
    ignore_watch: [
      'node_modules',
      'logs',
      '*.log',
      '*.json',
      '.git'
    ],

    // Environment
    env: {
      NODE_ENV: 'production',
      PORT: 9888
    },

    // Logging
    error_file: '/root/Projects/Designer-Wallcoverings/DW-Agents/logs/uptime-agent-error.log',
    out_file: '/root/Projects/Designer-Wallcoverings/DW-Agents/logs/uptime-agent-out.log',
    log_date_format: 'YYYY-MM-DD HH:mm:ss Z',
    merge_logs: true,

    // Advanced options
    kill_timeout: 5000, // Wait 5s for graceful shutdown
    listen_timeout: 10000, // Wait 10s for app to be ready

    // Instance configuration
    instances: 1, // Single instance (not clustered)
    exec_mode: 'fork',

    // Auto-recovery
    instance_var: 'INSTANCE_ID',

    // PM2 Plus monitoring (optional)
    // pmx: true
  }]
};