Ruby on Rails - avoiding browser timeout during long tasks

Apologies to those of my readers who are not web developers - what follows may well make no sense to you whatsoever!

I am so please with myself! For nigh on a year now, I have been aware of a potential problem with FreeMIS (the Ruby-on-Rails based  management information system for schools that I have developed) and today I solved that problem.

The problem is that the printing of a full set of reports for all the pupils in a year group is a huge task, which can take Rails anything up to 130 seconds to complete. By the time it has done this, the browser and or web server have got bored and given up waiting. I have played around with the configuration of Apache and FastCGI, and tried using mongrel instead, but none managed to fix the problem 100%.

In PHP, it would be easily solved by purging the data produced out to the browser repeatedly through the task, but Rails does not have this option.

Last night as I was drifting off to sleep I realised that I could solve the problem by doing the reports one pupil at a time!

Obvious, you might think, but the system needs a "one-click" solution for printing all the reports for a register class - it would not be acceptable to require user input to print out each individual pupil's reports.

What I realised last night was that if I did the printing of reports via Ajax and RJS, then I get exactly what I wanted.

I start by submitting a form via Ajax to print the reports for a register class.

The action renders the reports for the first pupil in the register class at the bottom of the relevant div, then puts a hidden variable into the form, with the id of the next pupil in the register class. It then tells the browser to "click" on the submit button of the form. All the reports for the register class then come out, pupil by pupil, and no individual browser<->server cycle lasts more than a couple of seconds, so no chance of timing out. I can even put a percentage progress bar at the top of the page. Happy happy joy joy :)

Popular posts from this blog

Assessment Through The Looking Glass

And what if we don't achieve our dreams?

Classroom Rules: Imagine it's the last lesson of the year.