Hi everybody,
What's up!
Today I am going to discuss about the background tasks management in rails.
Suppose we have a UserMailer which sends email to the users once they have successfully completed the registration process. Something like this:
class UserMailer < ActionMailer::Base
default :from => "admin@example.com"
And in the controller,
def create
@user = User.new(params[:user])
if @user.save
UserMailer.registration_confirmation(@user).deliver
redirect_to root_url, :notice => "Signed up! Please check confirmation mail!"
else
render "sign_up"
end
end
After the user hits the "Create User" button, he has to wait for some time while their mail is being delivered.
During this process, rails is busy and can't respond to any other requests.
This is a very bad user experience. After all why should one has to wait for some time unnecessarily.
Now there are many gems and plugins available to handle the background jobs. I prefer to use "delayed_job" gem.
Firstly, because it stores the jobs queue in the database table. Hence, no memory leak or loss of data.
Secondly, it's very easy to implement.
So, let's see what are the steps involved:
1) add: gem 'delayed_job_active record' to the gemfile and run 'bundle install'.
2) Next, we need the jobs table so run the commands:
$ rails generate delayed_job:active_record
$ rake db:migrate
3) Next go to the controller and just replace the line
UserMailer.registration_confirmation(@user).deliver
with,
UserMailer.delay.registration_confirmation(@user)
4) $ rake jobs:work
Step 4 will look for any new job in the "delayed_jobs" table and execute it in the background.
This was easy. We can add many more features to the "delayed_jobs" gem for which I suggest you to checkout this Github Link.
Thanks!!
What's up!
Today I am going to discuss about the background tasks management in rails.
Suppose we have a UserMailer which sends email to the users once they have successfully completed the registration process. Something like this:
class UserMailer < ActionMailer::Base
default :from => "admin@example.com"
def registration_confirmation(user)
mail(:to => user.email, :subject => "Registration Confirmation")
end
endAnd in the controller,
def create
@user = User.new(params[:user])
if @user.save
UserMailer.registration_confirmation(@user).deliver
redirect_to root_url, :notice => "Signed up! Please check confirmation mail!"
else
render "sign_up"
end
end
After the user hits the "Create User" button, he has to wait for some time while their mail is being delivered.
During this process, rails is busy and can't respond to any other requests.
This is a very bad user experience. After all why should one has to wait for some time unnecessarily.
Now there are many gems and plugins available to handle the background jobs. I prefer to use "delayed_job" gem.
Firstly, because it stores the jobs queue in the database table. Hence, no memory leak or loss of data.
Secondly, it's very easy to implement.
So, let's see what are the steps involved:
1) add: gem 'delayed_job_active record' to the gemfile and run 'bundle install'.
2) Next, we need the jobs table so run the commands:
$ rails generate delayed_job:active_record
$ rake db:migrate
3) Next go to the controller and just replace the line
UserMailer.registration_confirmation(@user).deliver
with,
UserMailer.delay.registration_confirmation(@user)
4) $ rake jobs:work
Step 4 will look for any new job in the "delayed_jobs" table and execute it in the background.
This was easy. We can add many more features to the "delayed_jobs" gem for which I suggest you to checkout this Github Link.
Thanks!!
Where I must create "UserMailer" class?
ReplyDeleteHope this liknk might help:
ReplyDeletehttp://guides.rubyonrails.org/action_mailer_basics.html
It's a nice gem to handle background process.
ReplyDelete(At my case when I need to multiple emails to multiple recipients).
Just a minor correction in step-1
1) add: gem 'delayed_job_active record'
An underscore is missing "delayed_job_active_record"
Thanks man, You saved my lots of time & effort.
Unique blog having such a valuable content !!!....
ReplyDeletekars
ReplyDeletesinop
sakarya
ankara
çorum
KH3
sakarya
ReplyDeleteyalova
elazığ
van
kilis
8MSZR
görüntülüshow
ReplyDeleteücretli show
41F
Kırşehir Lojistik
ReplyDeleteHakkari Lojistik
Kars Lojistik
Konya Lojistik
Kilis Lojistik
US2
F4CEA
ReplyDeleteKastamonu Evden Eve Nakliyat
Erzincan Şehirler Arası Nakliyat
Elazığ Şehir İçi Nakliyat
Bitlis Parça Eşya Taşıma
Mardin Şehirler Arası Nakliyat
Ardahan Şehir İçi Nakliyat
Adıyaman Parça Eşya Taşıma
Çorum Şehir İçi Nakliyat
Afyon Lojistik
956D4
ReplyDeleteGümüşhane Evden Eve Nakliyat
Eskişehir Parça Eşya Taşıma
Diyarbakır Lojistik
Poloniex Güvenilir mi
Hakkari Evden Eve Nakliyat
Çerkezköy Televizyon Tamircisi
Çerkezköy Evden Eve Nakliyat
Burdur Parça Eşya Taşıma
Yalova Parça Eşya Taşıma
54C53
ReplyDeleteUrfa Evden Eve Nakliyat
Baby Doge Coin Hangi Borsada
Silivri Duşa Kabin Tamiri
Konya Şehir İçi Nakliyat
Sinop Lojistik
Adıyaman Şehir İçi Nakliyat
Isparta Lojistik
Kırklareli Evden Eve Nakliyat
Çerkezköy Oto Boya
C10A1
ReplyDeleteyabancı görüntülü sohbet uygulamaları
aydın canlı görüntülü sohbet siteleri
aksaray görüntülü sohbet kadınlarla
parasız görüntülü sohbet
Tekirdağ Mobil Sesli Sohbet
aksaray canli sohbet
Elazığ Mobil Sohbet Sitesi
kilis rastgele canlı sohbet
mobil sohbet sitesi
D8A89
ReplyDeleteTwitter Beğeni Hilesi
Binance Borsası Güvenilir mi
Binance Kimin
Bitcoin Madenciliği Nasıl Yapılır
Gate io Borsası Güvenilir mi
Coin Üretme Siteleri
Binance Borsası Güvenilir mi
Mexc Borsası Güvenilir mi
Bitcoin Kazanma
FD8EE
ReplyDeleteBitcoin Giriş Nasıl Yapılır
Likee App Beğeni Satın Al
Coin Madenciliği Nedir
Twitter Takipçi Satın Al
Binance Yaş Sınırı
Coin Üretme Siteleri
Coin Nasıl Alınır
Tesla Coin Hangi Borsada
Chat Gpt Coin Hangi Borsada
738B5
ReplyDeleteshiba
yearn
galagames
trezor suite
avax
arbitrum
looksrare
ledger live
safepal