Rails 3:通过API向用户返回大量数据 [英] Rails 3: Return large amount of data to user via API

查看:71
本文介绍了Rails 3:通过API向用户返回大量数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序具有用户可以请求数据的API。有时候,数据需要花费一些时间来处理并破坏我的代码。 。如果用户提出要求,我需要给他一个答案。即使我在后台处理数据,呼叫仍然需要等待,直到作业返回。



对此有什么解决方案?我不确定该怎么做。



谢谢

解决方案

Heroku的 30秒超时,这就是您的请求失败的原因(可能是H12或H13在您的heroku日志中)。



有三种方法可以解决此问题。


  1. 通过发送空白数据来保持连接打开。
    您需要在前30秒内以及之后的每55秒内做出响应。使用之间的时间来处理数据。发送空格不应影响浏览器读取响应的能力。


  2. 回调
    让用户提供初始请求中的回调URL。完成数据处理后,请在响应中输入回调网址。


  3. 轮询
    如Codeglot所述,您可以为用户提供密钥。要检查他们的请求,他们可以使用该密钥ping您的服务器。



My app has an API that users can request data. Sometimes that data takes time to process and is breaking my code.

I need a solution for this and I was thinking in using delayed_job but I'm not sure how this works. If the user makes a request, I need to give him an answer. Even if I process the data in background, the call still needs to wait until the job returns.

What is the solution for this? I am not sure how to do it.

Thanks

解决方案

Heroku has a 30 second timeout, which is why your requests are failing (Probably H12 or H13 in your heroku logs).

There are three methods to work around this.

  1. Keep the connection open by sending blank data. You'll need to respond within the first 30 seconds and every 55 seconds after that. Use the time in between to process the data. Sending spaces should not affect the ability of the browser to read the response.

  2. Callback Have the user provide a callback URL in the initial request. When you finish processing the data, hit the callback url with your response.

  3. Polling As suggested by Codeglot, you can provide the user with a key. To check on their request, they can ping your server with that key.

这篇关于Rails 3:通过API向用户返回大量数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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