2015-12-16 22:57:58 +08:00
|
|
|
'use strict';
|
|
|
|
|
2016-04-04 22:05:33 +08:00
|
|
|
function JobSubscriber(redis, queueSeeker) {
|
2015-12-29 22:46:04 +08:00
|
|
|
this.channel = 'batch:hosts';
|
2015-12-16 22:57:58 +08:00
|
|
|
this.client = redis.createClient(global.settings.redis_port, global.settings.redis_host);
|
2016-04-04 22:05:33 +08:00
|
|
|
this.queueSeeker = queueSeeker;
|
2015-12-16 22:57:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
JobSubscriber.prototype.subscribe = function (onMessage) {
|
2016-03-31 23:37:35 +08:00
|
|
|
var self = this;
|
|
|
|
|
2016-04-04 22:05:33 +08:00
|
|
|
self.queueSeeker.seek(onMessage, function (err) {
|
2016-03-31 23:37:35 +08:00
|
|
|
if (err) {
|
|
|
|
console.error(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
self.client.subscribe(self.channel);
|
|
|
|
self.client.on('message', onMessage);
|
|
|
|
});
|
2015-12-16 22:57:58 +08:00
|
|
|
};
|
|
|
|
|
2015-12-23 03:12:10 +08:00
|
|
|
JobSubscriber.prototype.unsubscribe = function () {
|
|
|
|
this.client.unsubscribe(this.channel);
|
|
|
|
};
|
|
|
|
|
2015-12-16 22:57:58 +08:00
|
|
|
module.exports = JobSubscriber;
|