In ruby on rails application, if your request is taking too much time to process, you can make some part of the code to run in background.
In my application , I am sending notifications to owner,watch listers and trackers of a post, if there is any activity on the post. So when someone comments on the post, people will be getting notifications. I am using delayed jobs to generate notifications. But the problem here is inserts into delayed jobs. Whenever I comment on a post, all the inserts into the delayed job table takes long time, hence the response is coming too late.
class CommentsController < ApplicationController def comment @comment = Comment.new(params[:comment]) @comment.save @comment.send_messages end end
To make the inserts into delayed jobs in the background, I used Workling. Implementing Workling is very simple.
1. Install the Workling plugin
script/plugin install git://github.com/purzelrakete/workling.git
2. Create the file my_worker.rb in app/workers folder
class MyWorker < Workling::Base def delayed_jobs_entry(options) comment=options[:comment] comment.send_messages end end
3. Call to the worker method from the CommentsController
class CommentsController < ApplicationController def comment @comment = Comment.new(params[:comment]) @comment.save My
Worker.asynch_delayed_jobs_entry(:comment => @comment)end end
4. Start workling
Now you see the difference how fast the response is…