Added callback to job subscriber to allow to batch service emit ready event

This commit is contained in:
Daniel García Aubert 2016-07-22 13:47:14 +02:00
parent 438d61fb70
commit 0586f45413
2 changed files with 30 additions and 14 deletions

View File

@ -25,7 +25,7 @@ Batch.prototype.start = function () {
Batch.prototype._subscribe = function () {
var self = this;
this.jobSubscriber.subscribe(function (channel, host) {
this.jobSubscriber.subscribe(function onMessage(channel, host) {
var queue = self.jobQueuePool.getQueue(host);
// there is nothing to do. It is already running jobs
@ -46,6 +46,12 @@ Batch.prototype._subscribe = function () {
debug(err);
});
}, function (err) {
if (err) {
return self.emit('error', err);
}
self.emit('ready');
});
};

View File

@ -6,12 +6,7 @@ var error = require('./util/debug')('pubsub:subscriber:error');
var DB = 0;
var SUBSCRIBE_INTERVAL_IN_MILLISECONDS = 10 * 60 * 1000; // 10 minutes
function _subscribe(client, channel, queueSeeker, onMessage) {
queueSeeker.seek(onMessage, function (err) {
if (err) {
error(err);
}
function _subscribe(client, channel, queueSeeker, onMessage, callback) {
client.removeAllListeners('message');
client.unsubscribe(channel);
@ -21,6 +16,21 @@ function _subscribe(client, channel, queueSeeker, onMessage) {
debug('message received from: ' + channel + ':' + host);
onMessage(channel, host);
});
queueSeeker.seek(onMessage, function (err) {
if (err) {
error(err);
if (callback) {
callback(err);
}
} else {
debug('queues found successfully');
if (callback) {
callback();
}
}
});
}
@ -32,7 +42,7 @@ function JobSubscriber(pool, queueSeeker) {
module.exports = JobSubscriber;
JobSubscriber.prototype.subscribe = function (onMessage) {
JobSubscriber.prototype.subscribe = function (onMessage, callback) {
var self = this;
this.pool.acquire(DB, function (err, client) {
@ -42,8 +52,6 @@ JobSubscriber.prototype.subscribe = function (onMessage) {
self.client = client;
_subscribe(self.client, self.channel, self.queueSeeker, onMessage);
self.seekerInterval = setInterval(
_subscribe,
SUBSCRIBE_INTERVAL_IN_MILLISECONDS,
@ -52,6 +60,8 @@ JobSubscriber.prototype.subscribe = function (onMessage) {
self.queueSeeker,
onMessage
);
_subscribe(self.client, self.channel, self.queueSeeker, onMessage, callback);
});
};