Permalink
Browse files

added error arg to the calback data

  • Loading branch information...
1 parent 51b37ca commit 7b26eeef4bf48bdb0028796b4a3b65b429269cff @benlund committed Aug 15, 2010
Showing with 61 additions and 42 deletions.
  1. +14 −15 README
  2. +4 −4 lib/beanstalk_client.coffee
  3. +4 −5 lib/beanstalk_client.js
  4. +22 −0 test/#connect.js#
  5. +5 −5 test/connect.coffee
  6. +4 −4 test/connect.js
  7. +8 −9 test/test.js
View
29 README
@@ -1,31 +1,30 @@
Beanstalkd Client for Node.js
=============================
-Version: 0.0.1
-
--- Not all commands are yet implemented --
+Version: 0.2
Example:
-var sys = require('sys');
-var bt = require('lib/beanstalk_client');
+var client = require('beanstalk_client').Client;
-bt.Client.connect('127.0.0.1:11300').addCallback(function(conn) {
+client.connect('127.0.0.1:11300', function(err, conn) {
var job_data = {"data": {"name": "node-beanstalk-client"}};
- conn.put(0, 0, 1, JSON.stringify(job_data)).addCallback(function(job_id) {
-
- sys.puts('put job: ' + job_id);
-
- conn.reserve().addCallback(function(job_id, job_json) {
- sys.puts('got job: ' + job_id);
- sys.puts('got job data: ' + job_json);
- sys.puts('module name is ' + JSON.parse(job_json).data.name);
- conn.destroy(job_id);
+ conn.put(0, 0, 1, JSON.stringify(job_data), function(err, job_id) {
+ console.log('put job: ' + job_id);
+
+ conn.reserve(function(err, job_id, job_json) {
+ console.log('got job: ' + job_id);
+ console.log('got job data: ' + job_json);
+ console.log('module name is ' + JSON.parse(job_json).data.name);
+ conn.destroy(job_id, function(err) {
+ console.log('destroyed job');
+ });
});
});
});
+
Try it:
$ node test/test.js
@@ -54,8 +54,8 @@ class Connection
@try_handling_response()
- close: () ->
- @stream.close()
+ end: () ->
+ @stream.end()
try_handling_response: () ->
@@ -67,9 +67,9 @@ class Connection
if handler.complete
@finished_handling_response();
if handler.success
- callback.apply(null, handler.args)
+ callback.call(null, false, handler.args...)
else
- ##todo
+ callback.call(null, handler.args[0])
else
handler.reset()
View
@@ -55,8 +55,8 @@
}, this));
return this;
};
- Connection.prototype.close = function() {
- return this.stream.close();
+ Connection.prototype.end = function() {
+ return this.stream.end();
};
Connection.prototype.try_handling_response = function() {
var _a, callback, handler;
@@ -68,11 +68,10 @@
if (handler.complete) {
this.finished_handling_response();
if (handler.success) {
- return callback.apply(null, handler.args);
+ return callback.call.apply(callback, [null, false].concat(handler.args));
} else {
-
+ return callback.call(null, handler.args[0]);
}
- //#todo
} else {
return handler.reset();
}
View
@@ -0,0 +1,22 @@
+var sys = require('sys');
+var client = require('../lib/beanstalk_client').Client;
+
+client.connect(null, function(err, conn) {
+
+ sys.puts('success connecting');
+ conn.use('test').addCallback(function() {
+ conn.watch('test').addCallback(function() {
+ conn.put(0, 0, 1, 'hiben').addCallback(function(job_id) {
+ sys.puts('put job: ' + job_id);
+
+ conn.reserve().addCallback(function(job_id, data) {
+ sys.puts('got job: ' + job_id);
+ sys.puts('job data: ' + data);
+ conn.destroy(job_id);
+ });
+ });
+ });
+ });
+}).addErrback(function() {
+ sys.puts('error connecting');
+});
View
@@ -9,7 +9,7 @@ client.connect null, (err, conn) ->
sys.puts('Producer connected')
conn.use 'test', () ->
- conn.put 0, 0, 1, 'hiben', (job_id) ->
+ conn.put 0, 0, 1, 'hiben', (err, job_id) ->
sys.puts('Producer sent job: ' + job_id)
@@ -20,11 +20,11 @@ client.connect null, (err, conn) ->
else
sys.puts('Consumer connected')
- conn.watch 'test', () ->
+ conn.watch 'test', (err) ->
- conn.reserve (job_id, data) ->
+ conn.reserve (err, job_id, data) ->
sys.puts('Consumer got job: ' + job_id)
sys.puts(' job data: ' + data)
- conn.destroy job_id, () ->
- sys.puts('Consumer destroyed job: ' + job_id)
+ conn.destroy job_id, (err) ->
+ sys.puts('Consumer destroyed job: ' + job_id)
View
@@ -11,7 +11,7 @@
}
sys.puts('Producer connected');
return conn.use('test', function() {
- return conn.put(0, 0, 1, 'hiben', function(job_id) {
+ return conn.put(0, 0, 1, 'hiben', function(err, job_id) {
return sys.puts('Producer sent job: ' + job_id);
});
});
@@ -22,11 +22,11 @@
return sys.puts('Consumer connection error:', sys, inspect(err));
} else {
sys.puts('Consumer connected');
- return conn.watch('test', function() {
- return conn.reserve(function(job_id, data) {
+ return conn.watch('test', function(err) {
+ return conn.reserve(function(err, job_id, data) {
sys.puts('Consumer got job: ' + job_id);
sys.puts(' job data: ' + data);
- return conn.destroy(job_id, function() {
+ return conn.destroy(job_id, function(err) {
return sys.puts('Consumer destroyed job: ' + job_id);
});
});
View
@@ -1,17 +1,16 @@
-var sys = require('sys');
var client = require('../lib/beanstalk_client').Client;
client.connect('127.0.0.1:11300', function(err, conn) {
var job_data = {"data": {"name": "node-beanstalk-client"}};
- conn.put(0, 0, 1, JSON.stringify(job_data), function(job_id) {
- sys.puts('put job: ' + job_id);
+ conn.put(0, 0, 1, JSON.stringify(job_data), function(err, job_id) {
+ console.log('put job: ' + job_id);
- conn.reserve(function(job_id, job_json) {
- sys.puts('got job: ' + job_id);
- sys.puts('got job data: ' + job_json);
- sys.puts('module name is ' + JSON.parse(job_json).data.name);
- conn.destroy(job_id, function() {
- sys.puts('destroyed job');
+ conn.reserve(function(err, job_id, job_json) {
+ console.log('got job: ' + job_id);
+ console.log('got job data: ' + job_json);
+ console.log('module name is ' + JSON.parse(job_json).data.name);
+ conn.destroy(job_id, function(err) {
+ console.log('destroyed job');
});
});

0 comments on commit 7b26eee

Please sign in to comment.