Last active
August 3, 2019 12:12
-
-
Save zbjornson/a7ab0ec5ea34e40a8a5695c652a9e96b to your computer and use it in GitHub Desktop.
Showing off all cluster events
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const cluster = require("cluster"); | |
if (cluster.isMaster) { | |
function messageHandler(msg) { | |
console.log(msg); | |
} | |
cluster.on("message", function (worker, message) { | |
console.log(`master: (cluster.onMessage) '${message}'`); | |
}); | |
// Never fires (not a bug) | |
process.on("message", function (message) { | |
console.log("master: process.onMessage", message); | |
}); | |
for (var i = 0; i < 2; i++) { | |
var worker = cluster.fork(); | |
worker.on("message", (function(i) { | |
return function (message, otherarg) { | |
// Event handler unique to this worker: | |
console.log(`master: (worker[${i+1}].onMessage) '${message}'`); | |
} | |
})(i)); | |
// Docs: Send a message to a worker or master. ... In the master this | |
// sends a message to the specified worker, identical to ChildProcess.send. | |
console.log(`<-- master sending message to worker ${i+1}-->`); | |
worker.send(`message from master to worker ${i+1}`); | |
} | |
} else { | |
// Docs: send a message to the master. | |
console.log(`<-- worker${cluster.worker.id} sending message -->`); | |
// These two are identical. | |
//cluster.worker.send(`message from worker ${cluster.worker.id} (via cluster.worker.send)`); | |
process.send(`message from worker ${cluster.worker.id} (via process.send)`); | |
// These two are also identical: | |
process.on("message", function (message) { | |
console.log(`worker[${cluster.worker.id}]: (process.onMessage) '${message}'`); | |
}); | |
cluster.worker.on("message", function (message) { | |
console.log(`worker[${cluster.worker.id}]: (cluster.worker.onMessage) '${message}'`); | |
}); | |
// Never fires | |
cluster.on("message", function (worker, message) { | |
console.log(`worker[${cluster.worker.id}]: (cluster.onMessage) '${message}'`); | |
}); | |
} | |
/* | |
Docs say: | |
Cluster: Worker: event: 'message' | |
Similar to the cluster.on("message") event, but specific to this worker. | |
In a worker you can also use process.on("message") | |
https://nodejs.org/dist/latest-v6.x/docs/api/cluster.html#cluster_event_message_1 | |
Cluster: event: 'message' | |
Emitted when any worker receives a message. // Receives!? | |
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
> node .\test-cluster.js | |
<-- master sending message to worker 1--> | |
<-- master sending message to worker 2--> | |
<-- worker1 sending message --> | |
master: (cluster.onMessage) 'message from worker 1 (via process.send)' | |
master: (worker[1].onMessage) 'message from worker 1 (via process.send)' | |
worker[1]: (cluster.worker.onMessage) 'message from master to worker 1' | |
worker[1]: (process.onMessage) 'message from master to worker 1' | |
<-- worker2 sending message --> | |
master: (cluster.onMessage) 'message from worker 2 (via process.send)' | |
master: (worker[2].onMessage) 'message from worker 2 (via process.send)' | |
worker[2]: (cluster.worker.onMessage) 'message from master to worker 2' | |
worker[2]: (process.onMessage) 'message from master to worker 2' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment