Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 33 additions & 2 deletions cumin.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,12 +113,19 @@ module.exports = function(port, host, options) {
nonBlockingClient.publish("cumin.enqueued", message, done);
},

listen: function(queueName, handler) {
listen: function(queueName, autoReconnect, handler) {

// Do the argument shuffle
if (!handler) {
handler = autoReconnect;
autoReconnect = false;
}

if(!queueName) {
throw new Error(consolePrefix, "Queue name must be provided. eg. 'emailQueue'.");
}

if(!handler) {
if(!handler || typeof handler !== 'function') {
throw new Error(consolePrefix, "You must provide a hander to .listen.");
}

Expand All @@ -141,6 +148,30 @@ module.exports = function(port, host, options) {
}

continueListening("cumin." + queueName, handler);


if (autoReconnect) {

blockingClient.on('end', function(err) {
alreadyListening = false;
})

blockingClient.on('error', function(err) {
console.warn('Blocking client disconnect.');
});

nonBlockingClient.on('error', function(err) {
console.warn('Non-blocking client disconnect.');
});

blockingClient.on('ready', function() {
if (!alreadyListening) {
alreadyListening = true;
continueListening('cumin.' + queueName, handler);
}
});

}
}
}
}