I have main APP and child JS. I want to run child JS in child Process this child.js is an schedule loop to check every hour on db.
How to do it?
I will post the Quick Code
Child.js
exports.main = () => {
console.log('Running API DAEMON')
updateInventoryInit();
}
function updateInventoryInit(){ //something happen };
Main_APP.js
var api_DAEMON = require('./api_daemon');
api_DAEMON.main();
答案 0 :(得分:0)
据我所知,您可以尝试以下代码:
<强> main.js 强>
const cp = require('child_process');
let child = cp.fork(__dirname + "/child.js");
child.on('message', (msg) => {
console.log("from child : ", msg);
});
setInterval( () => {
// sent msg to child in every hour
child.send( 'check-db' );
}, 'every-hour');
<强> child.js 强>
// receive msg
process.on("message", (data) => {
// perform your task
// after complete task
process.send("complete");
});