-
-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathcluster.js
More file actions
44 lines (40 loc) · 1.61 KB
/
Copy pathcluster.js
File metadata and controls
44 lines (40 loc) · 1.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
const cluster = require('cluster');
if (cluster.isPrimary) {
const requested = parseInt(process.env.WEB_CONCURRENCY, 10);
const workers = Number.isInteger(requested) && requested > 0 ? requested : 4;
console.log(`Primary ${process.pid} starting ${workers} workers`);
for (let i = 0; i < workers; i++) fork();
// resurrect a worker if it dies, unless we're shutting down, also track fast crashes and exit if too many happen in a row
let shuttingDown = false;
let fastCrashes = 0;
cluster.on('exit', (worker, code, signal) => {
if (shuttingDown) return;
const ranForMs = Date.now() - worker.startedAt;
if (ranForMs < 5000) {
fastCrashes++;
console.error(`Worker ${worker.process.pid} died after ${ranForMs}ms (${signal || code}) [fast-crash ${fastCrashes}/${workers}]`);
if (fastCrashes >= workers) {
console.error('Too many fast worker crashes; exiting for the service manager to restart.');
process.exit(1);
}
} else {
fastCrashes = 0; // a healthy run clears the streak
console.log(`Worker ${worker.process.pid} died after ${Math.round(ranForMs / 1000)}s (${signal || code}); respawning`);
}
fork();
});
for (const sig of ['SIGTERM', 'SIGINT']) {
process.on(sig, () => {
shuttingDown = true;
for (const w of Object.values(cluster.workers)) w.kill(sig);
});
}
} else {
// equivalent to `probot run ./index.js` (app path passed as a positional arg)
require('probot').run([process.argv[0], process.argv[1], './index.js']);
}
function fork() {
const worker = cluster.fork();
worker.startedAt = Date.now();
return worker;
}