Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
A basic check health URL that can be used by System Administrators
branch: master

This branch is 3 commits ahead of latimes:master

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.
lib
test
MIT-LICENSE
README.rdoc
Rakefile

README.rdoc

Health

Health will setup a standardized check_health URL on your application. System Administrators can then use this URL to look for an HTTP response of 200 and the response content of 'SERVERUP'.

Example

To add the health check to your application you can include this plugin and simply include it in your application controller.

ApplicationController < ActionController::Base
  include Health
end

Once you've done this you can see if the application is running by navigating to

http://localhost:3000/check_health

HINT: Health does not care what controller it is mixed in with.

Alternate Success Response

By default, health returns HTTP 200 with content of 'SERVERUP'. To change the success content you configure check_health with an option:

ApplicationController < ActionController::Base
  include Health
  check_health :success_message => 'ALL GOOD!'
end

Database Check

By default, health will also run a basic sql query[1] on your database and check the results. If there is an error it will return content of 'DBDOWN'. NOTE, if your database is down or the credentials are not properly setup, Rails will throw a 500 error before the check_health action is ever run.

Some rails applications do not use a databases and are not backed ActiveRecord models. To turn of the database check you configure check_health with a option to turn it off:

ApplicationController < ActionController::Base
  include Health
  check_health :with_db => false
end
1

the query it runs is 'select 1' on your ActiveRecord::Base.connection.

Manual Checks

You can also supply any additional checking you'd like to have Health to perform. This return value must be true if successful, otherwise it will return the result after the content 'PROCDOWN'. You can specify multiple checks and Health will return once a check fails and will not process any other checks.

I suggest that if a process fails to return a descriptive reason as the result so you can see why/which check failed. (ie. PROCDOWN no donut)

You can specify a Proc, block, or a Symbol representing the method name on the controller in which Health was included.

For blocks, the controller argument is optional:

ApplicationController < ActionController::Base
  include Health
  check_health do |controller|
    controller.has_donut? || "no donut"
  end
end

For Procs, just past them as a list of arguments, they are run in context of the controller:

ApplicationController < ActionController::Base
  include Health
  check_health lambda {  donut? || "no donut" }, lambda { donuts.size < 12 || "less than a dozen donuts" }
end

For Symbols, make sure they are defined in your controller:

ApplicationController < ActionController::Base
  include Health
  check_health :check_donut, :check_quantity

  def check_donut
    donut? || "no donut"
  end

  def check_quantity
   donuts.size < 12 || "less than a dozen donuts"
  end
end

You may mix and match all three types, the block will be run last:

ApplicationController < ActionController::Base
  include Health
  check_health :check_donut, lambda { donuts.size < 12 || "less than a dozen donuts" }, :with_db => false do |c|
    c.homer.blank? || "watch out for homer"
  end

  def check_donut
    donut? || "no donut"
  end
end

Copyright © 2008 Los Angeles Times, released under the MIT license

Something went wrong with that request. Please try again.