Permalink
Browse files

Multiple response codes possible, onError, onSuccess, onEnd

  • Loading branch information...
1 parent c93cc31 commit 76e2e46705415fd53ae85746d82f49de46ed3ced @pascalopitz committed Mar 11, 2013
Showing with 40 additions and 16 deletions.
  1. +1 −0 .gitignore
  2. +38 −15 lib/beanstalk_client.js
  3. +1 −1 tests/test_peek_delayed.js
View
@@ -0,0 +1 @@
+node_modules
@@ -40,19 +40,42 @@ function BeanstalkCommand() {
};
util.inherits(BeanstalkCommand, events.EventEmitter);
-BeanstalkCommand.prototype.onSuccess = function(callback) {
- this.addListener('command_done', function(data) {
- callback(data);
+BeanstalkCommand.prototype.onEnd = function(fn) {
+ this.addListener('command_error', function(err) {
+ fn(err, null);
});
+
+ this.addListener('command_success', function(data) {
+ fn(null, data);
+ });
+
+ return this;
+};
+
+BeanstalkCommand.prototype.onError = function(fn) {
+ this.addListener('command_error', fn);
+};
+
+BeanstalkCommand.prototype.onSuccess = function(fn) {
+ this.addListener('command_success', fn);
};
BeanstalkCommand.prototype.responseHandler = function(data, obj, callback) {
var lines = data.toString().split('\r\n');
var chunks = lines[0].split(' ');
var jobdata = false;
- if(obj.expected != chunks[0]) {
- this.emit('command_done', chunks);
+ var expected = (typeof obj.expected === "string") ? [ obj.expected ] : obj.expected;
+ var expected_match = false;
+
+ for(var i=0; i<expected.length; i++) {
+ if(expected[i] === chunks[0]) {
+ expected_match = true;
+ }
+ }
+
+ if(!expected_match) {
+ this.emit('command_error', chunks);
return false;
}
@@ -63,8 +86,8 @@ BeanstalkCommand.prototype.responseHandler = function(data, obj, callback) {
jobdata = lines.join('\r\n');
}
- if(obj.is_yaml) {
- this.emit('command_done', yaml.load(jobdata));
+ if(obj.is_yaml && jobdata) {
+ this.emit('command_success', yaml.load(jobdata));
} else {
if(chunks.length > 1) {
chunks.shift();
@@ -76,7 +99,7 @@ BeanstalkCommand.prototype.responseHandler = function(data, obj, callback) {
}
}
- this.emit('command_done', chunks);
+ this.emit('command_success', chunks);
}
return true;
@@ -119,7 +142,7 @@ BeanstalkClient.prototype.command = function(obj) {
var cmd = new BeanstalkCommand();
// makes sure that if there's another command queued, it gets done
- cmd.addListener('command_done', function(data) {
+ cmd.addListener('command_success', function(data) {
if(_self.queue.length) {
var next = _self.queue.shift();
process.nextTick(function() {
@@ -218,7 +241,7 @@ BeanstalkClient.prototype.watch = function(tube) {
BeanstalkClient.prototype.ignore = function(tube) {
return this.command({
command: 'ignore '+tube+'\r\n',
- expected: 'WATCHING'
+ expected: ['WATCHING', 'NOT_IGNORED']
});
};
@@ -327,7 +350,7 @@ BeanstalkClient.prototype.kick = function(bound) {
BeanstalkClient.prototype.peek = function(id) {
return this.command({
command: 'peek '+id+'\r\n',
- expected: 'FOUND'
+ expected: [ 'FOUND', 'NOT_FOUND' ]
});
};
@@ -336,7 +359,7 @@ BeanstalkClient.prototype.peek = function(id) {
BeanstalkClient.prototype.peek_ready = function() {
return this.command({
command: 'peek-ready\r\n',
- expected: 'FOUND'
+ expected: [ 'FOUND', 'NOT_FOUND' ]
});
};
// camel case alias
@@ -347,7 +370,7 @@ BeanstalkClient.prototype.peekReady = BeanstalkClient.prototype.peek_ready;
BeanstalkClient.prototype.peek_delayed = function() {
return this.command({
command: 'peek-delayed\r\n',
- expected: 'FOUND'
+ expected: [ 'FOUND', 'NOT_FOUND' ]
});
};
// camel case alias
@@ -358,7 +381,7 @@ BeanstalkClient.prototype.peekDelayed = BeanstalkClient.prototype.peek_delayed;
BeanstalkClient.prototype.peek_buried = function() {
return this.command({
command: 'peek-buried\r\n',
- expected: 'FOUND'
+ expected: [ 'FOUND', 'NOT_FOUND' ]
});
};
// camel case alias
@@ -379,7 +402,7 @@ BeanstalkClient.prototype.stats = function() {
BeanstalkClient.prototype.stats_job = function(id) {
return this.command({
command: 'stats-job '+id+'\r\n',
- expected: 'OK',
+ expected: ['OK', 'NOT_FOUND'],
is_yaml: 1
});
};
@@ -5,7 +5,7 @@ var helper = require('./helper');
helper.bind(function(conn, data) {
if(String(data) == 'peek-delayed\r\n') {
- conn.write("OK\r\n");
+ conn.write("NOT_FOUND\r\n");
}
}, true);
var client = helper.getClient();

0 comments on commit 76e2e46

Please sign in to comment.