Rails 4: How to send mail to a separate stream?

I have an application that sometimes needs to send an email to all users when the administrator does something. This works fine, but when there are many users, the admin page will wait until all the mail has been sent, which is undesirable.

To mitigate this, I tried sending the email to a new stream:

t = Thread.new do
  User.all.each do |user|
    Mailer.email(user).deliver
  end
end
at_exit{ t.join }

This worked fine, but then in my test suite I can’t check if email sending works:

# This test now fails with the new Thread above
test "admin action should send email blast" do
  assert_difference("ActionMailer::Base.deliveries.count", User.count) do
    post :action
  end
end

So my questions are:

  • Is this method the best way to send email to a new stream? Or is there a gem that handles such interaction?
  • , , ? , ?
+4
4

4.2 , , ActiveJob, , , , , " 8 ".

, , backend , , ActiveJob

, sidekiq, , , , , , sucker punch , , , , , .

, rails guide , , ​​ , , , .

+2

, . , - , sidekiq .. sidekiq Redis db .

Rails 4.2 , , - , , quie yiu

module YourApp
 class Application < Rails::Application
  # Be sure to have the adapter gem in your Gemfile
  # and follow the adapter specific installation
  # and deployment instructions.
  config.active_job.queue_adapter = :sidekiq
 end
end
+2

- .

https://github.com/collectiveidea/delayed_job

, - .

- sidekiq, sidekiq gem application.rb

config.active_job.queue_adapter = :sidekiq
0

Source: https://habr.com/ru/post/1585016/


All Articles