3
\$\begingroup\$

Here is my scenario:

  • Every user checks in every five minutes.
  • If some user doesn't check in after 5 minutes, I should be able to keep track of it.
  • If some user doesn't check in N number of times, an alert should be generated , like send email or SMS.

Here is what I have implemented so far:

var Report = function(attendence) {
  this.attendence = attendence;
  EventEmitter.call(this);
};

util.inherits(Report, EventEmitter);

Report.prototype.addAPSStatsData = function() {

  var attendenceDB = new Report(this.attendence);

  //Promise 
  var prSave = attendenceDB.save();
  var self = this;
  prSave.then(function(data) {

    //emitting event that data has been saved 
    self.emit('attendenceRecieved', data.userName);

    var saveTime = Date.now();
    self.moniterAPSStatsData(data.userName, saveTime);

  }).catch(function(err) {
    self.emit('error', err);
  });
};

Report.prototype.moniterAPSStatsData = function(userName, time) {

  var cacheKey = appConfig.CACHE_KEY_UserTimeInterval + userName;
  var prevTimer = memoryCache.get(cacheKey);

  if(!prevTimer) 
    clearTimeout(prevTimer);

  var timer = setTimeout(this.moniterCallBackAfterInterval, (5 * 60000), userName, time ,cacheKey);

  //Putting timout in cache so later on it can be clearedout if the new attendence comes in 
  memoryCache.set(cacheKey, timer);

};

Report.prototype.moniterCallBackAfterInterval = function(userName, time ,cacheKey) {
  //Removing from cache
  memoryCache.del(cacheKey);

  //Emitting event so email can be send out
  this.emit('ReportNotRecieved_SENDEMAIL');
};
  • Do you think this is a good approach?
  • Will it hold in a production environment?
  • Are there any alternatives to setTimeOut which can be used here?
\$\endgroup\$
4
  • 2
    \$\begingroup\$ I would actually recommend doing this in redis instead of in a node program. \$\endgroup\$ Commented Jan 11, 2016 at 14:34
  • \$\begingroup\$ Sorry if my question seems annoying but can you please elaborate on this. I am totally new to realms of JS and from what I gathered about REDIS , its in memory storage thingi. \$\endgroup\$ Commented Jan 11, 2016 at 16:20
  • \$\begingroup\$ What do you mean from "every one checks"? Every server running this code? What are they "missing"? It looks to me like you're trying to invalidate a key after a set interval. As Dan mentioned you're probably better off having Redis handle this. See the expire command. \$\endgroup\$ Commented Jan 11, 2016 at 17:22
  • \$\begingroup\$ I have updated my question. The main purpose is to send out an email if some one doesn't report in given time. Using cahce to keep track of setTimeout. \$\endgroup\$ Commented Jan 11, 2016 at 18:48

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.