clustering.js 3.0 KB

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