|
6 | 6 | * @authro Zongmin Lei <leizongmin@gmail.com> |
7 | 7 | */ |
8 | 8 |
|
| 9 | +const EventEmitter = require('events').EventEmitter; |
| 10 | +const leiPromise = require('lei-promise'); |
| 11 | +const utils = require('./utils'); |
| 12 | +const debug = utils.debug('producer'); |
| 13 | + |
| 14 | +class Producer { |
| 15 | + |
| 16 | + /** |
| 17 | + * Constructor |
| 18 | + * |
| 19 | + * @param {Object} options |
| 20 | + * - {String} queue |
| 21 | + * - {Number} maxAge |
| 22 | + * - {Object} redis |
| 23 | + * - {String} host |
| 24 | + * - {Number} port |
| 25 | + * - {Number} db |
| 26 | + * - {String} prefix |
| 27 | + */ |
| 28 | + constructor(options) { |
| 29 | + |
| 30 | + if (!options.queue) throw new Error('missing queue name'); |
| 31 | + |
| 32 | + this._redis = utils.createRedisClient(options.redis); |
| 33 | + this.name = utils.generateClientId('producer'); |
| 34 | + this._maxAge = options.maxAge || 0; |
| 35 | + this._redisPrefix = (options.redis && options.redis.prefix) || ''; |
| 36 | + this.queue = options.queue; |
| 37 | + this._queueKey = this._redisPrefix + options.queue; |
| 38 | + |
| 39 | + this._msgId = 0; |
| 40 | + this._msgCallback = new EventEmitter(); |
| 41 | + const msgCallback = (id, err, result) => this._msgCallback.emit(id, err, result); |
| 42 | + |
| 43 | + this._redis.subscribe(`${this._redisPrefix}CB:${this.name}`, (chennel, message) => { |
| 44 | + |
| 45 | + // msg: success => id,s,data error => id,e,data |
| 46 | + const info = utils.splitString(message, 3); |
| 47 | + if (info.length !== 3) return; |
| 48 | + |
| 49 | + const id = info[0]; |
| 50 | + const type = info[1]; |
| 51 | + |
| 52 | + if (type === 's') { |
| 53 | + let data; |
| 54 | + try { |
| 55 | + data = JSON.parse(info[2]); |
| 56 | + } catch (err) { |
| 57 | + return msgCallback(id, new Error(`JSON.parse(data) fail: ${err.message}`)); |
| 58 | + } |
| 59 | + msgCallback(id, null, {result: data}); |
| 60 | + } else if (type === 'e') { |
| 61 | + const err = info[2]; |
| 62 | + msgCallback(id, new Error(err)); |
| 63 | + } |
| 64 | + |
| 65 | + }); |
| 66 | + |
| 67 | + this.push = leiPromise.promisify(this.push.bind(this)); |
| 68 | + this.exit = leiPromise.promisify(this.exit.bind(this)); |
| 69 | + |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * push to queue |
| 74 | + * |
| 75 | + * @param {Object} info |
| 76 | + * - {Object} data |
| 77 | + * - {Number} maxAge |
| 78 | + * @param {Function} |
| 79 | + */ |
| 80 | + push(info, callback) { |
| 81 | + |
| 82 | + info.maxAge = ('maxAge' in info ? info.maxAge : this._maxAge); |
| 83 | + |
| 84 | + const expire = utils.secondTimestamp() + info.maxAge; |
| 85 | + let data; |
| 86 | + try { |
| 87 | + data = JSON.stringify(info.data); |
| 88 | + } catch (err) { |
| 89 | + return callback(new Error(`JSON.stringify(data) fail: ${err.message}`)); |
| 90 | + } |
| 91 | + |
| 92 | + const id = utils.integerToBase64(++this._msgId); |
| 93 | + // msg: producer,id,expire,data |
| 94 | + const content = `${this.name},${id},${expire},${data}`; |
| 95 | + |
| 96 | + this._redis.lpush(this._queueKey, content, err => { |
| 97 | + if (err) return callback(err); |
| 98 | + |
| 99 | + this._msgCallback.once(id, callback); |
| 100 | + }); |
| 101 | + |
| 102 | + } |
| 103 | + |
| 104 | + /** |
| 105 | + * exit |
| 106 | + * |
| 107 | + * @param {Function} callback |
| 108 | + */ |
| 109 | + exit(callback) { |
| 110 | + this._redis.end(); |
| 111 | + this._msgCallback = null; |
| 112 | + callback && callback(null); |
| 113 | + } |
| 114 | + |
| 115 | +} |
| 116 | + |
| 117 | +module.exports = Producer; |
0 commit comments