[LRUG] Rails after_create transaction queueing problem

David Salgado david at digitalronin.com
Mon Jun 16 09:56:19 PDT 2008


Hi Folks

I've come across an odd quirk when trying to use beanstalk to manage
my tasks, and I'm wondering how other people have handled this.

I've got an 'add_to_beanstalk' method which gets called in an
after_create hook on a Rails model. All it does is to put the ID of
the newly-created model instance onto the appropriate beanstalk queue.
A separate process consumes this queue.

The trouble is that the after_create callback is called before the
'COMMIT' which finalises the INSERT. So, what happens is;

1. The after_create method gets the ID of the current record and puts
it on the beanstalk queue.
2. The agent which is processes the beanstalk queue gets the ID and
does a find to retrieve the record
3. The model class calls COMMIT to finish the model create.

This means that step 2 fails with a 'record not found' exception. In
theory, I would expect this issue to crop up with any queuing system,
not just beanstalk.

I've hacked around this by calling
self.class.connection.commit_db_transaction inside my after_create
method. This works, but it smells really bad.

Has anyone else come across this issue, and if so, how did you get around it?

Any help will be gratefully received and rewarded with a pint at the
next LRUG/LRUG Nights.

Cheers

David



More information about the Chat mailing list