pm2
pm2 copied to clipboard
pm2 cluster mode can not assign different process.env to worker as nodejs cluster.fork([env]) does
What's going wrong?
pm2 cluster mode can not assign different process.env to worker as nodejs cluster.fork([env]) does. For example, if not using pm2, my cluster.fork will assign different env for works, like following
if (cluster.isMaster) {
x = [1, 2, 3, 4];
for (let i = 0; i < 4; i++) {
cluster.fork({x:x[i]}); //set different env for each worker
}} else {
console.log(process.env.x);
//using its own process.env.x
}
But it seems with pm2 I can't do that.
$ pm2 report
--- PM2 report ----------------------------------------------------------------
Date : Tue Aug 02 2022 14:22:38 GMT+0800 (China Standard Time)
===============================================================================
--- Daemon -------------------------------------------------
pm2d version : 5.2.0
node version : 16.14.2
node path : /Users/qiulang/Projects/call-center/ws_redis/node_modules/.bin/pm2
argv : /usr/local/bin/node,/Users/qiulang/Projects/call-center/ws_redis/node_modules/pm2/lib/Daemon.js
argv0 : node
user : qiulang
uid : 501
gid : 20
uptime : 922min
===============================================================================
--- CLI ----------------------------------------------------
local pm2 : 5.2.0
node version : 16.14.2
node path : /Users/qiulang/Projects/call-center/ws_redis/node_modules/.bin/pm2
argv : /usr/local/bin/node,/Users/qiulang/Projects/call-center/ws_redis/node_modules/.bin/pm2,report
argv0 : node
user : qiulang
uid : 501
gid : 20
===============================================================================
--- System info --------------------------------------------
arch : arm64
platform : darwin
type : Darwin
cpus : Apple M1
cpus nb : 8
freemem : 139640832
totalmem : 8589934592
home : /Users/qiulang
===============================================================================
--- PM2 list -----------------------------------------------
┌─────┬─────────────┬─────────────┬─────────┬─────────┬──────────┬────────┬──────┬───────────┬──────────┬──────────┬──────────┬──────────┐
│ id │ name │ namespace │ version │ mode │ pid │ uptime │ ↺ │ status │ cpu │ mem │ user │ watching │
├─────┼─────────────┼─────────────┼─────────┼─────────┼──────────┼────────┼──────┼───────────┼──────────┼──────────┼──────────┼──────────┤
│ 0 │ ws_redis │ default │ 2.0.0 │ fork │ 0 │ 0 │ 0 │ stopped │ 0% │ 0b │ qiulang │ disabled │
└─────┴─────────────┴─────────────┴─────────┴─────────┴──────────┴────────┴──────┴───────────┴──────────┴──────────┴──────────┴──────────┘
===============================================================================
--- Daemon logs --------------------------------------------
/Users/qiulang/.pm2/pm2.log last 20 lines:
PM2 | 2022-08-01T23:00:27: PM2 log: --- New PM2 Daemon started ----------------------------------------------------
PM2 | 2022-08-01T23:00:27: PM2 log: Time : Mon Aug 01 2022 23:00:27 GMT+0800 (China Standard Time)
PM2 | 2022-08-01T23:00:27: PM2 log: PM2 version : 5.2.0
PM2 | 2022-08-01T23:00:27: PM2 log: Node.js version : 16.14.2
PM2 | 2022-08-01T23:00:27: PM2 log: Current arch : arm64
PM2 | 2022-08-01T23:00:27: PM2 log: PM2 home : /Users/qiulang/.pm2
PM2 | 2022-08-01T23:00:27: PM2 log: PM2 PID file : /Users/qiulang/.pm2/pm2.pid
PM2 | 2022-08-01T23:00:27: PM2 log: RPC socket file : /Users/qiulang/.pm2/rpc.sock
PM2 | 2022-08-01T23:00:27: PM2 log: BUS socket file : /Users/qiulang/.pm2/pub.sock
PM2 | 2022-08-01T23:00:27: PM2 log: Application log path : /Users/qiulang/.pm2/logs
PM2 | 2022-08-01T23:00:27: PM2 log: Worker Interval : 30000
PM2 | 2022-08-01T23:00:27: PM2 log: Process dump file : /Users/qiulang/.pm2/dump.pm2
PM2 | 2022-08-01T23:00:27: PM2 log: Concurrent actions : 2
PM2 | 2022-08-01T23:00:27: PM2 log: SIGTERM timeout : 1600
PM2 | 2022-08-01T23:00:27: PM2 log: ===============================================================================
PM2 | 2022-08-01T23:00:27: PM2 log: App [ws_redis:0] starting in -fork mode-
PM2 | 2022-08-01T23:00:27: PM2 log: App [ws_redis:0] online
PM2 | 2022-08-01T23:01:04: PM2 log: Stopping app:ws_redis id:0
PM2 | 2022-08-01T23:01:04: PM2 log: App [ws_redis:0] exited with code [0] via signal [SIGINT]
PM2 | 2022-08-01T23:01:04: PM2 log: pid=3982 msg=process killed
I briefly check the cluster code https://github.com/Unitech/pm2/blob/master/lib/God/ClusterMode.js#L48
clu = cluster.fork({pm2_env: JSON.stringify(env_copy), windowsHide: true});
So I assume currently pm2 can't do that.
Could it be helpful to use env.pm_id like https://github.com/Hexagon/croner/issues/86
I checked the issue you mentioned and process.env.pm_id
but I don't think they would help my question, which is to assign different process.env to different worker.