Reputation: 2753
I'm running rails on nginx. Then I have the function to deliver emails to all regiestered users at once.
I did ran this but it got timed out error. How can I fix this? How can I modify my configuration?
controller (There are over 10000 users so it means this mailer procedure repeats for 10000 times)
users.each do |user|
if user.nomail.blank?
CallMailer.call_email_to(user.email, subject, body).deliver
sent_to_count = sent_to_count + 1
end
end
Then it gets this timed out error.
The connection has timed out
The server www.foofooexample.com is taking too long to respond.
Here's my nginx's conf
etc/nginx/conf.d/foo.conf
server {
listen 80;
server_name foofooexample.com;
root /var/www/html/foo/public;
client_max_body_size 5M;
keepalive_timeout 1200;
proxy_connect_timeout 1200;
proxy_read_timeout 1200;
proxy_send_timeout 1200;
.
.
.
Upvotes: 0
Views: 261
Reputation:
As one of the comments mentioned, you want to offload this to a background worker using something such as sidekiq
, which comes with an ActionMailer
extension for sending email in the background.
After installing, instead of
CallMailer.call_email_to(user.email, subject, body).deliver
You would use:
CallMailer.delay.call_email_to(user.email, subject, body)
Also, I'd advise you use find_each
instead of each
. This is because each
will load all of your User
objects into memory, whereas find_each
will load them in batches. See the linked documentation for example usage.
Upvotes: 1