File tree 1 file changed +35
-0
lines changed
1 file changed +35
-0
lines changed Original file line number Diff line number Diff line change @@ -381,6 +381,41 @@ added: v0.11.14
381
381
This function returns ` true ` if the worker's process has terminated (either
382
382
because of exiting or being signaled). Otherwise, it returns ` false ` .
383
383
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
+
384
419
### worker.kill([ signal='SIGTERM'] )
385
420
<!-- YAML
386
421
added: v0.9.12
You can’t perform that action at this time.
0 commit comments