Consider we have a long running task like sending emails to large number of
users,the user have to wait until the request is completed then only the rails
application can accept another request.so it is preferable to run the long
running tasks in the background,for that we can use sidekiq or resque or
DelayedJob etc.Sidekiq and rescue requires redis,here we are going to use
delayedjob.Lets us see how to use delayed_jobs gem in our rails application.
Add the following to your gem file.
gem 'delayed_job_active_record'
Run the bundle install command to install the gems.
The following command will create a table as it is required to have a jobs table for AR backend.
rails generate delayed_job:active_record
rake db:migrate
Then we can use .delay method to process any job in the background.
without delayed_job
UserMailer.notification_email(self).deliver
Here we are going to remove the .deliver and add .delay to the UserMailer
UserMailer.delay.notification_email(self)
Thats it! now the process will be added to the delayed_jobs table.
But we have to start the jobs,for that use the following command by entering into your application directory.
script/delayed_job
In staging add gem "daemons" bundle and run rails generate delayed_job.
RAILS_ENV=staging script/delayed_job start
In production add gem "daemons" bundle and run rails generate delayed_job.
RAILS_ENV=production script/delayed_job start
Add the following to your gem file.
gem 'delayed_job_active_record'
Run the bundle install command to install the gems.
The following command will create a table as it is required to have a jobs table for AR backend.
rails generate delayed_job:active_record
rake db:migrate
Then we can use .delay method to process any job in the background.
without delayed_job
UserMailer.notification_email(self).deliver
Here we are going to remove the .deliver and add .delay to the UserMailer
UserMailer.delay.notification_email(self)
Thats it! now the process will be added to the delayed_jobs table.
But we have to start the jobs,for that use the following command by entering into your application directory.
script/delayed_job
In staging add gem "daemons" bundle and run rails generate delayed_job.
RAILS_ENV=staging script/delayed_job start
In production add gem "daemons" bundle and run rails generate delayed_job.
RAILS_ENV=production script/delayed_job start
(203) 208-3081
Very informative post, thanks for sharing. Its really works on ruby on rails Development in India.
ReplyDelete