2015-12-16 22:57:58 +08:00
|
|
|
'use strict';
|
|
|
|
|
2016-05-18 17:06:49 +08:00
|
|
|
var debug = require('./util/debug')('job-subscriber');
|
2016-04-05 02:00:18 +08:00
|
|
|
var SUBSCRIBE_INTERVAL_IN_MILLISECONDS = 10 * 60 * 1000; // 10 minutes
|
|
|
|
|
|
|
|
function _subscribe(client, channel, queueSeeker, onMessage) {
|
|
|
|
queueSeeker.seek(onMessage, function (err) {
|
|
|
|
if (err) {
|
2016-05-18 17:06:49 +08:00
|
|
|
debug(err);
|
2016-04-05 02:00:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
client.removeAllListeners('message');
|
|
|
|
client.unsubscribe(channel);
|
|
|
|
client.subscribe(channel);
|
|
|
|
client.on('message', onMessage);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2016-04-05 02:00:18 +08:00
|
|
|
module.exports = JobSubscriber;
|
|
|
|
|
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-05 02:00:18 +08:00
|
|
|
_subscribe(this.client, this.channel, this.queueSeeker, onMessage);
|
2016-03-31 23:37:35 +08:00
|
|
|
|
2016-04-05 02:00:18 +08:00
|
|
|
this.seekerInterval = setInterval(
|
|
|
|
_subscribe,
|
|
|
|
SUBSCRIBE_INTERVAL_IN_MILLISECONDS,
|
|
|
|
this.client,
|
|
|
|
this.channel,
|
|
|
|
self.queueSeeker,
|
|
|
|
onMessage
|
|
|
|
);
|
2015-12-16 22:57:58 +08:00
|
|
|
};
|
|
|
|
|
2015-12-23 03:12:10 +08:00
|
|
|
JobSubscriber.prototype.unsubscribe = function () {
|
2016-04-05 02:00:18 +08:00
|
|
|
clearInterval(this.seekerInterval);
|
2015-12-23 03:12:10 +08:00
|
|
|
this.client.unsubscribe(this.channel);
|
|
|
|
};
|