From 52d6aaca6e043e2bf3ec663a410ce5b624e47347 Mon Sep 17 00:00:00 2001 From: Jesse Cogollo Date: Fri, 21 Jun 2019 17:03:37 -0500 Subject: [PATCH 1/5] doc: add example function isDead to cluster [x] documentation is changed or added --- doc/api/cluster.md | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/doc/api/cluster.md b/doc/api/cluster.md index 340c2968cb6fb0..057bb3516ee0cd 100644 --- a/doc/api/cluster.md +++ b/doc/api/cluster.md @@ -382,6 +382,41 @@ added: v0.11.14 This function returns `true` if the worker's process has terminated (either because of exiting or being signaled). Otherwise, it returns `false`. +```js +const cluster = require('cluster'); +const http = require('http'); +const numCPUs = require('os').cpus().length; + +if (cluster.isMaster) { + console.log(`Master ${process.pid} is running`); + + // Fork workers. + for (let i = 0; i < numCPUs; i++) { + cluster.fork(); + } + + cluster.on('fork', (worker) => { + console.log('worker is dead:', worker.isDead()); + }); + + cluster.on('exit', (worker, code, signal) => { + console.log('worker is dead:', worker.isDead()); + }); + +} else { + // Workers can share any TCP connection + // In this case it is an HTTP server + http.createServer((req, res) => { + res.writeHead(200); + res.end(`Current process\n ${process.pid}`); + process.kill(process.pid); + }).listen(8000); + + // Make http://localhost:8000 to ckeck isDead method. +} + +``` + ### worker.kill([signal='SIGTERM'])