views:

34

answers:

3

I have a need to send alerts to a web-based monitoring system written in RoR. The brute force solution is to frequently poll a lightweight controller with javascript. Naturally, the downside is that in order to get a tight response time on the alerts, I'd have to poll very frequently (every 5 seconds).

One idea I had was to have the AJAX-originated polling thread sleep on the server side until an alert arrived on the server. The server would then wake up the sleeping thread and get a response back to the web client that would be shown immediately. This would have allowed me to cut the polling interval down to once every 30 seconds or every minute while improving the time it took to alert the user.

One thing I didn't count on was that mongrel/rails doesn't launch a thread per web request as I had expected it to. That means that other incoming web requests block until the first thread's sleep times out.

I've tried tinkering around with calling "config.threadsafe!" in my configuration, but that doesn't seem to change the behavior to a thread per request model. Plus, it appears that running with config.threadsafe! is a risky proposition that could require a great deal more testing and rework on my existing application.

Any thoughts on the approach I took or better ways to go about getting the response times I'm looking for without the need to deluge the server with requests?

A: 

One approach you could consider is to have (some or all of) your requests create deferred monitoring jobs in an external queue which would in turn periodically notify the monitoring application.

bjg
I saw references to queueing plugins, but figured that once you queue a request to process it that you still return a response to the client right away. I want to hold off on sending a response to the client while the server waits for alerts. Maybe I'm wrong about the queueing plugins?
Chris
A: 

You could use Rails Metal to improve the controller performance or maybe even separate it out entirely into a Sinatra application (Sinatra can handle some serious request throughput).

Another idea is to look into a push solution using Juggernaut or similar.

John Topley
It's not really about the controller performance. My issue is really with intentionally delaying some server responses - while the AJAX query is waiting on an alert - while allowing others to proceed.
Chris
A: 

What you need is Juggernaut which is a Rails plugin that allows your app to initiate a connection and push data to the client. In other words your app can have a real time connection to the server with the advantage of instant updates.

Redbeard
Juggernaut does look like a reasonable solution. I hate having to open up a new port in the firewall for flash, though.
Chris