-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
index.ts
47 lines (42 loc) · 1.45 KB
/
index.ts
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
45
46
47
import { initLog } from './utils/log';
require('dotenv').config();
require('elastic-apm-node').start({
serverUrl: process.env['apm.serverUrl'],
serviceName: process.env['apm.serviceName'],
environment: process.env['apm.environment'],
transactionSampleRate: parseInt(
process.env['apm.transactionSampleRate'] || '1',
),
});
import 'heapdump';
import cluster from 'node:cluster';
import { Config } from './utils/config';
import { initCache } from './utils/cache';
process.setMaxListeners(1000); // 将限制提高到20个
initLog();
Config.load();
Config.watchFile();
initCache();
if (cluster.isPrimary) {
console.log(`Master ${process.pid} is running`);
const workers = +(process.env.WORKERS || 1);
// Fork workers.
for (let i = 0; i < workers; i++) {
cluster.fork();
}
cluster.on('exit', (worker, code, signal) => {
console.log(`worker ${worker.process.pid} died,sig: ${signal}`);
console.log('Forking a new process...');
cluster.fork(); // Fork a new process if a worker dies
});
} else {
require('./router').registerApp();
}
process.on('uncaughtException', (err) => {
console.error('Uncaught exception:', err);
setTimeout(() => process.exit(1), 5000); // It's up to you whether to exit here or not
});
process.on('unhandledRejection', (reason, promise) => {
console.error('Unhandled rejection at ', promise, `reason: ${reason}`);
setTimeout(() => process.exit(1), 5000); // It's up to you whether to exit here or not
});