Long computations in the controller

If your controller is doing something very complicated that takes some time, for example heavy database changes, you may want to give to the user some feedback that work is being done. If your task is really long, you may also avoid worrying about browser timeout.

Here are some ideas.

Solution #1. Start creating the page in the controller

A quick and dirty solution (with a PHP-like mindset) is to send some HTML from the controller, so to start creating the page with no delay. You can do this with $c->write; this will also send the HTTP headers. If your computation is long, you may also want to send periodically some more white space to avoid a browser timeout. Your view will then complete the page later (and then you can also have some JavaScript to delete what you sent in the first part).

This solution is quite ugly, though. It breaks the MVC paradigm.

Solution #2. Create an interim page

This is a more elegant version of the idea above. You create an additional page to be shown before the "real" one. The first is just an HTML page with some kind of "loading" message; using AJAX, it then loads the results of the computation. This does not prevents browser timeouts, but at least gives something cleanly to the user before the job is completed.

Solution #3. Create a job queue

This is the most satisfactory, but also complex, solution. Your controller will launch a background job doing the real work; the result, when ready, will be available somewhere e.g. in a temporary file. Then, your client page will poll this resource until it is ready (with an HTTP refresh, or AJAX). [this is just a stub. Please expand]

My tags:
Popular tags:
Powered by Catalyst
Powered by MojoMojo Hosted by Shadowcat - Managed by Nordaaker