I have reqirement where I need to execute some code written in different js file. I am using fork method in child_process module. There will be upto 1000 request to execute that file code in single API call. With that much request it becomes important to kill that process that is executing the child file.
My queries are:
- Are the child process generated from fork method kill themselves after executing code or do we have to kill them explicitly(if yes, how can we achieve that)
There are many question and answers are available in forking and childprocess but none are available for NodeJS, I couldn't get any help from following those answers.
With my limited knowledge I wrote the sample code and tried to kill the process running in child node. That works fine for 1st API request, but when 2nd API request comes. Node applicatin collapses.
My Codes:
master.js
const fork = require('child_process').fork;
const cp1 = fork('./child1.js');
const app = require('express')();
function timer(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
app.get('/forktesting',(req, res)=>{
let num = [45, 5, 48, 91, 74, 3, 82];
try {
for (let index = 0; index < num.length; index++) {
await timer(1000);
cp1.send('test');
}
cp1.on('message', (msg)=>{
console.log(`recieving message code in parent process: ${msg.val}`);
});
cp1.on('exit',(msg=>{
console.log(`recieving exit code in parent process: ${msg}`);
}));
console.log('last line of API call');
} catch (error) {
console.log({'error' : error});
res.send({"code": 500, "status":"error"});
}
});
app.listen(8081, ()=>{ console.log(`listening to 8081`);})
child1.js
process.on('message', async(message)=>{
console.log(`in message child: ${message}`);
setTimeout(() => {
process.send({ val: "from child"});
console.log(`child pid: ${process.pid}`);
process.kill(process.pid);
}, 5000);
})
Response in 1st API call which is working fine
Note: Showing correct implementation would be very helpful
In the second file(child1.js), I am calling differen system and waiting the response from them, upon receving the response I will send it to the parent process.
Edit2: Added for loop around cp1.send() to show that with single API call there will be multiple forking of the child process and added try catch block. I have added for loop for forking as required by the actual scenario. so same file will be forked many times, I have shown 3 items in array but that would be 1000 later, which would be fetced from DB. The scenario is: with every API call, I have to fork multiple times, and that API would be frequented by users, so multiple users will be accessing it. With Every API hit, the listeners will be added, so single response will be heard my many listerners, that would also create problem after some threshold of listeners count will cross. So, implementation of event.once would be good, but I want the child response count also so probably have to go with on listener to listen response of every forked child.
Edit1: I removed the process.kill(process.pid)
in child process and hit twice within the 5 secs. I am getting twice response, that's not the functionality I desire. Below picture showing the console output.
I should get only one response only from that forked process. With the remove process.kill(process.pid), every call to this API is increasing the response count.
Photos shows logs for original question and edit 1