[LRUG] Rails after_create transaction queueing problem

Pat Allan pat at freelancing-gods.com
Mon Jun 16 10:03:14 PDT 2008


Hi David

I've come across this before... there's two very similar solutions I  
can point you to - both adding an after_commit callback.
First off - the original source:
http://elimiller.blogspot.com/2007/06/proper-cache-expiry-with-aftercommit.html

My modifications, to make it more friendly with 2.0 and 2.1:
http://github.com/freelancing-god/thinking-sphinx/tree/master/lib/thinking_sphinx/active_record/delta.rb

And a more rounded solution, that handles nested transactions (so I'll  
be pulling this plugin into my own in the near future):
http://github.com/GUI/after_commit/tree/master

Hope this is helpful.

Cheers

-- 
Pat
e: pat at freelancing-gods.com    || m: 0413 273 337
w: http://freelancing-gods.com || p: 03 9386 0928
discworld: http://ausdwcon.org || skype: patallan

On 16/06/2008, at 9:56 AM, David Salgado wrote:

> 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
> _______________________________________________
> Chat mailing list
> Chat at lists.lrug.org
> http://lists.lrug.org/listinfo.cgi/chat-lrug.org




More information about the Chat mailing list