I have an ActiveRecord transaction that takes place during a web service call. It doesn’t need to be synchronous with the call and there is a delay while this transaction happens and the user sees a response from the server because ActiveRecord is busy creating object(s) and updating the db (while retaining a lock on a certain MyISAM table) + running all of its callbacks, etc.
What’s a good strategy to move this ActiveRecord create call so that it is not synchronously holding up the web service? I need to pass the create call parameters from a user session that is created in the action, so I’m not sure if an after_filter will make sense. Should I use a delayed job, messaging queue, or is an after_filter the best bet? If the answer is an after_filter, can I pass it parameters?
Use a delayed job.
Just add
gem 'delayed_job'to your Gemfile, then runbundle install,rails g delayed_jobandrake db:migrate.Write a method on your user model that performs the stuff you want in the background, and tell it to always run in the background
Finally, call that method from your controller:
This should get you started. See https://github.com/collectiveidea/delayed_job for more info.