source: trip-planner-front/node_modules/log4js/lib/clustering.js@ 6c1585f

Last change on this file since 6c1585f was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

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