From 14328dedbebad12e6a7f1e5e873dda09341b5d59 Mon Sep 17 00:00:00 2001 From: Scott Miller Date: Fri, 18 Mar 2016 08:01:48 -0600 Subject: [PATCH 1/2] Convert mailers to deliver_later syntax, stub application.rb for background worker --- app/controllers/admin_controller.rb | 2 +- app/controllers/posts_controller.rb | 2 +- app/controllers/topics_controller.rb | 2 +- config/application.rb | 4 ++++ lib/email_processor.rb | 2 +- 5 files changed, 8 insertions(+), 4 deletions(-) diff --git a/app/controllers/admin_controller.rb b/app/controllers/admin_controller.rb index d17f63a48..2075d437c 100644 --- a/app/controllers/admin_controller.rb +++ b/app/controllers/admin_controller.rb @@ -147,7 +147,7 @@ def create_ticket :screenshots => params[:topic][:screenshots]) # Send email - UserMailer.new_user(@user, @token).deliver_now + UserMailer.new_user(@user, @token).deliver_later # track event in GA @tracker.event(category: 'Request', action: 'Post', label: 'New Topic') diff --git a/app/controllers/posts_controller.rb b/app/controllers/posts_controller.rb index c3fabe8ab..8a2c55dc4 100644 --- a/app/controllers/posts_controller.rb +++ b/app/controllers/posts_controller.rb @@ -178,7 +178,7 @@ def send_message end I18n.with_locale(email_locale) do - TopicMailer.new_ticket(@post.topic).deliver_now if @topic.private? + TopicMailer.new_ticket(@post.topic).deliver_later if @topic.private? end else logger.info("reply is not from admin, don't email") #might want to cchange this if we want admin notification emails diff --git a/app/controllers/topics_controller.rb b/app/controllers/topics_controller.rb index 846c02acc..0fee9fa16 100644 --- a/app/controllers/topics_controller.rb +++ b/app/controllers/topics_controller.rb @@ -183,7 +183,7 @@ def create :screenshots => params[:topic][:screenshots]) if built_user == true && !user_signed_in? - UserMailer.new_user(@user, @token).deliver_now + UserMailer.new_user(@user, @token).deliver_later end # track event in GA diff --git a/config/application.rb b/config/application.rb index 045b73e4f..e670aa7f0 100644 --- a/config/application.rb +++ b/config/application.rb @@ -26,5 +26,9 @@ class Application < Rails::Application # Do not swallow errors in after_commit/after_rollback callbacks. config.active_record.raise_in_transactional_callbacks = false + # We are using active_job and currently the inline backend. You may change this if + # you want a more robust solution. The queue is used for emails. + # config.active_job.queue_adapter = your_adapter_here + end end diff --git a/lib/email_processor.rb b/lib/email_processor.rb index 13bc48a53..ca89854a5 100644 --- a/lib/email_processor.rb +++ b/lib/email_processor.rb @@ -72,7 +72,7 @@ def create_user @user.name = @email.from[:name].blank? ? @email.from[:token] : @email.from[:name] @user.password = User.create_password if @user.save - UserMailer.new_user(@user, @token).deliver_now + UserMailer.new_user(@user, @token).deliver_later end end From 075029e1bfa3a7c91ef327563ffeb840d43e280b Mon Sep 17 00:00:00 2001 From: Scott Miller Date: Mon, 18 Apr 2016 10:43:43 -0600 Subject: [PATCH 2/2] Add suckerpunch, in memory queue --- Gemfile | 1 + Gemfile.lock | 3 +++ config/application.rb | 2 +- config/initializers/sucker_punch.rb | 3 +++ test/test_helper.rb | 4 ++++ 5 files changed, 12 insertions(+), 1 deletion(-) create mode 100644 config/initializers/sucker_punch.rb diff --git a/Gemfile b/Gemfile index b84afa1c0..b7736fa8d 100644 --- a/Gemfile +++ b/Gemfile @@ -34,6 +34,7 @@ gem 'ranked-model' gem 'staccato' gem "rails-settings-cached" +gem 'sucker_punch', '~> 2.0' # Auth Gems gem 'devise' diff --git a/Gemfile.lock b/Gemfile.lock index 398fa8d34..5c8bb27f8 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -422,6 +422,8 @@ GEM activesupport (>= 4.0) sprockets (>= 3.0.0) staccato (0.4.5) + sucker_punch (2.0.1) + concurrent-ruby (~> 1.0.0) temple (0.7.6) terminal-table (1.5.2) thor (0.19.1) @@ -540,6 +542,7 @@ DEPENDENCIES shoulda spring (~> 1.4.0) staccato + sucker_punch (~> 2.0) timecop trix turbolinks diff --git a/config/application.rb b/config/application.rb index e7fba96e7..a5663e055 100644 --- a/config/application.rb +++ b/config/application.rb @@ -28,7 +28,7 @@ class Application < Rails::Application # We are using active_job and currently the inline backend. You may change this if # you want a more robust solution. The queue is used for emails. - # config.active_job.queue_adapter = your_adapter_here + config.active_job.queue_adapter = :sucker_punch end end diff --git a/config/initializers/sucker_punch.rb b/config/initializers/sucker_punch.rb new file mode 100644 index 000000000..f41fb3159 --- /dev/null +++ b/config/initializers/sucker_punch.rb @@ -0,0 +1,3 @@ +# config/initializers/sucker_punch.rb + +require 'sucker_punch/async_syntax' diff --git a/test/test_helper.rb b/test/test_helper.rb index 0f461a7b7..91097180f 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -4,6 +4,10 @@ require File.expand_path('../../config/environment', __FILE__) require 'rails/test_help' +# Requiring this library causes your jobs to run everything inline. So a call to the following +# will actually be SYNCHRONOUS +require 'sucker_punch/testing/inline' + class ActiveSupport::TestCase # Setup all fixtures in test/fixtures/*.yml for all tests in alphabetical order. fixtures :all