2015-12-16 22:57:58 +08:00
|
|
|
'use strict';
|
|
|
|
|
2016-10-12 00:41:59 +08:00
|
|
|
var Channel = require('./channel');
|
2016-10-12 01:01:39 +08:00
|
|
|
var QueueSeeker = require('./queue-seeker');
|
2016-10-12 00:28:46 +08:00
|
|
|
var debug = require('./../util/debug')('pubsub:subscriber');
|
|
|
|
var error = require('./../util/debug')('pubsub:subscriber:error');
|
2016-07-07 16:44:17 +08:00
|
|
|
|
2016-10-12 18:29:18 +08:00
|
|
|
var MINUTE = 60 * 1000;
|
|
|
|
var SUBSCRIBE_INTERVAL = 5 * MINUTE;
|
2016-04-05 02:00:18 +08:00
|
|
|
|
2016-10-12 01:01:39 +08:00
|
|
|
function JobSubscriber(pool) {
|
2016-07-07 20:14:46 +08:00
|
|
|
this.pool = pool;
|
2016-10-12 01:01:39 +08:00
|
|
|
this.queueSeeker = new QueueSeeker(pool);
|
2015-12-16 22:57:58 +08:00
|
|
|
}
|
|
|
|
|
2016-04-05 02:00:18 +08:00
|
|
|
module.exports = JobSubscriber;
|
|
|
|
|
2016-10-12 01:59:11 +08:00
|
|
|
function seeker(queueSeeker, onJobHandler, callback) {
|
2016-10-12 01:45:26 +08:00
|
|
|
queueSeeker.seek(function (err, hosts) {
|
|
|
|
if (err) {
|
2016-10-12 01:59:11 +08:00
|
|
|
if (callback) {
|
|
|
|
callback(err);
|
|
|
|
}
|
2016-10-12 01:45:26 +08:00
|
|
|
return error(err);
|
|
|
|
}
|
|
|
|
debug('queues found successfully');
|
|
|
|
hosts.forEach(onJobHandler);
|
2016-10-12 01:59:11 +08:00
|
|
|
|
|
|
|
if (callback) {
|
|
|
|
return callback(null);
|
|
|
|
}
|
2016-10-12 01:45:26 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-10-12 01:59:11 +08:00
|
|
|
JobSubscriber.prototype.subscribe = function (onJobHandler, callback) {
|
2016-03-31 23:37:35 +08:00
|
|
|
var self = this;
|
|
|
|
|
2016-10-12 18:29:18 +08:00
|
|
|
this.seekerInterval = setInterval(seeker, SUBSCRIBE_INTERVAL, this.queueSeeker, onJobHandler);
|
2016-10-12 01:45:26 +08:00
|
|
|
|
2016-10-12 00:41:59 +08:00
|
|
|
this.pool.acquire(Channel.DB, function (err, client) {
|
2016-07-07 20:14:46 +08:00
|
|
|
if (err) {
|
|
|
|
return error('Error adquiring redis client: ' + err.message);
|
|
|
|
}
|
|
|
|
|
|
|
|
self.client = client;
|
2016-10-12 01:45:26 +08:00
|
|
|
client.removeAllListeners('message');
|
|
|
|
client.unsubscribe(Channel.NAME);
|
|
|
|
client.subscribe(Channel.NAME);
|
|
|
|
|
|
|
|
client.on('message', function (channel, host) {
|
|
|
|
debug('message received from: ' + channel + ':' + host);
|
|
|
|
onJobHandler(host);
|
|
|
|
});
|
2016-10-12 01:45:43 +08:00
|
|
|
|
|
|
|
client.on('error', function () {
|
|
|
|
self.unsubscribe();
|
|
|
|
self.pool.release(Channel.DB, client);
|
|
|
|
self.subscribe(onJobHandler);
|
|
|
|
});
|
2016-07-07 20:14:46 +08:00
|
|
|
});
|
2016-03-31 23:37:35 +08:00
|
|
|
|
2016-10-12 01:59:11 +08:00
|
|
|
seeker(this.queueSeeker, onJobHandler, callback);
|
2015-12-16 22:57:58 +08:00
|
|
|
};
|
|
|
|
|
2016-10-12 18:29:54 +08:00
|
|
|
JobSubscriber.prototype.unsubscribe = function (callback) {
|
2016-04-05 02:00:18 +08:00
|
|
|
clearInterval(this.seekerInterval);
|
2016-07-07 20:14:46 +08:00
|
|
|
if (this.client && this.client.connected) {
|
2016-10-12 18:29:54 +08:00
|
|
|
this.client.unsubscribe(Channel.NAME, callback);
|
|
|
|
} else {
|
|
|
|
return callback(null);
|
2016-07-07 20:14:46 +08:00
|
|
|
}
|
2015-12-23 03:12:10 +08:00
|
|
|
};
|