【问题标题】:Why does it get timed out when using nginx?为什么使用nginx时会超时?
【发布时间】:2014-06-04 20:29:58
【问题描述】:

我在 nginx 上运行 rails。然后我就有了一次向所有注册用户发送电子邮件的功能。
我确实运行了这个,但它出现了超时错误。我怎样才能解决这个问题?如何修改我的配置?

controller(有超过 10000 个用户,所以这意味着这个邮件程序重复了 10000 次)

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

然后它得到这个超时错误。

The connection has timed out
The server www.foofooexample.com is taking too long to respond.

这是我的 nginx 的配置文件

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;
.
.
.

【问题讨论】:

  • 这样的任务不应该在(等待的)网络请求中完成。您应该在后台执行此操作,有很多 gem 可以执行此操作,例如 sidekiq
  • @Tamer 感谢您的评论! sidekiq 会做我在控制器中所做的事情吗?它正在加载所有用户并向所有用户发送消息。

标签: ruby-on-rails ruby-on-rails-3 nginx


【解决方案1】:

正如上面提到的 cmets 之一,您希望使用诸如 sidekiq 之类的东西将其卸载到后台工作人员,它带有用于在后台发送电子邮件的 ActionMailer 扩展名。

安装后,而不是

CallMailer.call_email_to(user.email, subject, body).deliver

你会使用:

CallMailer.delay.call_email_to(user.email, subject, body)

另外,我建议您使用find_each 而不是each。这是因为each 会将所有User 对象加载到内存中,而find_each 将分批加载它们。有关示例用法,请参阅链接文档。

【讨论】:

    猜你喜欢
    • 2020-10-27
    • 2018-02-05
    • 2018-05-20
    • 2016-04-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-22
    • 1970-01-01
    相关资源
    最近更新 更多