CartoDB-SQL-API/batch/pubsub/job-subscriber.js
Daniel García Aubert 36d5dc1719 Queue seeker refactor:
- Extract from job subcriber everything related to queue dicovering
 - Job Queue is responsible of queue dicovering and exposes functions to scan user queues.
 - Batch creates an interval to consume queues
 - Now Batch.start() does:
  * Finds existing queues from metadata, adding to queue index the ones that are not indexed yet
  * Binds callback to process jobs for every queue found.
  * Inits queue consumer interval
  * Subscribes to PUB/SUB channel
2017-04-03 19:26:21 +02:00

55 lines
1.4 KiB
JavaScript

'use strict';
var Channel = require('./channel');
var debug = require('./../util/debug')('pubsub:subscriber');
var error = require('./../util/debug')('pubsub:subscriber:error');
function JobSubscriber(pool) {
this.pool = pool;
}
module.exports = JobSubscriber;
JobSubscriber.prototype.subscribe = function (onJobHandler, callback) {
var self = this;
self.pool.acquire(Channel.DB, function(err, client) {
if (err) {
if (callback) {
callback(err);
}
return error('Error adquiring redis client: ' + err.message);
}
self.client = client;
client.removeAllListeners('message');
client.unsubscribe(Channel.NAME);
client.subscribe(Channel.NAME);
client.on('message', function (channel, user) {
debug('message received in channel=%s from user=%s', channel, user);
onJobHandler(user);
});
client.on('error', function () {
self.unsubscribe();
self.pool.release(Channel.DB, client);
self.subscribe(onJobHandler);
});
if (callback) {
callback();
}
});
};
JobSubscriber.prototype.unsubscribe = function (callback) {
if (this.client && this.client.connected) {
this.client.unsubscribe(Channel.NAME, callback);
} else {
if (callback) {
return callback(null);
}
}
};