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
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Â
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
for other method, we can just simply put .delay on that method
Â 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.