statusManager.js 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. var utils = require('../util/utils');
  2. var redis = require('redis');
  3. var logger = require('../../../pomelo-logger').getLogger('pomelo', __filename);
  4. var DEFAULT_PREFIX = 'COS';
  5. var DEFAULT_STATUSPRE = 'STATUS';
  6. var StatusManager = function(app, opts) {
  7. this.app = app;
  8. this.opts = opts || {};
  9. this.prefix = opts.prefix || DEFAULT_PREFIX;
  10. this.statusPre = opts.statusPre || DEFAULT_STATUSPRE;
  11. this.host = opts.host;
  12. this.port = opts.port;
  13. this.redis = null;
  14. };
  15. module.exports = StatusManager;
  16. StatusManager.prototype.start = function(cb) {
  17. var app = this.app || {};
  18. var serverId = app.getServerId();
  19. logger.info("[status-plugin][redis] start", this.opts);
  20. this.redis = redis.createClient(this.port, this.host, this.opts);
  21. if (this.opts.auth_pass) {
  22. this.redis.auth(this.opts.auth_pass);
  23. }
  24. this.redis.on("error", function (err) {
  25. logger.error("[status-plugin][redis] error" , err.stack, serverId);
  26. });
  27. this.redis.once('ready', function () {
  28. logger.info("[status-plugin][redis] ready", serverId);
  29. // utils.invokeCallback(cb);
  30. });
  31. utils.invokeCallback(cb);
  32. };
  33. StatusManager.prototype.stop = function(force, cb) {
  34. if(this.redis) {
  35. this.redis.end();
  36. this.redis = null;
  37. }
  38. utils.invokeCallback(cb);
  39. };
  40. StatusManager.prototype.clean = function(cb) {
  41. this.redis.flushdb(cb);
  42. };
  43. StatusManager.prototype.add = function(uid, sid ,cb) {
  44. this.redis.sadd(genKey(this, uid), sid, function(err) {
  45. utils.invokeCallback(cb, err);
  46. });
  47. };
  48. StatusManager.prototype.leave = function(uid, sid, cb) {
  49. this.redis.srem(genKey(this, uid), sid, function(err) {
  50. utils.invokeCallback(cb, err);
  51. });
  52. };
  53. StatusManager.prototype.getSidsByUid = function(uid, cb) {
  54. this.redis.smembers(genKey(this, uid), function(err, list) {
  55. utils.invokeCallback(cb, err, list);
  56. });
  57. };
  58. StatusManager.prototype.getSidsByUids = function(uids, cb) {
  59. var cmds = [];
  60. for (var i=0; i<uids.length; i++) {
  61. cmds.push(['exists', genKey(this, uids[i])]);
  62. }
  63. execMultiCommands(this.redis, cmds, function(err, list) {
  64. utils.invokeCallback(cb, err, list);
  65. });
  66. };
  67. var execMultiCommands = function(redis, cmds, cb) {
  68. if(!cmds.length) {
  69. utils.invokeCallback(cb);
  70. return;
  71. }
  72. redis.multi(cmds).exec(function(err, replies) {
  73. utils.invokeCallback(cb, err, replies);
  74. });
  75. };
  76. var genKey = function(self, uid) {
  77. return ':'+self.statusPre + ':' + uid;
  78. };
  79. var genCleanKey = function(self) {
  80. return '*';
  81. };