clustering.js 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. const debug = require('debug')('log4js:clustering');
  2. const LoggingEvent = require('./LoggingEvent');
  3. const configuration = require('./configuration');
  4. const cluster = require('cluster');
  5. const listeners = [];
  6. let disabled = false;
  7. let pm2 = false;
  8. let pm2InstanceVar = 'NODE_APP_INSTANCE';
  9. const isPM2Master = () => pm2 && process.env[pm2InstanceVar] === '0';
  10. const isMaster = () => disabled || cluster.isMaster || isPM2Master();
  11. const sendToListeners = (logEvent) => {
  12. listeners.forEach(l => l(logEvent));
  13. };
  14. // in a multi-process node environment, worker loggers will use
  15. // process.send
  16. const receiver = (worker, message) => {
  17. // prior to node v6, the worker parameter was not passed (args were message, handle)
  18. debug('cluster message received from worker ', worker, ': ', message);
  19. if (worker.topic && worker.data) {
  20. message = worker;
  21. worker = undefined;
  22. }
  23. if (message && message.topic && message.topic === 'log4js:message') {
  24. debug('received message: ', message.data);
  25. const logEvent = LoggingEvent.deserialise(message.data);
  26. sendToListeners(logEvent);
  27. }
  28. };
  29. configuration.addListener((config) => {
  30. // clear out the listeners, because configure has been called.
  31. listeners.length = 0;
  32. disabled = config.disableClustering;
  33. pm2 = config.pm2;
  34. pm2InstanceVar = config.pm2InstanceVar || 'NODE_APP_INSTANCE';
  35. debug(`clustering disabled ? ${disabled}`);
  36. debug(`cluster.isMaster ? ${cluster.isMaster}`);
  37. debug(`pm2 enabled ? ${pm2}`);
  38. debug(`pm2InstanceVar = ${pm2InstanceVar}`);
  39. debug(`process.env[${pm2InstanceVar}] = ${process.env[pm2InstanceVar]}`);
  40. // just in case configure is called after shutdown
  41. if (pm2) {
  42. process.removeListener('message', receiver);
  43. }
  44. if (cluster.removeListener) {
  45. cluster.removeListener('message', receiver);
  46. }
  47. if (config.disableClustering) {
  48. debug('Not listening for cluster messages, because clustering disabled.');
  49. } else if (isPM2Master()) {
  50. // PM2 cluster support
  51. // PM2 runs everything as workers - install pm2-intercom for this to work.
  52. // we only want one of the app instances to write logs
  53. debug('listening for PM2 broadcast messages');
  54. process.on('message', receiver);
  55. } else if (cluster.isMaster) {
  56. debug('listening for cluster messages');
  57. cluster.on('message', receiver);
  58. } else {
  59. debug('not listening for messages, because we are not a master process');
  60. }
  61. });
  62. module.exports = {
  63. onlyOnMaster: (fn, notMaster) => (isMaster() ? fn() : notMaster),
  64. isMaster: isMaster,
  65. send: (msg) => {
  66. if (isMaster()) {
  67. sendToListeners(msg);
  68. } else {
  69. if (!pm2) {
  70. msg.cluster = {
  71. workerId: cluster.worker.id,
  72. worker: process.pid
  73. };
  74. }
  75. process.send({ topic: 'log4js:message', data: msg.serialise() });
  76. }
  77. },
  78. onMessage: (listener) => {
  79. listeners.push(listener);
  80. }
  81. };