views:

128

answers:

1

Hi, currently I am running a Rails app on Heroku, and everything is working great with exception of generating PDF documents that sometimes contain thousands of records. Heroku has a built-in timeout of 30 seconds, so if the request takes more than 30 seconds, it's abandoned.

That's fine, since they offer delayed_job support built-in. However, all of the PDF's i generate follow a typical restful pattern. For instance, a request to "/posts.pdf" generates a pdf (using PRAWN and PRAWNTO) and it's delivered to the browser.

So my basic question is, how do I create dynamically generated PDF's with delayed_job while maintaining the basic RESTful patterns Rail's so conveniently provides. Thanks.

+3  A: 

You could do something like:

  1. Send a request to generate the pdf: POST /generate_new_pdf
  2. Have that action return the ID of the new pdf before it's created
  3. Poll the endpoint for that resource ID until it's done (returning 202's in the meantime): GET /pdfs/:id
Ryan Angilly
Good answer (+1), but there is a slight error: The response to the POST should return 202 Accepted with the location header pointing to the resource to be polled. I would then return 404 for GET /pdfs/:id until the PDF actually exists.
Jan Algermissen
Doing things asynchronously is a good way :)Maybe he should returns the entire url ?
dzen