Skip to content

Commit 0ba2ea8

Browse files
y1d7ngtargos
authored andcommitted
doc: fix examples in cluster.md
PR-URL: #42889 Reviewed-By: Antoine du Hamel <[email protected]> Reviewed-By: Mohammed Keyvanzadeh <[email protected]>
1 parent 9dd6476 commit 0ba2ea8

File tree

1 file changed

+24
-24
lines changed

1 file changed

+24
-24
lines changed

doc/api/cluster.md

+24-24
Original file line numberDiff line numberDiff line change
@@ -197,31 +197,35 @@ Similar to the `cluster.on('exit')` event, but specific to this worker.
197197
```mjs
198198
import cluster from 'node:cluster';
199199

200-
const worker = cluster.fork();
201-
worker.on('exit', (code, signal) => {
202-
if (signal) {
203-
console.log(`worker was killed by signal: ${signal}`);
204-
} else if (code !== 0) {
205-
console.log(`worker exited with error code: ${code}`);
206-
} else {
207-
console.log('worker success!');
208-
}
209-
});
200+
if (cluster.isPrimary) {
201+
const worker = cluster.fork();
202+
worker.on('exit', (code, signal) => {
203+
if (signal) {
204+
console.log(`worker was killed by signal: ${signal}`);
205+
} else if (code !== 0) {
206+
console.log(`worker exited with error code: ${code}`);
207+
} else {
208+
console.log('worker success!');
209+
}
210+
});
211+
}
210212
```
211213

212214
```cjs
213215
const cluster = require('node:cluster');
214216

215-
const worker = cluster.fork();
216-
worker.on('exit', (code, signal) => {
217-
if (signal) {
218-
console.log(`worker was killed by signal: ${signal}`);
219-
} else if (code !== 0) {
220-
console.log(`worker exited with error code: ${code}`);
221-
} else {
222-
console.log('worker success!');
223-
}
224-
});
217+
if (cluster.isPrimary) {
218+
const worker = cluster.fork();
219+
worker.on('exit', (code, signal) => {
220+
if (signal) {
221+
console.log(`worker was killed by signal: ${signal}`);
222+
} else if (code !== 0) {
223+
console.log(`worker exited with error code: ${code}`);
224+
} else {
225+
console.log('worker success!');
226+
}
227+
});
228+
}
225229
```
226230

227231
### Event: `'listening'`
@@ -235,16 +239,12 @@ added: v0.7.0
235239
Similar to the `cluster.on('listening')` event, but specific to this worker.
236240

237241
```mjs
238-
import cluster from 'node:cluster';
239-
240242
cluster.fork().on('listening', (address) => {
241243
// Worker is listening
242244
});
243245
```
244246

245247
```cjs
246-
const cluster = require('node:cluster');
247-
248248
cluster.fork().on('listening', (address) => {
249249
// Worker is listening
250250
});

0 commit comments

Comments
 (0)