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 :)


  1. Browsing around for Timeout stuff with Rails I stumbled across this.

    I have been developing a project with some fairly long running processes and found BackgrounDRb to be the best solution. I'm having to do SNMP scanning which can take a while ;)

    I obviously don't know your system or requirements as well as you but BackgrounDRb might well be something to look at. But that would probably require storing the reports either on the filesystem or database then when complete sending the browser to a URL which dumps the reports from either FS or DB. Or you could store the reports in an array in the BackgrounDRb worker and then just request the array or part of it somehow.

    Not sure how likely it will be that I'll pop back, but feel free to email me if you want.

    Have to say, using Ajax is a pretty nifty solution to this type of problem.

  2. Hi Geoff. I tried BackgroundDRb a couple of times, but always ran into the problem that you can't generate lots of model variables then pass them back to the live Rails app. In order to make it work, I would have had to do one of the things you suggest. Actually... as I am typing this, the possibility of doing a cached version of each report in the database seems feasible.... Oh dear, looks like I might have to reinstall BackgroundDRb again!

  3. You could send the report request to a server side script that generates a PDF based report which also updates a database with it's progress. The browser could then display the progress and redirect to the file once complete.

    The user could even carry on with there business around FreeMIS and be emailed the report when done.

    How is FreeMIS coming along these days? I've not seen an update on the main development site for a year now!

  4. Hi Andrew. Generating a PDF is a nice idea.

    FreeMIS has been running smoothly at my school this year (once we sorted out decent hosting!). From a personal perspective, FreeMIS already does what I need it to do. The bigger picture of producing a comprehensive alternative to the commercial MIS's can't realistically happen unless I get some help on the development side, and that has not been forthcoming. My fault partly, as I have not made much effort to court developers. Maybe I'll get things together over the Summer holidays :)

  5. i have a problem with ruby ruporting tool i was unable to generate reports in pdf
    it has been saying that # slice is missing hw could i resolve this


Post a Comment

Popular posts from this blog

ActiVote Walkthrough Part 1

Learning styles are a myth and I am not an auditory learner

The actor's life for me