forked from balena-io/open-balena-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
35 lines (29 loc) · 1.02 KB
/
index.js
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
const numWorkers = process.env.NUM_WORKERS || require('os').cpus().length;
if (numWorkers > 1) {
const cluster = require('cluster');
if (cluster.isPrimary) {
// Setup the RateLimiterCluster store on the master worker
const { RateLimiterClusterMaster } = require('rate-limiter-flexible');
// tslint:disable-next-line:no-unused-expression-chai
new RateLimiterClusterMaster();
console.log(`Forking ${numWorkers} workers`);
for (let i = 0; i < numWorkers; i++) {
console.log(`Forking worker ${i}`);
cluster.fork(process.env);
}
cluster.on('exit', (worker) => {
console.log('Worker ' + worker.id + ' died, replacing it');
cluster.fork(process.env);
});
return;
}
}
// Use fast-boot to cache require lookups, speeding up startup
require('fast-boot2').start({
cacheFile: '.fast-boot.json',
});
// Set the desired es version for downstream modules that support it
require('@balena/es-version').set('es2022');
// Support `require()` of *.ts files
require('ts-node/register/transpile-only');
require('./init.ts');