Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit b6d0cbc

Browse filesBrowse files
jessecogollotargos
authored andcommitted
doc: add example code for worker.isDead() to cluster.md
PR-URL: #28362 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Rich Trott <rtrott@gmail.com> Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
1 parent 69f17f1 commit b6d0cbc
Copy full SHA for b6d0cbc

File tree

Expand file treeCollapse file tree

1 file changed

+35
-0
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

1 file changed

+35
-0
lines changed
Open diff view settings
Collapse file

‎doc/api/cluster.md‎

Copy file name to clipboardExpand all lines: doc/api/cluster.md
+35Lines changed: 35 additions & 0 deletions
  • Display the source diff
  • Display the rich diff
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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.