feat: fork worker in dev mode

This commit is contained in:
karishmas6
2024-10-30 22:05:16 +05:30
parent 17b6503297
commit 80238fbf5e

View File

@@ -63,20 +63,20 @@ readdirSync(path.join(__dirname, 'api')).forEach((r) => {
}); });
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');
let workerProcess; let workerProcess: any;
if (!isProduction) { if (!isProduction) {
workerProcess = fork(workerPath, [], { workerProcess = fork(workerPath, [], {
execArgv: ['--inspect=5859'], execArgv: ['--inspect=5859'],
}); });
workerProcess.on('message', (message) => { workerProcess.on('message', (message: any) => {
console.log(`Message from worker: ${message}`); console.log(`Message from worker: ${message}`);
}); });
workerProcess.on('error', (error) => { workerProcess.on('error', (error: any) => {
console.error(`Error in worker: ${error}`); console.error(`Error in worker: ${error}`);
}); });
workerProcess.on('exit', (code) => { workerProcess.on('exit', (code: any) => {
console.log(`Worker exited with code: ${code}`); console.log(`Worker exited with code: ${code}`);
}); });
} }
@@ -91,13 +91,21 @@ app.get('/', function (req, res) {
}); });
server.listen(SERVER_PORT, async () => { server.listen(SERVER_PORT, async () => {
await connectDB(); try {
await syncDB(); await connectDB();
logger.log('info', `Server listening on port ${SERVER_PORT}`); await syncDB();
logger.log('info', `Server listening on port ${SERVER_PORT}`);
} catch (error: any) {
logger.log('error', `Failed to connect to the database: ${error.message}`);
process.exit(1); // Exit the process if DB connection fails
}
}); });
process.on('SIGINT', () => { process.on('SIGINT', () => {
console.log('Main app shutting down...'); console.log('Main app shutting down...');
//workerProcess.kill(); if (!isProduction) {
workerProcess.kill();
}
process.exit(); process.exit();
}); });