-
Notifications
You must be signed in to change notification settings - Fork 29
/
index.js
36 lines (31 loc) · 1017 Bytes
/
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
36
const start = async () => {
// Set the desired es version for downstream modules that support it
(await import('@balena/es-version')).set('es2022');
// Support import of *.cts files
(await import('ts-node')).register({ transpileOnly: true });
await import('./init.js');
};
const numWorkers =
parseInt(process.env.NUM_WORKERS ?? '0', 10) ||
(await import('os')).cpus().length;
if (numWorkers > 1) {
const { default: cluster } = await import('cluster');
if (cluster.isPrimary) {
// Setup the RateLimiterCluster store on the master worker
const { RateLimiterClusterMaster } = await import('rate-limiter-flexible');
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);
});
} else {
await start();
}
} else {
await start();
}