It seems all instances are cluster.Worker in pm2 cluster mode and cluser.isMaster are all false .
In my web app , i'm going to collect some system info every minutes and send to a monitor server.
I don't want to do this in every instance.
So is there an equivalent to cluster.isMaster or any other way ?
node -v 4.3.1
pm2 -v 1.0.2
After investigating the source code of pm2 , i found the master is the cli process itself.
And i decide to use process.env.pm_id , is this the right way?
if (process.env && process.env.pm_id) {
//running in pm2
if (process.env.pm_id % os.cpus().length !== 0) {
return;
}else{
collectSysInfo();
}
}
Yes exactly, to detect that your script is run into pm2, you can check process.env.pm_id or process.env.NODE_APP_INSTANCE
Thanks and learned a lot from the source code . Really a great project!
the right way is use process.env.NODE_APP_INSTANCE
As of yet the environment variable is also coming without under scores: NODE APP INSTANCE
One possible approach:
const _ = require('lodash');
const cluster = require('cluster');
function isMasterProcess() {
if (_.has(process.env, 'NODE APP INSTANCE')) {
return _.get(process.env, 'NODE APP INSTANCE') === '0';
} else if (_.has(process.env, 'NODE_APP_INSTANCE')) {
return _.get(process.env, 'NODE_APP_INSTANCE') === '0';
} else {
return cluster.isMaster;
}
}
@Unitech Can you please help me with this problem: https://stackoverflow.com/questions/51118808/export-pm2-cluster-stats-to-prometheus-and-then-to-grafana
Its regarding monitoring pm2 cluster using prometheus and grafana. I tried a lot of things as mentioned in the post. Not sure how to achieve it.
@tvvignesh It's strictly not possible to run code that isn't a worker because the master is the pm2 daemon, which managed your processes. You must aggregate your metrics somewhere like a web server.
What was suggested above is to only send metrics for one process, and in this case you can just check the internal id of the process that pm2 use.
Most helpful comment
After investigating the source code of pm2 , i found the master is the cli process itself.
And i decide to use
process.env.pm_id, is this the right way?