/**
 * PM2 process file for LaraBlog long-running workers.
 *
 * Usage (from project root):
 *   pm2 start ecosystem.config.cjs
 *   pm2 save
 *   pm2 startup
 *
 * Notes:
 * - Web HTTP is served by nginx/php-fpm (or Herd), NOT pm2.
 * - Adjust cwd if deploying outside this path.
 */
const path = require('path');

const root = __dirname;
const php = process.env.PHP_BINARY || 'php';

module.exports = {
  apps: [
    {
      name: 'larablog-queue',
      cwd: root,
      script: php,
      // Keep AI queues off this worker when larablog-ai-workerman is running,
      // otherwise both processes race-consume the same jobs.
      args: 'artisan queue:work redis --queue=default --sleep=3 --tries=3 --max-time=3600',
      interpreter: 'none',
      instances: 1,
      autorestart: true,
      max_restarts: 20,
      min_uptime: '10s',
      kill_timeout: 10000,
      out_file: path.join(root, 'storage/logs/pm2-queue.out.log'),
      error_file: path.join(root, 'storage/logs/pm2-queue.err.log'),
      merge_logs: true,
      time: true,
      env: {
        APP_ENV: 'production',
      },
    },
    {
      name: 'larablog-schedule',
      cwd: root,
      script: php,
      args: 'artisan schedule:work',
      interpreter: 'none',
      instances: 1,
      autorestart: true,
      max_restarts: 20,
      min_uptime: '10s',
      out_file: path.join(root, 'storage/logs/pm2-schedule.out.log'),
      error_file: path.join(root, 'storage/logs/pm2-schedule.err.log'),
      merge_logs: true,
      time: true,
      env: {
        APP_ENV: 'production',
      },
    },
    {
      name: 'larablog-ai-workerman',
      cwd: root,
      script: php,
      args: 'artisan workerman:ai start',
      interpreter: 'none',
      instances: 1,
      autorestart: true,
      max_restarts: 30,
      min_uptime: '5s',
      kill_timeout: 15000,
      out_file: path.join(root, 'storage/logs/pm2-ai.out.log'),
      error_file: path.join(root, 'storage/logs/pm2-ai.err.log'),
      merge_logs: true,
      time: true,
      env: {
        APP_ENV: 'production',
      },
    },
  ],
};
