Nodestalker - A beanstalkd client for node.js
by Pascal Opitz on May 14 2010, 12:31
Following my previous article about PHP and beanstalkd I was keen to use beanstalk in combination with node.js, but wasn't entirely satisfied with the available implementation. So as a little brain exercise I wrote nodestalker from scratch.
Usage
Usage should be pretty straight forward. Below is the code for putting a job into the queue:
var sys = require('sys');
var bs = require('../lib/beanstalk_client');
var client = bs.Client();
client.use('default').onSuccess(function(data) {
sys.puts(sys.inspect(data));
client.put('my job').onSuccess(function(data) {
sys.puts(sys.inspect(data));
client.disconnect();
});
});
Issues during development
Writing it wasn't as easy as I expected, but that was because I didn't read the documentation on sockets properly, and missed out on stream.setKeepAlive()
and stream.setNoDelay()
first, which made my sockets close on me in what I thought was a random manner.
Another issue I had was the yaml library for node, which just didn't like the yaml formatted beanstalk output, which doesn't contain the expected indentations. Not sure why, maybe a yaml version conflict or something, but I hacked the yaml parsing with some regular expressions.
var corrected = str.replace(/\n-\ ([\w\d_-]+)/mig, '\n - '$1'') //indent list
.replace(/(\w)\-(\w)/mgi, '$1_$2') // replace minuses in hash names
.replace(/\n([\w\d_-]+)\:\ ([\.\,\w\d_-]+)/mig, '\n $1: '$2''); // format hashes
try {
return kiwi.require('yaml').eval(corrected);
} catch(e) {
Debug.log(e);
Debug.log(corrected);
return str;
}
Demo application: Beanspector
I also included a demo application, which I baptised "beanspector". It is a simple command line client to inspect, list and empty tubes and put some content into them. This is how I use it in action:
$ node beanspector.js -lt
[ 'default', 'confirm' ]
$ node beanspector.js -pt default "here"
61
$ node beanspector.js -lc default
listing tube default
{ id: '61', data: 'here' }
Download
Get nodestalker from GitHub, and feel free to fork and do some improvements if you want.
Comments
I rolled out a bunch of updates a couple of days ago and moved nodestalker into npm. Beanspector now is it's own repo on github.
by Pascal Opitz on March 6 2011, 06:49 #