Posted by aaron
on Friday, October 26
Paul Gross from
Thoughtworks recently created a
pulse gem. The gem adds a simple action to your rails app, "/pulse", which acts as a heartbeat.
The pulse gem currently defines the method as:
def pulse
render :text => "OK"
end
Then you can configure haproxy to monitor your application by hitting http://server/pulse and verifying the response is "OK".
We've had a similar action in our applications for quite some time, although I really like the idea of externalizing it to a gem. Our implementation is slightly different though. Since so many rails applications depend on a database, I instead added:
def pulse
rows = ActiveRecord::Base.connection.execute("select 1 from dual").num_rows rescue 0
render :text => rows == 1 ? "OK" : "Error!"
end
IIRC, "select 1 from dual" is the fastest query you can run against a database, in MySQL, Oracle, and Postgres. And if you have mutliple databases, you can add union in the one pulse request, or have multiple actions.
Now you are not only testing your application is live, but that it can connect to the database.