const { execSync } = require('child_process'); const path = require('path'); // Cleanup function to kill any process using specified ports const cleanupPorts = () => { const ports = [4031, 4032]; // Backend and dev frontend ports ports.forEach(port => { try { // Find process using the port and kill it const output = execSync(`lsof -ti :${port}`, { encoding: 'utf-8' }).trim(); if (output) { const pids = output.split('\n').filter(Boolean); pids.forEach(pid => { try { process.kill(parseInt(pid), 9); console.log(`[Cleanup] Killed process ${pid} on port ${port}`); } catch (err) { // Process already dead or permission issue } }); } } catch (err) { // Port not in use or lsof not available } }); // Give system time to release ports const start = Date.now(); while (Date.now() - start < 1000) { // Sleep for 1 second } }; module.exports = { apps: [ { name: 'tilbudgivern-unified', script: path.join(__dirname, 'backend', 'unified-server.js'), cwd: path.join(__dirname, 'backend'), watch: false, max_memory_restart: '500M', env: { NODE_ENV: 'production', PORT: 4032 }, exec_mode: 'fork', error_file: '.pm2/logs/tilbudgivern-unified-error.log', out_file: '.pm2/logs/tilbudgivern-unified-out.log', merge_logs: false, autorestart: true, max_restarts: 10, min_uptime: '10s', kill_timeout: 5000, // 5 second grace period before SIGKILL listen_timeout: 3000, // 3 second timeout for listen to succeed shutdown_with_message: true, instance_var: 'INSTANCE_ID' } ] };