• Home
  • Raw
  • Download

Lines Matching refs:worker

29   cluster.on('exit', (worker, code, signal) => {
30 console.log(`worker ${worker.process.pid} died`);
55 On Windows, it is not yet possible to set up a named pipe server in a worker.
61 The worker processes are spawned using the [`child_process.fork()`][] method,
72 overloading a worker process.
86 Node.js process and a cluster worker differs:
90 handle passed to the worker, rather than listening to the worker's
93 the worker to use the supplied handle, rather than talk to the master
96 random port. However, in a cluster, each worker will receive the
99 on a unique port, generate a port number based on the cluster worker ID.
111 responsibility to manage the worker pool based on its own needs.
114 also be used for other use cases requiring worker processes.
123 A `Worker` object contains all public information and method about a worker.
124 In the master it can be obtained using `cluster.workers`. In a worker
125 it can be obtained using `cluster.worker`.
132 Similar to the `cluster.on('disconnect')` event, but specific to this worker.
147 Within a worker, `process.on('error')` may also be used.
158 Similar to the `cluster.on('exit')` event, but specific to this worker.
161 const worker = cluster.fork();
162 worker.on('exit', (code, signal) => {
164 console.log(`worker was killed by signal: ${signal}`);
166 console.log(`worker exited with error code: ${code}`);
168 console.log('worker success!');
180 Similar to the `cluster.on('listening')` event, but specific to this worker.
188 It is not emitted in the worker.
198 Similar to the `'message'` event of `cluster`, but specific to this worker.
200 Within a worker, `process.on('message')` may also be used.
254 Similar to the `cluster.on('online')` event, but specific to this worker.
262 It is not emitted in the worker.
264 ### `worker.disconnect()`
270 description: This method now returns a reference to `worker`.
273 * Returns: {cluster.Worker} A reference to `worker`.
275 In a worker, this function will close all servers, wait for the `'close'` event
278 In the master, an internal message is sent to the worker causing it to call
284 but connections may be accepted by any other listening worker. Existing
286 see [`server.close()`][], the IPC channel to the worker will close allowing it
293 In a worker, `process.disconnect` exists, but it is not this function;
298 close them. It also may be useful to implement a timeout, killing a worker if
303 const worker = cluster.fork();
306 worker.on('listening', (address) => {
307 worker.send('shutdown');
308 worker.disconnect();
310 worker.kill();
314 worker.on('disconnect', () => {
334 ### `worker.exitedAfterDisconnect`
341 This property is `true` if the worker exited due to `.kill()` or
342 `.disconnect()`. If the worker exited any other way, it is `false`. If the
343 worker has not exited, it is `undefined`.
345 The boolean [`worker.exitedAfterDisconnect`][] allows distinguishing between
346 voluntary and accidental exit, the master may choose not to respawn a worker
350 cluster.on('exit', (worker, code, signal) => {
351 if (worker.exitedAfterDisconnect === true) {
356 // kill worker
357 worker.kill();
360 ### `worker.id`
367 Each new worker is given its own unique id, this id is stored in the
370 While a worker is alive, this is the key that indexes it in
373 ### `worker.isConnected()`
378 This function returns `true` if the worker is connected to its master via its
379 IPC channel, `false` otherwise. A worker is connected to its master after it
382 ### `worker.isDead()`
387 This function returns `true` if the worker's process has terminated (either
403 cluster.on('fork', (worker) => {
404 console.log('worker is dead:', worker.isDead());
407 cluster.on('exit', (worker, code, signal) => {
408 console.log('worker is dead:', worker.isDead());
420 ### `worker.kill([signal])`
425 * `signal` {string} Name of the kill signal to send to the worker
428 This function will kill the worker. In the master, it does this by disconnecting
429 the `worker.process`, and once disconnected, killing with `signal`. In the
430 worker, it does it by disconnecting the channel, and then exiting with code `0`.
432 Because `kill()` attempts to gracefully disconnect the worker process, it is
434 if the worker enters an infinite loop, a graceful disconnect will never occur.
435 If the graceful disconnect behavior is not needed, use `worker.process.kill()`.
439 This method is aliased as `worker.destroy()` for backward compatibility.
441 In a worker, `process.kill()` exists, but it is not this function;
444 ### `worker.process`
452 from this function is stored as `.process`. In a worker, the global `process`
461 ### `worker.send(message[, sendHandle[, options]][, callback])`
481 Send a message to a worker or master, optionally with a handle.
483 In the master this sends a message to a specific worker. It is identical to
486 In a worker this sends a message to the master. It is identical to
493 const worker = cluster.fork();
494 worker.send('hi there');
508 * `worker` {cluster.Worker}
510 Emitted after the worker IPC channel has disconnected. This can occur when a
511 worker exits gracefully, is killed, or is disconnected manually (such as with
512 `worker.disconnect()`).
519 cluster.on('disconnect', (worker) => {
520 console.log(`The worker #${worker.id} has disconnected`);
529 * `worker` {cluster.Worker}
536 This can be used to restart the worker by calling [`.fork()`][] again.
539 cluster.on('exit', (worker, code, signal) => {
540 console.log('worker %d died (%s). restarting...',
541 worker.process.pid, signal || code);
553 * `worker` {cluster.Worker}
555 When a new worker is forked the cluster module will emit a `'fork'` event.
556 This can be used to log worker activity, and create a custom timeout.
564 cluster.on('fork', (worker) => {
565 timeouts[worker.id] = setTimeout(errorMsg, 2000);
567 cluster.on('listening', (worker, address) => {
568 clearTimeout(timeouts[worker.id]);
570 cluster.on('exit', (worker, code, signal) => {
571 clearTimeout(timeouts[worker.id]);
581 * `worker` {cluster.Worker}
584 After calling `listen()` from a worker, when the `'listening'` event is emitted
588 The event handler is executed with two arguments, the `worker` contains the
589 worker object and the `address` object contains the following connection
591 worker is listening on more than one address.
594 cluster.on('listening', (worker, address) => {
596 `A worker is now connected to ${address.address}:${address.port}`);
613 description: The `worker` parameter is passed now; see below for details.
616 * `worker` {cluster.Worker}
620 Emitted when the cluster master receives a message from any worker.
629 * `worker` {cluster.Worker}
631 After forking a new worker, the worker should respond with an online message.
634 master forks a worker, and `'online'` is emitted when the worker is running.
637 cluster.on('online', (worker) => {
638 console.log('Yay, the worker responded after it was forked');
665 Calls `.disconnect()` on each worker in `cluster.workers`.
680 * `env` {Object} Key/value pairs to add to worker process environment.
683 Spawn a new worker process.
714 global setting and effectively frozen once either the first worker is spawned,
751 * `exec` {string} File path to worker file. **Default:** `process.argv[1]`.
752 * `args` {string[]} String arguments passed to worker.
754 * `cwd` {string} Current working directory of the worker process. **Default:**
767 * `inspectPort` {number|Function} Sets inspector port of worker.
769 number. By default each worker gets its own port, incremented from the
796 The only attribute of a worker that cannot be set via `.setupMaster()` is
805 exec: 'worker.js',
809 cluster.fork(); // https worker
811 exec: 'worker.js',
814 cluster.fork(); // http worker
819 ## `cluster.worker`
826 A reference to the current worker object. Not available in the master process.
836 console.log(`I am worker #${cluster.worker.id}`);
847 A hash that stores the active worker objects, keyed by `id` field. Makes it
851 A worker is removed from `cluster.workers` after the worker has disconnected
863 eachWorker((worker) => {
864 worker.send('big announcement to all workers');
868 Using the worker's unique id is the easiest way to locate the worker.
872 const worker = cluster.workers[id];
889 [`worker.exitedAfterDisconnect`]: #cluster_worker_exitedafterdisconnect