开发者

Rails 3: Return large amount of data to user via API

开发者 https://www.devze.com 2023-03-15 17:46 出处:网络
My app has an API that users can request data. Sometimes that data takes time to process and is breaking my code.

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.


Tell the user that their data is being processed and will be available shortly. Youtube, Vimeo, Facebook, Twitter, they all do this.

0

精彩评论

暂无评论...
验证码 换一张
取 消