@@ -108,12 +108,18 @@ responsibility to manage the worker pool for your application's needs.
108
108
109
109
110
110
## Class: Worker
111
+ <!-- YAML
112
+ added: v0.7.0
113
+ -->
111
114
112
115
A Worker object contains all public information and method about a worker.
113
116
In the master it can be obtained using ` cluster.workers ` . In a worker
114
117
it can be obtained using ` cluster.worker ` .
115
118
116
119
### Event: 'disconnect'
120
+ <!-- YAML
121
+ added: v0.7.7
122
+ -->
117
123
118
124
Similar to the ` cluster.on('disconnect') ` event, but specific to this worker.
119
125
@@ -124,12 +130,18 @@ cluster.fork().on('disconnect', () => {
124
130
```
125
131
126
132
### Event: 'error'
133
+ <!-- YAML
134
+ added: v0.7.3
135
+ -->
127
136
128
137
This event is the same as the one provided by [ ` child_process.fork() ` ] [ ] .
129
138
130
139
In a worker you can also use ` process.on('error') ` .
131
140
132
141
### Event: 'exit'
142
+ <!-- YAML
143
+ added: v0.11.2
144
+ -->
133
145
134
146
* ` code ` {Number} the exit code, if it exited normally.
135
147
* ` signal ` {String} the name of the signal (eg. ` 'SIGHUP' ` ) that caused
@@ -151,6 +163,9 @@ worker.on('exit', (code, signal) => {
151
163
```
152
164
153
165
### Event: 'listening'
166
+ <!-- YAML
167
+ added: v0.7.0
168
+ -->
154
169
155
170
* ` address ` {Object}
156
171
@@ -165,6 +180,9 @@ cluster.fork().on('listening', (address) => {
165
180
It is not emitted in the worker.
166
181
167
182
### Event: 'message'
183
+ <!-- YAML
184
+ added: v0.7.0
185
+ -->
168
186
169
187
* ` message ` {Object}
170
188
* ` handle ` {undefined|Object}
@@ -220,6 +238,9 @@ if (cluster.isMaster) {
220
238
```
221
239
222
240
### Event: 'online'
241
+ <!-- YAML
242
+ added: v0.7.0
243
+ -->
223
244
224
245
Similar to the ` cluster.on('online') ` event, but specific to this worker.
225
246
@@ -232,6 +253,9 @@ cluster.fork().on('online', () => {
232
253
It is not emitted in the worker.
233
254
234
255
### worker.disconnect()
256
+ <!-- YAML
257
+ added: v0.7.7
258
+ -->
235
259
236
260
In a worker, this function will close all servers, wait for the ` 'close' ` event on
237
261
those servers, and then disconnect the IPC channel.
@@ -293,6 +317,9 @@ if (cluster.isMaster) {
293
317
```
294
318
295
319
### worker.id
320
+ <!-- YAML
321
+ added: v0.8.0
322
+ -->
296
323
297
324
* {Number}
298
325
@@ -303,17 +330,26 @@ While a worker is alive, this is the key that indexes it in
303
330
cluster.workers
304
331
305
332
### worker.isConnected()
333
+ <!-- YAML
334
+ added: v0.11.14
335
+ -->
306
336
307
337
This function returns ` true ` if the worker is connected to its master via its IPC
308
338
channel, ` false ` otherwise. A worker is connected to its master after it's been
309
339
created. It is disconnected after the ` 'disconnect' ` event is emitted.
310
340
311
341
### worker.isDead()
342
+ <!-- YAML
343
+ added: v0.11.14
344
+ -->
312
345
313
346
This function returns ` true ` if the worker's process has terminated (either
314
347
because of exiting or being signaled). Otherwise, it returns ` false ` .
315
348
316
349
### worker.kill([ signal='SIGTERM'] )
350
+ <!-- YAML
351
+ added: v0.9.12
352
+ -->
317
353
318
354
* ` signal ` {String} Name of the kill signal to send to the worker
319
355
process.
@@ -330,6 +366,9 @@ Note that in a worker, `process.kill()` exists, but it is not this function,
330
366
it is [ ` kill ` ] [ ] .
331
367
332
368
### worker.process
369
+ <!-- YAML
370
+ added: v0.7.0
371
+ -->
333
372
334
373
* {ChildProcess}
335
374
@@ -344,6 +383,9 @@ on `process` and `.suicide` is not `true`. This protects against accidental
344
383
disconnection.
345
384
346
385
### worker.send(message[ , sendHandle] [ , callback ] )
386
+ <!-- YAML
387
+ added: v0.7.0
388
+ -->
347
389
348
390
* ` message ` {Object}
349
391
* ` sendHandle ` {Handle}
@@ -373,6 +415,10 @@ if (cluster.isMaster) {
373
415
```
374
416
375
417
### worker.suicide
418
+ <!-- YAML
419
+ added: v0.7.0
420
+ deprecated: v6.0.0
421
+ -->
376
422
377
423
* {Boolean}
378
424
@@ -393,6 +439,9 @@ worker.kill();
393
439
```
394
440
395
441
## Event: 'disconnect'
442
+ <!-- YAML
443
+ added: v0.7.9
444
+ -->
396
445
397
446
* ` worker ` {cluster.Worker}
398
447
@@ -411,6 +460,9 @@ cluster.on('disconnect', (worker) => {
411
460
```
412
461
413
462
## Event: 'exit'
463
+ <!-- YAML
464
+ added: v0.7.9
465
+ -->
414
466
415
467
* ` worker ` {cluster.Worker}
416
468
* ` code ` {Number} the exit code, if it exited normally.
@@ -432,6 +484,9 @@ cluster.on('exit', (worker, code, signal) => {
432
484
See [ child_process event: 'exit'] [ ] .
433
485
434
486
## Event: 'fork'
487
+ <!-- YAML
488
+ added: v0.7.0
489
+ -->
435
490
436
491
* ` worker ` {cluster.Worker}
437
492
@@ -457,6 +512,9 @@ cluster.on('exit', (worker, code, signal) => {
457
512
```
458
513
459
514
## Event: 'listening'
515
+ <!-- YAML
516
+ added: v0.7.0
517
+ -->
460
518
461
519
* ` worker ` {cluster.Worker}
462
520
* ` address ` {Object}
@@ -493,6 +551,9 @@ Emitted when the cluster master receives a message from any worker.
493
551
See [ child_process event: 'message'] [ ] .
494
552
495
553
## Event: 'online'
554
+ <!-- YAML
555
+ added: v0.7.0
556
+ -->
496
557
497
558
* ` worker ` {cluster.Worker}
498
559
@@ -508,6 +569,9 @@ cluster.on('online', (worker) => {
508
569
```
509
570
510
571
## Event: 'setup'
572
+ <!-- YAML
573
+ added: v0.7.1
574
+ -->
511
575
512
576
* ` settings ` {Object}
513
577
@@ -520,6 +584,9 @@ The `settings` object is the `cluster.settings` object at the time
520
584
If accuracy is important, use ` cluster.settings ` .
521
585
522
586
## cluster.disconnect([ callback] )
587
+ <!-- YAML
588
+ added: v0.7.7
589
+ -->
523
590
524
591
* ` callback ` {Function} called when all workers are disconnected and handles are
525
592
closed
@@ -534,6 +601,9 @@ The method takes an optional callback argument which will be called when finishe
534
601
This can only be called from the master process.
535
602
536
603
## cluster.fork([ env] )
604
+ <!-- YAML
605
+ added: v0.6.0
606
+ -->
537
607
538
608
* ` env ` {Object} Key/value pairs to add to worker process environment.
539
609
* return {cluster.Worker}
@@ -543,6 +613,9 @@ Spawn a new worker process.
543
613
This can only be called from the master process.
544
614
545
615
## cluster.isMaster
616
+ <!-- YAML
617
+ added: v0.8.1
618
+ -->
546
619
547
620
* {Boolean}
548
621
@@ -551,12 +624,18 @@ by the `process.env.NODE_UNIQUE_ID`. If `process.env.NODE_UNIQUE_ID` is
551
624
undefined, then ` isMaster ` is ` true ` .
552
625
553
626
## cluster.isWorker
627
+ <!-- YAML
628
+ added: v0.6.0
629
+ -->
554
630
555
631
* {Boolean}
556
632
557
633
True if the process is not a master (it is the negation of ` cluster.isMaster ` ).
558
634
559
635
## cluster.schedulingPolicy
636
+ <!-- YAML
637
+ added: v0.11.2
638
+ -->
560
639
561
640
The scheduling policy, either ` cluster.SCHED_RR ` for round-robin or
562
641
` cluster.SCHED_NONE ` to leave it to the operating system. This is a
@@ -572,6 +651,9 @@ distribute IOCP handles without incurring a large performance hit.
572
651
values are ` "rr" ` and ` "none" ` .
573
652
574
653
## cluster.settings
654
+ <!-- YAML
655
+ added: v0.7.1
656
+ -->
575
657
576
658
* {Object}
577
659
* ` execArgv ` {Array} list of string arguments passed to the Node.js
@@ -590,6 +672,9 @@ the settings, including the default values.
590
672
This object is not supposed to be changed or set manually, by you.
591
673
592
674
## cluster.setupMaster([ settings] )
675
+ <!-- YAML
676
+ added: v0.7.1
677
+ -->
593
678
594
679
* ` settings ` {Object}
595
680
* ` exec ` {String} file path to worker file. (Default=` process.argv[1] ` )
@@ -630,6 +715,9 @@ cluster.fork(); // http worker
630
715
This can only be called from the master process.
631
716
632
717
## cluster.worker
718
+ <!-- YAML
719
+ added: v0.7.0
720
+ -->
633
721
634
722
* {Object}
635
723
@@ -648,6 +736,9 @@ if (cluster.isMaster) {
648
736
```
649
737
650
738
## cluster.workers
739
+ <!-- YAML
740
+ added: v0.7.0
741
+ -->
651
742
652
743
* {Object}
653
744
0 commit comments