Browse Source

Fix PM2 env loading: parse .env.production via fs instead of env_file

PM2's env_file option is unreliable across versions. Replace with a
loadEnv() helper that reads and parses .env.production at startup time,
injecting all variables directly into the env section.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Tony T 1 week ago
parent
commit
25bc2c6ccf
1 changed files with 28 additions and 1 deletions
  1. 28 1
      ecosystem.config.cjs

+ 28 - 1
ecosystem.config.cjs

@@ -3,13 +3,40 @@
 // Save:    pm2 save
 // Startup: pm2 startup systemd
 
+const fs = require("fs");
+const path = require("path");
+
+// Parse .env.production manually so PM2 always picks up the latest values
+// regardless of PM2 version's env_file support.
+function loadEnv(envPath) {
+  try {
+    const content = fs.readFileSync(envPath, "utf8");
+    const env = {};
+    for (const line of content.split("\n")) {
+      const trimmed = line.trim();
+      if (!trimmed || trimmed.startsWith("#")) continue;
+      const eqIdx = trimmed.indexOf("=");
+      if (eqIdx === -1) continue;
+      const key = trimmed.slice(0, eqIdx).trim();
+      const val = trimmed.slice(eqIdx + 1).trim();
+      env[key] = val;
+    }
+    return env;
+  } catch {
+    return {};
+  }
+}
+
+const ENV_FILE = path.resolve(__dirname, ".env.production");
+const env = loadEnv(ENV_FILE);
+
 module.exports = {
   apps: [
     {
       name: "homelegance-chat",
       script: "./dist/index.js",
       cwd: "/redant/web/homelegance-chatbot",
-      env_file: "/redant/web/homelegance-chatbot/.env.production",
+      env,                          // inject all vars from .env.production
       instances: 1,
       autorestart: true,
       max_memory_restart: "512M",