[Rails] How to show progress
Ezra Zygmuntowicz
ezra at yakimaherald.com
Sun Jan 1 23:33:16 GMT 2006
On Jan 1, 2006, at 1:07 PM, Gerry Shaw wrote:
> I'm looking for ideas on how you would implement a controller/view in
> Rails that needs to show the progress of a possibly 2-3 minute
> operation.
>
> For example the user wants to generate 100 reports that will take
> about
> 3 minutes. I would like to have a page that would indicate that the
> operation is taking place as well as showing progress as to how far
> along. With basic CGI you could render a line of text after each
> report
> was generated and as the page loaded over a few minutes all the
> operations would be completed. The user could cancel the operation by
> just stopping the page from being loaded (I think). This feels a bit
> hacky and I'm looking for a better design but I'm not too sure of one.
>
> The best I've come up with is the job is inserted into a database
> table
> and a seperate cron job ticks every minute looking for jobs in that
> table that actually runs the job. After the job is submitted the user
> is redirected to a job status page which reads the status from the
> database. For long operations this makes sense but for 3 minute
> operations the user is forced to wait ~30 seconds just for the cron
> job
> to tick, not to mention the site gets pinged every minute even when
> jobs
> might only exist every few days.
>
> How would you design this system? Jobs will take 30 - 300 seconds and
> be run a few times every hour. User feedback on progress is
> important.
>
> --
For something like this you might be better off looking at a drb
solution. You could have a simple drb server running that would
process the jobs when your user clicks go. This way you divorce the
long running task from the request-response cycle where it could time
out or have other problems. Then you have your drb server publish a
variable with the status of the long running job. And your user would
have see a web page with a process indicator that gets the status
from the drb server through an ajax request to your rails app. Once
your ajax requests return the finished flag from the drb server you
can redirect the user to a new page with the results.
You can have your web page do a periodically_call_remote to get the
status every 5 or 10 seconds so the user sees the progress. Here are
a few drb tutorial links:
http://segment7.net/projects/ruby/drb/introduction.html
http://ian.blenke.com/drb
http://www.chadfowler.com/ruby/drb.html
Hope that helps
-Ezra Zygmuntowicz
WebMaster
Yakima Herald-Republic Newspaper
ezra at yakima-herald.com
509-577-7732
More information about the Rails
mailing list