-
Notifications
You must be signed in to change notification settings - Fork 195
/
index.js
56 lines (42 loc) · 1.63 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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
const os = require('node:os');
const cluster = require('node:cluster');
const connectDB = require('./src/db/mongoose');
const { log, logError } = require('./src/helpers/logger');
const { handleClusterExit, handleClusterMessage, logCounts } = require('./src/utils/cluster');
const { validateEnvVar } = require('./src/utils/util');
const { setupCRONJobs } = require('./src/utils/cron-jobs');
const { version } = require('./package.json');
const { PORT = 8888, NODE_ENV } = process.env;
const numCPUs = os.cpus().length;
async function setupMasterProcess() {
try {
validateEnvVar();
await connectDB();
setupCRONJobs();
logCounts();
log(`[Master] ${process.pid} running with 4/${numCPUs} workers`);
log(`[Master][${NODE_ENV}] App v${version} running at http://localhost:${PORT}`);
forkWorkers(4);
} catch (error) {
logError(`[Master] Critical error: ${error.message}`, { error: error.stack });
process.exit(1);
}
}
function forkWorkers(numWorkers) {
for (let i = 0; i < numWorkers; i++) cluster.fork();
cluster.on('exit', (worker, code, signal) => handleClusterExit(worker, code, signal));
cluster.on('message', (worker, message) => handleClusterMessage(worker, message));
}
// Main execution block
if (cluster.isMaster) {
setupMasterProcess();
} else {
require('./worker');
process.on('uncaughtException', err => {
logError(`[Worker] Error in worker ${process.pid}: ${err.message}`, { error: err.stack });
// Send the full stack trace to the master process
process.send({ type: 'error', error: err.stack });
// After handling the error, let it die naturally
process.exit(1);
});
}