Tuesday, November 18, 2008

Rails periodically_call_remote issue

Here is an interesting problem that I ran into today. A few guys where working on a feature that used a progress bar to give a user feedback on the status of a background task. In order to do this, they used the Rails prototype helper periodically_call_remote method however they noticed that multiple requests were being fired after an interval had passed. I wasn't sure what was happening so I started to read the source code and as of the time of the writing of this post periodically_call_remote creates a PeriodicExecuter but does not ever stop it. According to the Prototype website: "Once launched, a PeriodicalExecuter triggers indefinitely, until the page unloads (which browsers usually take as an opportunity to clear all intervals and timers) or the executer is manually stopped"
e.g. of stopping manually:
new PeriodicalExecuter(function(pe) {
if (!confirm('Want me to annoy you again later?'))
pe.stop();
}, 5);

Now what happens is that every time the application rendered the ajax call, it spawned a new executor, which ends up hanging around for a while. This would not be a problem if the browser "unloaded" itself every once and a while so here is some advice... if you need write a feature that requires rendering a periodically_call_remote multiple times then you are probably doing it wrong.

No comments: