Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

API revision to Worker constructor

  • Loading branch information...
commit 2b6800ff248ac855dbf586b6aea78b26f103b13a 1 parent cf00cbd
Tim tim-smart authored
Showing with 48 additions and 71 deletions.
  1. +8 −8 README
  2. +29 −54 index.js
  3. +11 −9 test/worker.test.js
16 README
View
@@ -38,17 +38,17 @@ queue-worker.js:
// Require the node-redis-queue
var client = require('./node-redis-queue');
-// Create a worker.
-var worker = client.createWorker('localhost', 6379);
+// Create a worker for each queue.
+var worker = client.createWorker('user', 'localhost', 6379);
-// Setup events for each queue you want to listen to.
-worker.on('user', function (job) {
+// Setup message event
+worker.on('message', function (job) {
// Process message
process(job.payload);
-});
-// Listen for all messages on worker.
-worker.on('*', fn);
+ // Listen for next job
+ worker.next();
+});
// Start listening.
-worker.listen('user');
+worker.start();
83 index.js
View
@@ -84,13 +84,16 @@ Queue.prototype.push = function (payload, callback) {
* Inherits from EventEmitter.
* @constructor
*/
-var Worker = function (host, port) {
+var Worker = function (name, host, port) {
var self = this;
- this.host = host;
- this.port = port;
- this.prefix = 'queue:';
- this.queues = {};
+ this.host = host;
+ this.port = port;
+ this.prefix = 'queue:';
+ this.name = name;
+ this.queues = {};
+ // TODO: Rename?
+ this.continual = false;
// Client for use with child jobs.
this.child_client = redis.createClient(host, port);
@@ -106,12 +109,15 @@ var Worker = function (host, port) {
return self.emit('error', error);
}
- var key = removePrefix(self.prefix, data[0].toString()),
- data = JSON.parse(Buffer.isBuffer(data[1]) ? data[1].toString() : data),
- job = new Job(self, data, key);
+ var data = JSON.parse(Buffer.isBuffer(data[1]) ? data[1].toString() : data),
+ job = new Job(self, data);
+
+ self.emit('message', job);
- self.emit(key, job);
- self.emit('*', job);
+ if (!self.client.quitting && self.continual) {
+ // Listen for more jobs.
+ self.client.blpop(self.prefix + self.name, 0, self._onPop);
+ }
};
};
@@ -131,57 +137,26 @@ exports.createWorker = function (host, port) {
exports.Worker = Worker;
-/*
- * Recycle the client.
- * @private
+/**
+ * Listen for the next job. Only has to be called by user if `continual` is false.
*/
-Worker.prototype._recycle = function () {
- if (this.client) {
- this.client.destroy();
- }
-
- var keys = Object.keys(this.queues);
-
- if (0 < keys.length) {
- var key_string = '';
- for (var i = 0, il = keys.length; i < il; i++) {
- key_string += this.prefix + keys[i] + ' ';
- }
- key_string = key_string.slice(0, -1);
-
- this.client = redis.createClient(this.host, this.port);
- this.client.blpop(key_string, 0, this._onPop);
- }
-
- // TODO: Recovery when disconnected. Is it needed? (node-redis might re-send the command for us)
-}
+Worker.prototype.next = function () {
+ this.client.blpop(this.prefix + this.name, 0, this._onPop);
+};
/**
- * Tell the worker what queue(s) to listen on.
- * We have to create a new redis client for each queue, as we use
- * blpop.
+ * Start the worker
*/
-Worker.prototype.listen = function () {
- for (var i = 0, il = arguments.length; i < il; i++) {
- this.queues[arguments[i]] = null;
- }
-
- this._recycle();
+Worker.prototype.start = function () {
+ this.client = redis.createClient(this.host, this.port);
+ this.next();
};
-/*
- * Un-listen to the specified queues
+/**
+ * Stop the worker
*/
-Worker.prototype.unlisten = function () {
- var name;
-
- for (var i = 0, il = arguments.length; i < il; i++) {
- name = arguments[i];
- delete this.queues[name];
- this.removeAllListeners(name);
- }
-
- this._recycle();
+Worker.prototype.stop = function () {
+ this.client.destroy();
};
20 test/worker.test.js
View
@@ -2,9 +2,11 @@ var client = require('../'),
assert = require('assert');
var q = client.createQueue('worker.test'),
- w = client.createWorker(),
+ w = client.createWorker('worker.test'),
job;
+w.continual = true;
+
job = new client.Job(w, {
payload: 'testing',
id: 2,
@@ -19,7 +21,7 @@ q.push({
module.exports = {
"test Worker events": function (done) {
- w.on('worker.test', function (job) {
+ w.once('message', function (job) {
assert.ok(job);
assert.equal(typeof job.reportError, 'function');
assert.equal(typeof job.retry, 'function');
@@ -33,11 +35,11 @@ module.exports = {
done();
});
- w.listen('worker.test');
+ w.start();
},
- "test Worker#unlisten": function () {
- w.unlisten('worker.test');
- assert.equal(w.listeners('worker.test').length, 0);
+ "test Worker#stop": function () {
+ w.stop();
+ assert.ok(w.client.quitting);
},
"test Job#reportError": function () {
job.reportError(new Error('Bacon was not tasty enough.'));
@@ -46,7 +48,7 @@ module.exports = {
assert.equal(job.error_count, 1);
},
"test Job#retry": function (done) {
- w.on('worker.test', function (job) {
+ w.on('message', function (job) {
assert.ok(job);
assert.equal(job.id, 2);
@@ -58,7 +60,7 @@ module.exports = {
done();
});
- w.listen('worker.test');
+ w.start();
job.retry(function (error, id) {
assert.ok(!error);
@@ -71,6 +73,6 @@ module.exports = {
q.client.quit();
w.child_client.quit();
- w.client.destroy();
+ w.stop();
}
}
Please sign in to comment.
Something went wrong with that request. Please try again.