/** * For Node apps - Cluster mode * It will wrap the code and enable load-balancing mode * @method nodeApp * @param {} env_copy * @param {} cb * @returnLiteral */ God.nodeApp = functionnodeApp(env_copy, cb){ var clu = null;
console.log(`App [${env_copy.name}:${env_copy.pm_id}] starting in -cluster mode-`) if (env_copy.node_args && Array.isArray(env_copy.node_args)) { cluster.settings.execArgv = env_copy.node_args; }
env_copy._pm2_version = pkg.version;
try { // node.js cluster clients can not receive deep-level objects or arrays in the forked process, e.g.: // { "args": ["foo", "bar"], "env": { "foo1": "bar1" }} will be parsed to // { "args": "foo, bar", "env": "[object Object]"} // So we passing a stringified JSON here. clu = cluster.fork({pm2_env: JSON.stringify(env_copy), windowsHide: true}); } catch(e) { God.logAndGenerateError(e); return cb(e); }
clu.pm2_env = env_copy;
/** * Broadcast message to God */ clu.on('message', functioncluMessage(msg) { /********************************* * If you edit this function * Do the same in ForkMode.js ! *********************************/ if (msg.data && msg.type) { returnGod.bus.emit(msg.type ? msg.type: 'process:msg', { at : Utility.getDate(), data : msg.data, process : { pm_id : clu.pm2_env.pm_id, name : clu.pm2_env.name, rev : (clu.pm2_env.versioning && clu.pm2_env.versioning.revision) ? clu.pm2_env.versioning.revision : null, namespace : clu.pm2_env.namespace } }); } else {
if (typeof msg == 'object' && 'node_version' in msg) { clu.pm2_env.node_version = msg.node_version; returnfalse; }
return God.bus.emit('process:msg', { at : Utility.getDate(), raw : msg, process : { pm_id : clu.pm2_env.pm_id, name : clu.pm2_env.name, namespace : clu.pm2_env.namespace } }); } });
return cb(null, clu); }; };
我们还可以看下cluster mode的测试用例
God.js的依赖
1 2 3 4 5 6 7 8 9 10 11 12
var cluster = require('cluster'); var numCPUs = require('os').cpus() ? require('os').cpus().length : 1; var path = require('path'); var EventEmitter2 = require('eventemitter2').EventEmitter2; var fs = require('fs'); var vizion = require('vizion'); var debug = require('debug')('pm2:god'); var Utility = require('./Utility'); var cst = require('../constants.js'); var timesLimit = require('async/timesLimit'); var Configuration = require('./Configuration.js'); var semver = require('semver');
# server pm2 link xxx-sec-key xxx-pub-key # docker RUN npm install pm2 -g ENV PM2_PUBLIC_KEY xxx ENV PM2_SECRET_KEY xxx
CMD ["pm2-runtime", "app.js"] # pm2 link pm2 link -h
Usage: link [options] [secret] [public] [name]
linkwith the pm2 monitoring dashboard
Options:
--info-node [url] set url info node --ws websocket mode --axon axon mode -h, --help output usage information
Launch PM2 in no deamon
Make sure you kill any PM2 instance before starting PM2 in no deamon mode (pm2 kill).
Launching PM2 without daemonizing itself:
pm2 start app.js –no-daemon There is also the CLI pm2-runtime installed by default at PM2 installation, that is a drop-in replacement of the Node.js binary.