Skip to content

Commit b6d0cbc

Browse files
jessecogollotargos
authored andcommitted
doc: add example code for worker.isDead() to cluster.md
PR-URL: #28362 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Rich Trott <[email protected]> Reviewed-By: Trivikram Kamat <[email protected]>
1 parent 69f17f1 commit b6d0cbc

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

doc/api/cluster.md

+35
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,41 @@ added: v0.11.14
381381
This function returns `true` if the worker's process has terminated (either
382382
because of exiting or being signaled). Otherwise, it returns `false`.
383383

384+
```js
385+
const cluster = require('cluster');
386+
const http = require('http');
387+
const numCPUs = require('os').cpus().length;
388+
389+
if (cluster.isMaster) {
390+
console.log(`Master ${process.pid} is running`);
391+
392+
// Fork workers.
393+
for (let i = 0; i < numCPUs; i++) {
394+
cluster.fork();
395+
}
396+
397+
cluster.on('fork', (worker) => {
398+
console.log('worker is dead:', worker.isDead());
399+
});
400+
401+
cluster.on('exit', (worker, code, signal) => {
402+
console.log('worker is dead:', worker.isDead());
403+
});
404+
405+
} else {
406+
// Workers can share any TCP connection
407+
// In this case it is an HTTP server
408+
http.createServer((req, res) => {
409+
res.writeHead(200);
410+
res.end(`Current process\n ${process.pid}`);
411+
process.kill(process.pid);
412+
}).listen(8000);
413+
414+
// Make http://localhost:8000 to ckeck isDead method.
415+
}
416+
417+
```
418+
384419
### worker.kill([signal='SIGTERM'])
385420
<!-- YAML
386421
added: v0.9.12

0 commit comments

Comments
 (0)