我需要从主节点应用程序派生/派生进程。此过程应“无休止地”运行,因为它们检查文件系统中不同文件夹中的更改并处理新文件。
我通过以下方式分叉进程:
var processes = [];
var fork = require('child_process').fork;
var processList = [
{'servicename':'faxservice1'},
{'servicename':'faxservice2'},
{'servicename':'faxservice3'}
];
timeOutValue = 10000;
processList.forEach(Service => {
var cp = fork(['./modules/services/inbound/fax/service.js']);
cp.on('close', function() {
console.log('child process has ended');
process.exit();
})
cp.on('message', (m) => {
console.log(`SVC | ${m}`);
});
setTimeout(function() {
cp.send('stop');
}, timeOutValue);
// Push spawned process to process-array
processes.push(cp);
timeOutValue = timeOutValue + 5000;
});
子进程执行以下操作:
const svcName = 'FAX-INBOUND';
var chokidar = require('chokidar');
var watcher = chokidar.watch('file, dir, or glob', {
ignored: /[\/\\]\./, persistent: true
});
var log = console.log.bind(console);
var args = process.argv.slice(2);
console.log(`${svcName} | ${args} | starting`);
process.on('message', (m) => {
console.log(`${svcName} | ${m} | Service termination command recieved from host`);
process.exit();
});
watcher
.on('add', function(path) { log(svcName + " | " + 'File', path, 'has been added'); })
.on('addDir', function(path) { log(svcName + " | " + 'Directory', path, 'has been added'); })
.on('change', function(path) { log(svcName + " | " + 'File', path, 'has been changed'); })
.on('unlink', function(path) { log(svcName + " | " + 'File', path, 'has been removed'); })
.on('unlinkDir', function(path) { log(svcName + " | " + 'Directory', path, 'has been removed'); })
.on('error', function(error) { log(svcName + " | " + 'Error happened', error); })
.on('ready', function() { log(svcName + " | " + 'Initial scan complete. Ready for changes.'); })
.on('raw', function(event, path, details) { log(svcName + " | " + 'Raw event info:', event, path, details); })
如果我运行显示的代码,我会在日志中看到三个进程分叉了:
FAX-INBOUND | | starting
FAX-INBOUND | | starting
FAX-INBOUND | Initial scan complete. Ready for changes.
FAX-INBOUND | Initial scan complete. Ready for changes.
FAX-INBOUND | | starting
FAX-INBOUND | Initial scan complete. Ready for changes.
setTimeout 仅用于调试目的,并且应将消息分别发送到三个分叉的进程,每个进程有5秒的延迟。产生的进程重新审视消息后,它们应该退出。
BUT 在控制台上,我仅看到一个分叉过程的控制台输出:
FAX-INBOUND | stop | Service termination command recieved from host
如果一个分叉的流程调用
process.exit()
所有分叉的进程都被杀死了吗?
我在做什么错了?
致谢