slackManager.js 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. const Slack = require('./slack.js').Slack
  2. ,INACTIVE_DELAY = 120000
  3. ,PING_DELAY = 600000
  4. ;
  5. function SlackManager() {
  6. this.cache = require('memory-cache');
  7. }
  8. var instance = new SlackManager();
  9. setInterval(function() {
  10. var keys = instance.cache.keys();
  11. keys.forEach(function(instKey) {
  12. instance.cache.get(instKey).ping();
  13. });
  14. }, PING_DELAY);
  15. SlackManager.prototype.getScope = function() {
  16. return [
  17. "client"
  18. ];
  19. };
  20. SlackManager.prototype.lazyGet = function(sess, reqT) {
  21. var key = "SLACK_" +sess.sessId
  22. ,val = this.cache.get(key);
  23. if (val) {
  24. this.cache.put(key, val, INACTIVE_DELAY, function(key, val) {
  25. instance.removed(key, val);
  26. });
  27. } else {
  28. val = new Slack(sess, this);
  29. this.cache.put(key, val, INACTIVE_DELAY, function(key, val) {
  30. instance.removed(key, val);
  31. val.close();
  32. });
  33. }
  34. return val;
  35. };
  36. SlackManager.prototype.removed = function(key, slack) {
  37. slack.close();
  38. };
  39. SlackManager.prototype.suicide = function(slack) {
  40. this.cache.del("SLACK_" +slack.sessId);
  41. };
  42. module.exports.SlackManager = instance;