@ynode/cluster 1.1.0 → 1.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +3 -0
- package/package.json +1 -1
- package/src/cluster.js +24 -25
package/README.md
CHANGED
|
@@ -62,6 +62,9 @@ The `run(startWorker, options)` function accepts the following options:
|
|
|
62
62
|
| `scaleDownThreshold` | `number` | `10` | Event loop lag (ms) threshold to trigger scaling down. |
|
|
63
63
|
| `scalingCooldown` | `number` | `10000` | Minimum time (ms) between scaling actions. |
|
|
64
64
|
| `scaleDownGrace` | `number` | `30000` | Grace period (ms) after scaling up before scaling down is allowed. |
|
|
65
|
+
| `autoScaleInterval` | `number` | `5000` | Interval (ms) for auto-scaling checks in "smart" mode. |
|
|
66
|
+
| `shutdownSignals` | `string[]` | `['SIGINT', 'SIGTERM', 'SIGQUIT']` | Signals to listen for to trigger graceful shutdown. |
|
|
67
|
+
| `shutdownTimeout` | `number` | `10000` | Time (ms) to wait for workers to shutdown before forced exit. |
|
|
65
68
|
|
|
66
69
|
## Working with @ynode/autoshutdown
|
|
67
70
|
|
package/package.json
CHANGED
package/src/cluster.js
CHANGED
|
@@ -67,6 +67,9 @@ export function run(startWorker, options = true, log = console) {
|
|
|
67
67
|
mode = "smart", // 'smart' or 'max'
|
|
68
68
|
scalingCooldown = 10000,
|
|
69
69
|
scaleDownGrace = 30000,
|
|
70
|
+
autoScaleInterval = 5000,
|
|
71
|
+
shutdownSignals = ["SIGINT", "SIGTERM", "SIGQUIT"],
|
|
72
|
+
shutdownTimeout = 10000,
|
|
70
73
|
} = typeof options === "object" ? options : {};
|
|
71
74
|
|
|
72
75
|
if (minWorkers > maxWorkers) {
|
|
@@ -208,34 +211,30 @@ export function run(startWorker, options = true, log = console) {
|
|
|
208
211
|
|
|
209
212
|
return;
|
|
210
213
|
}
|
|
211
|
-
|
|
214
|
+
return;
|
|
215
|
+
}, autoScaleInterval).unref();
|
|
212
216
|
}
|
|
213
217
|
|
|
214
218
|
// Graceful shutdown handling for Master
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
219
|
+
if (Array.isArray(shutdownSignals) && shutdownSignals.length > 0) {
|
|
220
|
+
shutdownSignals.forEach((signal) => {
|
|
221
|
+
process.on(signal, () => {
|
|
222
|
+
log.info(`Master received ${signal}, shutting down workers...`);
|
|
223
|
+
isShuttingDown = true;
|
|
224
|
+
for (const worker of Object.values(cluster.workers)) {
|
|
225
|
+
if (worker && worker.isConnected()) {
|
|
226
|
+
worker.send("shutdown");
|
|
227
|
+
}
|
|
224
228
|
}
|
|
225
|
-
}
|
|
226
229
|
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
process.exit(0);
|
|
236
|
-
}, 10000).unref();
|
|
230
|
+
// Allow some time for workers to clean up
|
|
231
|
+
if (shutdownTimeout > 0) {
|
|
232
|
+
setTimeout(() => {
|
|
233
|
+
log.warn(`Master force exiting after ${shutdownTimeout}s timeout.`);
|
|
234
|
+
process.exit(0);
|
|
235
|
+
}, shutdownTimeout * 1000).unref();
|
|
236
|
+
}
|
|
237
|
+
});
|
|
237
238
|
});
|
|
238
|
-
}
|
|
239
|
-
}
|
|
240
|
-
|
|
241
|
-
|
|
239
|
+
}
|
|
240
|
+
}
|