Today I started hacking on a re-implementation of my BlogSpam service - which tests that incoming comments are SPAM/HAM - in node.js (blogspam.js)
The current API uses XML::RPC and a perl server, along with a list of plugins, to do the work.
Having had some fun and success with the HTTP+JSON mstore toy I figured I'd have a stab at making BlogSpam more modern:
- Receive a JSON body via HTTP-POST.
- Deserialize it.
- Run the body through a series of Javascript plugins.
- Return the result back to the caller via HTTP status-code + text.
In theory this is easy, I've hacked up a couple of plugins, and a Perl client to make a submission. But sadly the async-stuff is causing me .. pain.
This is my current status:
shelob ~/git/blogspam.js $ node blogspam.js Loaded plugin: ./plugins/10-example.js Loaded plugin: ./plugins/20-ip.js Loaded plugin: ./plugins/80-sfs.js Loaded plugin: ./plugins/99-last.js Received submission: {"body":"This is my body ..
","ip":"109.194.111.184","name":"Steve Kemp"} plugin 10-example.js said next :next plugin 20-ip.js said next :next plugin 99-last.js said spam SPAM: Listed in StopForumSpam.com
So we've loaded plugins, and each has been called. But the end result was "SPAM: Listed .." and yet the caller didn't get that result. Instead the caller go this:
shelob ~/git/blogspam.js $ ./client.pl 200 OK 99-last.js
The specific issue is that I iterate over every loaded-plugin, and wait for them to complete. Because they complete asynchronously the plugin which should be last, and just return "OK" , has executed befure the 80-sfs.js plugin. (Which makes an outgoing HTTP request).
I've looked at async, I've looked at promises, but right now I can't get anything working.
Meh.
Surprise me with a pull request ;)
Tags: blogspam, mstore, node.js 6 comments