ActionMailer Execution Timeout

When I try to send an email to a user to resell the password, I get an error message at startup. Other mail functions work, so I know that the configuration settings are correct. The headline reads: "Timeout :: Password error resetsController # create"

Here is the password for resets_controller:

def create @user = User.find_by_email(params[:email]) if @user User.deliver_password_reset_instructions(@user.id) flash[:notice] = "Instructions to reset your password have been emailed to you. " + "Please check your email." redirect_to '/' else flash[:notice] = "No user was found with that email address" render :action => :new end end 

Here is the method inside User.rb

 def self.deliver_password_reset_instructions(user_id) user = User.find(user_id) user.reset_perishable_token! Emailer.deliver_password_reset_instructions(user) end 

Finally, here is the actual method inside emailer.rb:

 default_url_options[:host] = "http://0.0.0.0:3000" #development def password_reset_instructions(user) @subject = "Application Password Reset" @from = ' Notice@myApp.com ' @recipients = user.email @sent_on = Time.now @body["edit_password_reset_url"] = edit_password_reset_url(user.perishable_token) @headers["X-SMTPAPI"] = "{\"category\" : \"Password Recovery\"}"#send grid category header end 

Why does the error message mention "Password" causing a timeout :: error

+4
source share
1 answer

Sending email (or other lengthy processes) from the request flow of the primary controller is not a good idea. Sending an e-mail message can be for various reasons that are not under your control (for example, the outgoing e-mail delivery server does not work), and you do not want your application server and users to suffer from this.

The best approach is to use a queuing mechanism, such as a Delayed Job (DJ), to queue these email tasks and process them outside of your controller threads.

See https://github.com/collectiveidea/delayed_job

Integrating this (or another queuing system) into your rails application is pretty straightforward. Rails 4 is said to have built queue services (which I haven't used yet) http://blog.remarkablelabs.com/2012/12/asynchronous-action-mailer-rails-4-countdown-to-2013 .

For example, if you use DJ in your application, the new code will look below

 def self.deliver_password_reset_instructions(user_id) user = User.find(user_id) user.reset_perishable_token! # this is the only line that changes Emailer.delay.deliver_password_reset_instructions(user) end 

Jobs are stored in the database and rechecked when errors such as timeouts occur.

You can learn more about DJ on the github page.

+1
source

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


All Articles