Last active
June 5, 2025 15:20
-
Star
(149)
You must be signed in to star a gist -
Fork
(36)
You must be signed in to fork a gist
Revisions
-
maxivak revised this gist
Dec 1, 2015 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -140,7 +140,7 @@ To change queue name for ActionMailer use this config: config.active_job.queue_name_delimiter = "_" ``` This will use queues named `:mysite_mailers`, etc. !!! important. You may need to include new queue names in sidekiq.yml file: ``` -
maxivak revised this gist
Dec 1, 2015 . 1 changed file with 3 additions and 3 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -136,7 +136,7 @@ To change queue name for ActionMailer use this config: config.active_job.queue_adapter = :sidekiq config.active_job.queue_name_prefix = "mysite" config.active_job.queue_name_delimiter = "_" ``` @@ -152,8 +152,8 @@ This will use queues named `:mysite_development_mailers`, etc. - default - mailers - mysite_default - mysite_mailers ``` -
maxivak revised this gist
Dec 1, 2015 . 1 changed file with 36 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -126,6 +126,42 @@ end ``` ## Sidekiq and ActionMailer (ActionJob) By default, jobs to deliver emails will be placed in queue named :mailers. To change queue name for ActionMailer use this config: ``` # config/environments/development.rb config.active_job.queue_adapter = :sidekiq config.active_job.queue_name_prefix = "mysite_#{Rails.env}" config.active_job.queue_name_delimiter = "_" ``` This will use queues named `:mysite_development_mailers`, etc. !!! important. You may need to include new queue names in sidekiq.yml file: ``` # config/sidekiq.yml --- :concurrency: 1 :queues: - default - mailers - mysite_development_default - mysite_development_mailers ``` ## Sidekiq and Devise Read this: https://github.com/mperham/sidekiq/wiki/Devise ## Run Sidekiq ``` @@ -137,8 +173,6 @@ bundle exec sidekiq --environment development -C config/sidekiq.yml Use god for monitoring and running sidekiq automatically: https://gist.github.com/maxivak/05847dc7f558d5ef282e ## RSpec tests -
maxivak revised this gist
Nov 11, 2015 . 1 changed file with 3 additions and 3 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -112,16 +112,16 @@ config/sidekiq.yml: ``` Specify Redis namespace for different environments: ``` # config/initializers/sidekiq.rb Sidekiq.configure_server do |config| config.redis = { url: 'redis://localhost:6379/0', namespace: "app3_sidekiq_#{Rails.env}" } end Sidekiq.configure_client do |config| config.redis = { url: 'redis://localhost:6379/0', namespace: "app3_sidekiq_#{Rails.env}" } end ``` -
maxivak revised this gist
Nov 11, 2015 . 1 changed file with 30 additions and 11 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -61,15 +61,21 @@ Redis provides data storage for Sidekiq. It holds all the job data along with ru ### Configure Sidekiq To make `#deliver_later` work we need to tell ActiveJob to use Sidekiq. As long as Active Job is setup to use Sidekiq we can use #deliver_later. ``` # config/initializers/active_job.rb config.active_job.queue_adapter = :sidekiq ``` Environment file: ``` # config/environments/development.rb @@ -134,7 +140,13 @@ Use god for monitoring and running sidekiq automatically: https://gist.github.co ## Sidekiq and Devise Read this: https://github.com/mperham/sidekiq/wiki/Devise ## RSpec tests ### RSpec tests for ActionMailer In these tests we do not use Sidekiq. test environment: ``` @@ -143,7 +155,7 @@ test environment: Rails.application.configure do ... config.active_job.queue_adapter = :test config.action_mailer.perform_deliveries = true config.action_mailer.delivery_method = :test @@ -179,6 +191,13 @@ test environment: Rails.application.configure do ... config.active_job.queue_adapter = :sidekiq config.action_mailer.perform_deliveries = true config.action_mailer.delivery_method = :test config.action_mailer.raise_delivery_errors = true ``` @@ -198,12 +217,12 @@ Test that email was enqueued: RSpec.describe "Test sending email with sidekiq", :type => :request do it 'send email to sidekiq' do user = User.first expect{ UsersMailer.welcome_email(user.id).deliver_later }.to change( Sidekiq::Worker.jobs, :size ).by(1) end -
maxivak revised this gist
Nov 11, 2015 . 1 changed file with 18 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -134,7 +134,24 @@ Use god for monitoring and running sidekiq automatically: https://gist.github.co ## Sidekiq and Devise Read this: https://github.com/mperham/sidekiq/wiki/Devise ## RSpec tests for ActionMailer test environment: ``` # config/environments/test.rb Rails.application.configure do ... #config.active_job.queue_adapter = :test config.action_mailer.perform_deliveries = true config.action_mailer.delivery_method = :test config.action_mailer.raise_delivery_errors = true ``` Test that deliver_later method was called: ``` -
maxivak revised this gist
Nov 11, 2015 . 1 changed file with 20 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -134,8 +134,27 @@ Use god for monitoring and running sidekiq automatically: https://gist.github.co ## Sidekiq and Devise Read this: https://github.com/mperham/sidekiq/wiki/Devise ## RSpec tests Test that deliver_later method was called: ``` user = User.first # message_delivery = instance_double(ActionMailer::MessageDelivery) expect(UsersMailer).to receive(:welcome_email).with(user.id).and_return(message_delivery) expect(message_delivery).to receive(:deliver_later) # mail = UsersMailer.welcome_email(user.email) mail.deliver_later ``` ### RSpec tests for Sidekiq test environment: ``` -
maxivak revised this gist
Nov 11, 2015 . 1 changed file with 6 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -27,7 +27,12 @@ end ``` Views for email: ``` app/views/users_mailer/welcome_email.html.erb - HTML version app/views/users_mailer/welcome_email.text.erb - TEXT version ``` Send email: ``` -
maxivak revised this gist
Nov 11, 2015 . 1 changed file with 1 addition and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -27,6 +27,7 @@ end ``` Views for email: app/views/users_mailer/welcome_email.html.erb and app/views/users_mailer/welcome_email.text.erb Send email: ``` -
maxivak revised this gist
Nov 11, 2015 . 1 changed file with 26 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -65,7 +65,23 @@ ActiveJob::Base.queue_adapter = :sidekiq As long as Active Job is setup to use Sidekiq we can use #deliver_later: ``` # config/environments/development.rb Rails.application.configure do ... config.active_job.queue_adapter = :sidekiq config.action_mailer.perform_deliveries = true config.action_mailer.raise_delivery_errors = true config.action_mailer.delivery_method = :smtp config.action_mailer.smtp_settings = { ... } end ``` Read more about ActionJob and Sidekiq: https://github.com/mperham/sidekiq/wiki/Active-Job @@ -115,6 +131,15 @@ Read this: https://github.com/mperham/sidekiq/wiki/Devise ## RSpec tests for Sidekiq test environment: ``` # config/environments/test.rb Rails.application.configure do ... ``` spec/rails_helper.rb: ``` # sidekiq -
maxivak revised this gist
Nov 11, 2015 . 1 changed file with 26 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -115,5 +115,31 @@ Read this: https://github.com/mperham/sidekiq/wiki/Devise ## RSpec tests for Sidekiq spec/rails_helper.rb: ``` # sidekiq require 'sidekiq/testing' Sidekiq::Testing.fake! # by default it is fake ``` User Sidekiq::Worker.jobs.size to see the number of jobs in the queue. Test that email was enqueued: ``` RSpec.describe "Test sending email with sidekiq", :type => :request do it 'send email to sidekiq' do expect{ user = User.first mail = UsersMailer.welcome_email(user.id) mail.deliver_later }.to change( Sidekiq::Worker.jobs, :size).by(1) end end ``` -
maxivak revised this gist
Nov 11, 2015 . 1 changed file with 23 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -63,10 +63,27 @@ ActiveJob::Base.queue_adapter = :sidekiq ``` As long as Active Job is setup to use Sidekiq we can use #deliver_later: ``` UsersMailer.welcome_email(user_id).deliver_later ``` Read more about ActionJob and Sidekiq: https://github.com/mperham/sidekiq/wiki/Active-Job ## Configure Sidekiq config/sidekiq.yml: ``` --- :concurrency: 1 :queues: - default - mailers ``` Specify queue name for different environments: ``` # config/initializers/sidekiq.rb @@ -94,3 +111,9 @@ Use god for monitoring and running sidekiq automatically: https://gist.github.co ## Sidekiq and Devise Read this: https://github.com/mperham/sidekiq/wiki/Devise ## RSpec tests for Sidekiq -
maxivak revised this gist
Nov 11, 2015 . 1 changed file with 21 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -55,10 +55,31 @@ Redis provides data storage for Sidekiq. It holds all the job data along with ru ### Configure Sidekiq To make work mailer.deliver_later we need to tell ActiveJob to use Sidekiq: ``` # config/initializers/active_job.rb ActiveJob::Base.queue_adapter = :sidekiq ``` Read more about ActionJob and Sidekiq: https://github.com/mperham/sidekiq/wiki/Active-Job ## Configure Sidekiq Specify queue name for different environments: ``` # config/initializers/sidekiq.rb Sidekiq.configure_server do |config| config.redis = { url: 'redis://localhost:6379/0', namespace: "mysite_sidekiq_#{Rails.env}" } end Sidekiq.configure_client do |config| config.redis = { url: 'redis://localhost:6379/0', namespace: "mysite_sidekiq_#{Rails.env}" } end ``` ## Run Sidekiq -
maxivak revised this gist
Nov 11, 2015 . 1 changed file with 15 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -47,6 +47,17 @@ Gemfile: gem 'sidekiq' ``` ### Install Redis Redis provides data storage for Sidekiq. It holds all the job data along with runtime and historical data ### Configure Sidekiq ## Run Sidekiq @@ -58,3 +69,7 @@ bundle exec sidekiq --environment development -C config/sidekiq.yml ### God + Sidekiq Use god for monitoring and running sidekiq automatically: https://gist.github.com/maxivak/05847dc7f558d5ef282e ## Sidekiq and Devise Read this: https://github.com/mperham/sidekiq/wiki/Devise -
maxivak revised this gist
Nov 11, 2015 . No changes.There are no files selected for viewing
-
maxivak revised this gist
Nov 11, 2015 . 1 changed file with 43 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -3,8 +3,50 @@ Send email asynchroniously using Sidekiq. ## ActionMailer Create your mailer us usual: ``` # app/mailers/users_mailer.rb class UsersMailer < ActionMailer::Base def welcome_email(user_id) @user = User.find(user_id) mail( :to => @user.email, :subject => "Welcome" ) do |format| format.text format.html end end end ``` Send email: ``` user = User.find(1) mail = UsersMailer.welcome_email(user.id) #mail.deliver_now mail.deliver_later ``` ## Sidekiq Gemfile: ``` gem 'sidekiq' ``` ## Run Sidekiq @@ -15,4 +57,4 @@ bundle exec sidekiq --environment development -C config/sidekiq.yml ### God + Sidekiq Use god for monitoring and running sidekiq automatically: https://gist.github.com/maxivak/05847dc7f558d5ef282e -
maxivak created this gist
Nov 11, 2015 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,18 @@ # Sending emails with ActionMailer and Sidekiq Send email asynchroniously using Sidekiq. ## Sidekiq ## Run Sidekiq ``` bundle exec sidekiq --environment development -C config/sidekiq.yml ``` ### God + Sidekiq Use god for monitoring and running sidekiq automatically: https://gist.github.com/maxivak/05847dc7f558d5ef282e