feat: fork worker only in dev mode

This commit is contained in:
karishmas6
2024-10-30 21:12:51 +05:30
parent 7689a0b159
commit 4d2f0ed5bf

View File

@@ -62,24 +62,24 @@ readdirSync(path.join(__dirname, 'api')).forEach((r) => {
} }
}); });
// Check if we're running in production or development
const isProduction = process.env.NODE_ENV === 'production'; const isProduction = process.env.NODE_ENV === 'production';
const workerPath = path.resolve(__dirname, isProduction ? './worker.js' : '/worker.ts'); const workerPath = path.resolve(__dirname, isProduction ? './worker.js' : '/worker.ts');
// Fork the worker process let workerProcess;
const workerProcess = fork(workerPath, [], { if (!isProduction) {
execArgv: isProduction ? ['--inspect=8081'] : ['--inspect=5859'], workerProcess = fork(workerPath, [], {
}); execArgv: ['--inspect=5859'],
});
workerProcess.on('message', (message) => { workerProcess.on('message', (message) => {
console.log(`Message from worker: ${message}`); console.log(`Message from worker: ${message}`);
}); });
workerProcess.on('error', (error) => { workerProcess.on('error', (error) => {
console.error(`Error in worker: ${error}`); console.error(`Error in worker: ${error}`);
}); });
workerProcess.on('exit', (code) => { workerProcess.on('exit', (code) => {
console.log(`Worker exited with code: ${code}`); console.log(`Worker exited with code: ${code}`);
}); });
}
app.get('/', function (req, res) { app.get('/', function (req, res) {
capture( capture(
@@ -98,6 +98,6 @@ server.listen(SERVER_PORT, async () => {
process.on('SIGINT', () => { process.on('SIGINT', () => {
console.log('Main app shutting down...'); console.log('Main app shutting down...');
workerProcess.kill(); //workerProcess.kill();
process.exit(); process.exit();
}); });