pm2 cluster mode can not assign different process.env to worker as nodejs cluster.fork([env]) does
Open
Nobody has claimed this yet.
- Dominant language
- JavaScript
- Stars
- 43.3k
- Forks
- 2.7k
- PR merge metrics
- No merged PRs in 30d
Description
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
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
No repository file or test is named. Start by tracing PM2 cluster mode against Node.js cluster.fork([env]) and its per-worker environment handling; done means workers can receive distinct process.env values and a regression test covers the behavior.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- javascript, nodejs
- Domain
- backend, cli
- Issue type
- Feature
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 25/100