防止长请求请求超时 [英] Prevent request timeout with long requests

查看:122
本文介绍了防止长请求请求超时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我在Heroku上有一个Rails控制器,用于发送电子邮件,并向用户回复发送给哪个电子邮件地址的一些信息。 >尽管只有少量(〜40个)电子邮件需要发送,但当有更多的电子邮件发送出去时(例如> 40),请求会超时。



Heroku在其指南中声明,请求必须在30秒内响应至少一个字节: https://devcenter.heroku.com/articles/request-timeout



尽管我知道这不是实现这一目标的最佳方式,如果这是一个PHP应用程序,我可以做一个 echo

code>在进入循环之前,然后在每次迭代中保留 echo 一些东西。如何在rails中实现类似的功能?

解决方案

最好的办法是在发送回应之前不要做邮件。首先将工作添加到Heroku的许多可用工作队列中,然后踢到显示工作进度并定期更新自己的监控页面,将会有更好的运气。如果您试图避免使用其中一种队列服务,出于预算的原因,您可以使用新线程而不是队列来完成同样的任务。无论哪种方式,这种技术会更好地扩展,以及能够更容易地从失败中恢复。



您似乎已经知道您提出的解决方案不是理想的解决方案,我也会尽力回答你确切的问题。您可能可以使HTTP流式传输为此工作。我建议您查看 http://railscasts.com/episodes/266-http-streaming


I have a Rails Controller on Heroku where I send emails in a loop, and respond to the user with some information on which email address the emails were sent to.

While this works when only a few (~40) emails need to be sent out, the request times out when more there are more than just a few emails to be sent out (e.g. > 40).

Heroku states in their guides that requests must respond with at least one byte within 30 seconds: https://devcenter.heroku.com/articles/request-timeout

While I know this is not the best way to achieve this, I'm currently trying to figure out how to do this in Ruby.

If this were a PHP app, I could do an echo before entering the loop, and then keep echoing something in every iteration. How do I achieve something similar in rails?

解决方案

Your best bet is to not do the mailing before sending the response back. You will have better luck first adding the job to one of Heroku's many available worker queues, then kicking to a monitoring page that displays the job progress and updates itself periodically. If you are trying to avoid using one of those queue services, for budget reasons, you may be able to accomplish the same thing using a new thread, instead of a queue. Either way, this technique will scale better, as well as being able to recover from failure more easily as well.

As you appear to already know that your proposed solution is not the ideal solution, I will also try to answer your exact question. You may be able to make HTTP streaming work for this. I would recommend checking out http://railscasts.com/episodes/266-http-streaming.

这篇关于防止长请求请求超时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆