Press enter to see results or esc to cancel.

Rails Implementation of Delayed_jobs

From previous post regarding Rails Introduction of Background Process, we already knew the usage and importance of background process. It helps us to optimise the performance without sacrifice the lost of data or process.

First step, we need to add Delayed_jobs gem in our Gemfile

gem ‘delayed_job_active_record’

then we just bundle install it.

after that we need to generate a table in our database to record all the delayed_jobs activities

$ rails generate delayed_job:active_record

then as usual, 

$ rake db:migrate

in localhost, we need to run the rake jobs:work

$ bundle exec rake jobs:work

and we need to put this line on Procfile, so Heroku can identify and run worker to do that command

worker: bundle exec rake jobs:work

done, all the requirement set-up already, we can straight away use it in our controller

For implementation in controller, we used to use this 

UserMailer.notification_email(mymail).deliver

to send the notification email, now we can queue this process in the background simply by put .delay method on it  and remove the deliver, rails will automagically queue and deliver it

UserMailer.delay.notification_email(mymail)

for other method, we can just simply put .delay on that method

Post.send_notif(user)

become

Post.send_notif(user).delay

done.

 So far, we satisfy with Delayed_jobs, however we also looking at Resque and Sidekiq (which both use Redis instead of Active_record) for anticipation of scalability issues later on. We tried implement Sidekiq for some of our process and saw that the performance was awesome and very fast, however currently we switch it off first for some reason.

Comments

Leave a Comment

Show Buttons
Hide Buttons