Skip to content

Commit

Permalink
doc: modernize and simplify cluster example
Browse files Browse the repository at this point in the history
PR-URL: #41626
Reviewed-By: Mestery <mestery@protonmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
  • Loading branch information
tniessen authored Jan 23, 2022
1 parent c18ca14 commit 0e6db9c
Showing 1 changed file with 4 additions and 16 deletions.
20 changes: 4 additions & 16 deletions doc/api/cluster.md
Original file line number Diff line number Diff line change
Expand Up @@ -1074,29 +1074,17 @@ list happens before the last `'disconnect'` or `'exit'` event is emitted.
```mjs
import cluster from 'cluster';

// Go through all workers
function eachWorker(callback) {
for (const id in cluster.workers) {
callback(cluster.workers[id]);
}
}
eachWorker((worker) => {
for (const worker of Object.values(cluster.workers)) {
worker.send('big announcement to all workers');
});
}
```

```cjs
const cluster = require('cluster');

// Go through all workers
function eachWorker(callback) {
for (const id in cluster.workers) {
callback(cluster.workers[id]);
}
}
eachWorker((worker) => {
for (const worker of Object.values(cluster.workers)) {
worker.send('big announcement to all workers');
});
}
```

Using the worker's unique id is the easiest way to locate the worker.
Expand Down

0 comments on commit 0e6db9c

Please sign in to comment.