当客户端加载时,Meteor应用程序停留在100%CPU - 如何解决?

时间:2015-05-26 11:28:02

标签: meteor

当我在客户端加载应用程序的某些页面时,我的meteor应用程序已经在服务器上达到100%cpu。它会导致应用程序挂起其他客户端。我该如何解决?

我怀疑我可以通过更有效地发布或订阅这些特定页面来提高性能。但是,我不确定是否在发布函数中进行更多计算以减少发送给客户端的字段有助于或受到伤害。当发送的数据大小减小时,发送数据之前的计算数量会增加。我应该优化更多的计算,更少的数据或更少的计算和更多的数据?具体来说,我用什么标准来决定?

我的发布功能:

Meteor.publish('userdata', function(id) {
  if (!this.userId) return [];

  var user = Meteor.users.findOne(this.userId);
  if (!user) return [];

  if (isStaff(this.userId)) user = Meteor.users.findOne(id);

  var tasks;
  var emails = [];
  var network = user.users ? [user.users] : [];
  var meals;
  if (isStaff(this.userId) && this.userId === id) { //staff looking at own profile
    tasks = Tasks.find({users: this.userId, status: "active"}, {sort: { taskName: 1 } });
    emails = Emails.find({staffId: id});
    meals = Meals.find({userId: this.userId});
  }
  else { //staff looking at other profiles
    meals = Meals.find({userId: id});

    var completedTasks = [];
    if (user.userTaskStatus) {
      completedTasks = user.userTaskStatus.map(function (thisTaskStatus) { 
        if (thisTaskStatus.status !== "incomplete") return thisTaskStatus.taskId; 
      });
    }

    var allUserTasks = Tasks.find({users: user._id});
    var showTaskIds = [];

    allUserTasks.forEach(function (thisTask) {
      if (!thisTask.prereqs) showTaskIds.push(thisTask._id); 
      else {
        var prereqs = thisTask.prereqs;
        var pushTaskFlag = true;
        for(var i=0; i<prereqs.length; i++)
          if (completedTasks.indexOf(prereqs[i]) < 0) {
            pushTaskFlag = false;
            break;
          }
        if (pushTaskFlag) showTaskIds.push(thisTask._id);      
      }
    });

    tasks = Tasks.find({_id: {$in: showTaskIds}, visible: true, status: "active"}, {sort: { taskName: 1 } });

    var network = user.users ? user.users.push(id) : [id];

    if (isStaff(this.userId)) emails = Emails.find({userId: {$in: network}}, {sort: {date: -1}});

  }

  network.push(user._id);

  var calls = Calls.find({$or: [{to:user.phone},{from:user.phone}] });
  var callStaffIds = calls.map(function (thisCall) {
    return thisCall.staff;
  });
  var callNoteIds = calls.map(function (thisCall) {
    return thisCall.noteId;
  });

  var notes = Notes.find({
    $or:[
      {userId: this.userId},
      {noteId: {$in: callNoteIds}}
    ]
  });
  var noteStaffIds = notes.map(function (thisNote) {
    return thisNote.staff;
  });

  var allUserIds = network.concat(callStaffIds, noteStaffIds);

  var groups = [];
  if (user.groups) groups = user.groups;

  return [
    Meteor.users.find({_id: {$in: allUserIds}}, {fields: {services: 0}}), 
    tasks,
    Groups.find({_id: {$in: groups}}, {sort: { groupName: 1 } }),
    emails,
    calls,
    notes,
    meals
  ];
});

0 个答案:

没有答案