Rails 4 - Mailer deliver_later does not do what I expect, blocks the user interface

I have a rails application in which I have a function that sends a lot of emails. I would like to do this asynchronously , and I thought the deliver_later method would do this. I currently have some delay when the user clicks submit until the form is submitted, which leads to a bad user experience (this is a pretty simple form). My implementation is as follows:

 def create respond_to do |format| if @competition.save [...] send_notification_to_team_members end end def send_notification_to_team_members @team.members.each do |member| unless member.user.eql?(current_user) Mailer.deliver_new_competition_notification(member.user, @competition).deliver_later end end end 

Currently, it takes ~ 4 seconds to complete the action. I also tried:

 Mailer.deliver_new_competition_notification(member.user, @competition).deliver_later(wait: 1.minute) 

Then it will take even more time - I would rate ~ 1 minute.

So, I am using deliver_later incorrectly, or a method that does not fulfill what I expect. In this case, is there any other way that I can use to improve my work?

+6
source share
1 answer

deliver_later uses ActiveJob to provide asynchronous execution.

However, ActiveJob does not provide asynchrony - it is a merging api layer that can be executed by many backends. By default, only one line is used, it is not async.

To get asynchronous use, you need to choose an asynchronous backend. You can configure the backend in the configuration of your application.

  config.active_job.queue_adapter = ... 

In most cases, most adapters require matching pearls (e.g. delayed_job, sidekiq, sucker_punch), which may also have their own dependencies (e.g. sidekiq requires you to use redis.

+14
source

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


All Articles