jpw
jpw

Reputation: 19247

In rails 3.2 app, what is the best way to update a page when a background job is complete?

My rails 3.2 app has a report page that frequently takes 90+ seconds to render, so I run the report-builder as a delayed_job task then display a "wait a minute" view (with a refresh button) until the content is ready (there is a content_ready flag used in the view to display either the "wait message" or the report, as appropriate). In my prototype, the user has to refresh the page manually until the content_ready is set (and then the report view is rendered).

I'd like to have the page refresh automatically when content_ready is true.

What's the simplest way to accomplish this, given it's a rarely-hit page and thus an inefficient method, such as telling the user's browser to poll the server (or redraw the whole page) every 10 seconds, is just fine?

Upvotes: 0

Views: 338

Answers (2)

Dave S.
Dave S.

Reputation: 6419

I'd recommend the following strategy:

  • After creating the delayed job, pass the id of the job back to the browser immediately.
  • Expose an endpoint on your site that will return the status of the job.
  • Use jQuery + setTimeout or setInterval to hit the endpoint.
  • When the job is done, redirect the user to the appropriate page.

It's a pretty simple strategy, but it works.

Upvotes: 1

d_ethier
d_ethier

Reputation: 3911

Setup either a meta-refresh on the page or an ajax updater (if using jquery, see here) that checks a specific controller action that tells you whether the job is completed, or that the file exists in the location expected.

Or, and assuming you're using the delayed_job gem, check out the hooks that are provided as part of the job queuing execution. There is documentation on the gem's GH page.

Upvotes: 1

Related Questions