Richard Bucker

Getting Mojolicious and AnyEvent::Beanstalkd to work

Posted at — Nov 17, 2011

I have been having a hard time getting Mojolicious and AnyEvent::Beanstalkd to integrate. Given the commonality of AnyEvent one would think that it would a) work off the shelf; and b) there would plenty of documentation or examples. Sadly neither are true and I had to test several permutations, give up, and then try one last method (which worked).It basically comes down to this. $cv->recv() does not work inside a Mojolicious. It causes a recursion error that AnyEvent just barely explains. The first alternative, that worked, was a matter of nesting all of the callback functions and as long as the stack is shallow that would work but it’s not pretty and it’s certainly unmaintainable.In the end the code was replaced with $txn->cb(sub { $[0]->recv }); and then the beanstalkd commands were flattened; and everything seems to be working nicely now. There was no explanation as to why this worked.After looking at the AnyEvent code I think the code was able to “falsely” detect the $cv->revc() as a recursive call where the other format is undetectable. ¬†I’m not certain whether this is good or bad but it works. . . . my $txn = AnyEvent::condvar; $txn->begin; $client->use(‘restmq’, sub {$txn->end;}); $txn->begin; $client->watch($guid, sub {$txn->end;}); $txn->cb(sub { $[0]->recv }); $txn = AnyEvent::condvar; $txn->begin; my $job2 = $client->put( $msg, sub {$txn->end;}); $txn->begin; $client->reserve( sub { my $job = shift; my $decode = $job->decode; my $uuid = (@$decode)[0][0]->{‘uuid’}; my $retval = sprintf “data> %s, %s\n”, $job->data, $uuid; $client->delete($job->id); $client->ignore($guid); $client->quit; $self->render(text => $retval); $txn->end; }); $txn->cb(sub { $_[0]->recv }); $self->render_later;The complete¬†code is here. I’ll be posting my scratch code shortly and I’ll be updating and commenting the code too.