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

2019-05-25 07:19发布

问题:

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

回答1:

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.



回答2:

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