diff options
author | Jesse Cogollo <cogollo87@gmail.com> | 2019-06-22 01:03:37 +0300 |
---|---|---|
committer | Rich Trott <rtrott@gmail.com> | 2019-06-25 06:49:18 +0300 |
commit | e2d445be8f5f786a7b3b6dc1724fc6080cb37b8c (patch) | |
tree | c743924f6f405b0f8cd76a52730fc426b29c5f9c /doc/api/cluster.md | |
parent | c3284822af61be6dcdc3c54bb4c05a7b6f025a99 (diff) |
doc: add example code for worker.isDead() to cluster.md
PR-URL: https://github.com/nodejs/node/pull/28362
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
Diffstat (limited to 'doc/api/cluster.md')
-rw-r--r-- | doc/api/cluster.md | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/doc/api/cluster.md b/doc/api/cluster.md index e72bf8f7f7d..a3dc1239aa8 100644 --- a/doc/api/cluster.md +++ b/doc/api/cluster.md @@ -381,6 +381,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']) <!-- YAML added: v0.9.12 |