Javascript 识别node.js导致处理大量数据的cpu使用率达到100%
My node.js在foreach循环中处理大量数据,在mongo中执行读写操作。它需要100%的cpu利用率和大约1 gb的ram。有什么建议可以改进吗 请帮忙,谢谢 代码:Javascript 识别node.js导致处理大量数据的cpu使用率达到100%,javascript,node.js,mongodb,mongoose,kue,Javascript,Node.js,Mongodb,Mongoose,Kue,My node.js在foreach循环中处理大量数据,在mongo中执行读写操作。它需要100%的cpu利用率和大约1 gb的ram。有什么建议可以改进吗 请帮忙,谢谢 代码: worker.process('bell',(作业,完成)=>{ Logger.info('处理'+job.type+'id'+job.id'类型的作业); 让data=job.data; Logger.info(JSON.stringify(data)); 让状态; if(data.users&&Array.isAr
worker.process('bell',(作业,完成)=>{
Logger.info('处理'+job.type+'id'+job.id'类型的作业);
让data=job.data;
Logger.info(JSON.stringify(data));
让状态;
if(data.users&&Array.isArray(data.users)){
if(data.users.length){
doc.states=状态;
doc.modified_at=new Date().getTime();
doc.save();
}).catch(记录器错误);
data.users.forEach((用户)=>{
UserService.findOne({
id:用户
})。然后((doc)=>{
if(doc&!doc.bell_notifications.includes(data.notifications.id)){
doc.badge++;
如果(doc.bell_.length==20){
doc.bell_notifications.pop();
log('checkingpop');
}
doc.bell_notifications.unshift(数据通知id);
//Logger.info('向'+用户发送通知');
console.log(“向用户发送通知”);
io.io.to(用户).emit('newNotificationCount',doc.badge');
doc.save();
}否则{
//Logger.info(新错误(“未找到用户:“+User”);
}
}).catch(console.error)
});
完成();
状态=‘成功’;
}否则{
状态='失败';
完成(新错误('chrome作业中不存在通知数据'))
}
NotificationService.findById(data.notifications.\u id)//更改通知状态
。然后((doc)=>{
doc.states=状态;
doc.modified_at=new Date().getTime();
doc.save();
}).catch(err=>Logger.error(新错误(err)))
});
在将数据写入mongo之前,是否将其加载到内存中?写入后是否清除保留的对象(您是否需要在节点中..?)?太宽了,如果需要帮助,请添加foreach循环的代码是否在将数据写入mongo之前将其加载到内存中?您是否在写入后清除保留的对象(您甚至需要在节点..中清除吗?)?它太宽了,如果需要帮助,请添加foreach循环的代码
worker.process('bell', (job, done) => {
Logger.info('Processing job of type ' + job.type + ' id ' + job.id);
let data = job.data;
Logger.info(JSON.stringify(data));
let status;
if (data.users && Array.isArray(data.users)) {
if (data.users.length <= 0) {
Logger.error(new Error('No User List is provided For Notification: '));
done(new Error('No User List is provided For Notification: '));
}
status = 'active';
NotificationService.findById(data.notifications._id) // change state of notification
.then((doc) => {
doc.states = status;
doc.modified_at = new Date().getTime();
doc.save();
}).catch(Logger.error);
data.users.forEach((user) => {
UserService.findOne({
id: user
}).then((doc) => {
if (doc && !doc.bell_notifications.includes(data.notifications._id)) {
doc.badge++;
if (doc.bell_notifications.length == 20) {
doc.bell_notifications.pop();
console.log('checking pop');
}
doc.bell_notifications.unshift(data.notifications._id);
// Logger.info('Sending Notification to ' + user);
console.log('Sending notification to ', user);
io.io.to(user).emit('newNotificationCount', doc.badge);
doc.save();
}else{
// Logger.info(new Error("User Not found : " + user));
}
}).catch(console.error)
});
done();
status = 'success';
} else {
status = 'failed';
done(new Error('notification data is not present in chrome job'))
}
NotificationService.findById(data.notifications._id) // change state of notification
.then((doc) => {
doc.states = status;
doc.modified_at = new Date().getTime();
doc.save();
}).catch(err => Logger.error(new Error(err)))
});