gpt4free-ts/index.ts

48 行
1.4 KiB
TypeScript
Raw 通常表示 履歴

2023-09-10 23:26:06 +09:00
import { initLog } from './utils/log';
2023-05-11 20:32:10 +09:00
2024-02-27 22:52:08 +09:00
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';
2023-06-27 15:21:05 +09:00
2024-02-27 22:52:08 +09:00
process.setMaxListeners(1000); // 将限制提高到20个
2023-09-10 23:26:06 +09:00
initLog();
Config.load();
Config.watchFile();
2024-02-27 22:52:08 +09:00
initCache();
2023-05-04 22:32:21 +09:00
2024-02-27 22:52:08 +09:00
if (cluster.isPrimary) {
console.log(`Master ${process.pid} is running`);
const workers = +(process.env.WORKERS || 1);
2023-05-04 22:32:21 +09:00
2024-02-27 22:52:08 +09:00
// Fork workers.
for (let i = 0; i < workers; i++) {
cluster.fork();
2023-09-10 23:26:06 +09:00
}
2023-07-02 13:08:14 +09:00
2024-02-27 22:52:08 +09:00
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();
2023-07-03 12:49:40 +09:00
}
2024-02-27 22:52:08 +09:00
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
2023-07-03 12:49:40 +09:00
});