0
我正在使用PM2來管理基於Node.js的微服務平臺。我們想要從我們可以看到微服務狀態的儀表板如果有任何服務需要太多的CPU或內存,並且爲此我使用了PM2的api並編寫了以下代碼片段。pm2錯誤:EMFILE,打開的文件太多
function getMicroService(){
pm2.connect(function(err) {
if(!err){
// Get all processes running
logger.info('core_module','Connecting to PM2 Daemon for Micro Services List');
var dataArr = {};
var microServices = [];
var counter = 0;
var curDateTime = helperLib.getDateTimeISO();
pm2.list(function(err, process_list) {
if(process_list.length > 0){
process_list.forEach(function(process){
delete process.pm2_env;
process.lastChecked = curDateTime;
microServices.push(process);
counter++;
})
}
if(counter == process_list.length){
dataArr.event = 'microServices';
dataArr.data = microServices;
publishStats(dataArr);
}
});
}else{
logger.error('core_module','on Line 245: '+err)
}
})
}
上面的函數每15秒調用一次,它在儀表板上顯示數據。但是我注意到這項服務開始佔用100%以上的CPU,並且PM2整個守護進程服務脫機並停止響應。無法發出任何命令,例如pm2停止所有等我必須手動殺死進程,然後再次啓動服務。我從日誌文件中提取的錯誤是
{"message":"core_module Threw Exception: ","stack":"Error: EMFILE: too many open files, open '/root/.pm2/pm2.log'\n at Object.fs.openSync (fs.js:584:18)\n at module.exports.Client.launchDaemon (/etc/node/node_modules/pm2/lib/Client.js:207:14)\n at /etc/node/node_modules/pm2/lib/Client.js:102:10\n at /etc/node/node_modules/pm2/lib/Client.js:294:14\n at _combinedTickCallback (internal/process/next_tick.js:73:7)\n at process._tickDomainCallback (internal/process/next_tick.js:128:9)","errno":-24,"code":"EMFILE","syscall":"open","path":"/root/.pm2/pm2.log","__error_callsites":[{},{},{},{},{},{}],"level":"error","timestamp":"2017-10-20T00:49:26.826Z"}
任何人都可以請幫忙,如果上面的代碼是正確的。每15秒調用一次是一種好方法,或者我可以如何優化它。我應該在函數結尾調用pm2.disconnect()。
請指教。
問候 哈比卜