Files
ai-news-writer/ecosystem.config.cjs
T
David Santos 7f941e3cf7
Node tests / test (push) Has been cancelled
chore(pm2): auto DEPLOY_REVISION from git in ecosystem
When DEPLOY_REVISION/GIT_REVISION are unset, ecosystem.config.cjs runs
git rev-parse --short HEAD at pm2 start so /health deploy_revision matches
the checkout. Documented in README and .env.example.

Made-with: Cursor
2026-03-25 02:50:38 +00:00

58 lines
1.4 KiB
JavaScript

'use strict';
const { execSync } = require('child_process');
/**
* For GET /health `deploy_revision`: honour existing env, else short git SHA when `.git` exists.
*/
function resolveDeployRevisionForPm2() {
const fromEnv = (process.env.DEPLOY_REVISION || process.env.GIT_REVISION || '').trim();
if (fromEnv) {
return fromEnv;
}
try {
return execSync('git rev-parse --short HEAD', {
cwd: __dirname,
encoding: 'utf8',
stdio: ['ignore', 'pipe', 'ignore'],
maxBuffer: 64,
}).trim();
} catch (_err) {
return '';
}
}
const _deploy_revision = resolveDeployRevisionForPm2();
/**
* PM2 process definition — always runs `listen` (HTTP + RSS cron).
* Prefer this over `pm2 start src/index.js ... -- listen` to avoid footguns.
*
* Usage (inside CT, after clone + npm install + .env):
* pm2 start ecosystem.config.cjs
* pm2 save && pm2 startup
*/
module.exports = {
apps: [
{
name: 'ai-news-writer',
script: 'src/index.js',
args: ['listen'],
cwd: __dirname,
instances: 1,
autorestart: true,
watch: false,
max_memory_restart: '512M',
// Allow SIGTERM/SIGINT handler to stop cron + close HTTP before PM2 sends SIGKILL
kill_timeout: 15_000,
time: true,
env: {
NODE_ENV: 'production',
HOST: '0.0.0.0',
PORT: '3200',
...(_deploy_revision ? { DEPLOY_REVISION: _deploy_revision } : {}),
},
},
],
};