slackManager.js 1.3 KB

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