Class: Rack::ActiveRecordStatus

Inherits:
Object
  • Object
show all
Defined in:
lib/rack/active_record_status.rb

Instance Method Summary collapse

Constructor Details

#initialize(app, options = {}) ⇒ ActiveRecordStatus

Returns a new instance of ActiveRecordStatus.



3
4
5
6
7
8
9
10
# File 'lib/rack/active_record_status.rb', line 3

def initialize(app, options={})
  @app = app
  # Support legacy arguments (0.4.1 and below)
  options = {:path => options} if options.is_a?(String)

  @path = options[:path] || '/app_status'
  @response = options[:response] || "OK\n"
end

Instance Method Details

#call(env) ⇒ Object



12
13
14
15
16
17
18
# File 'lib/rack/active_record_status.rb', line 12

def call(env)
  if @path == env['PATH_INFO']
    get_status
  else
    @app.call(env)
  end
end

#get_statusObject



20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/rack/active_record_status.rb', line 20

def get_status
  begin
    # Check that the application is connected to the database
    ActiveRecord::Base.connection.select_all('select 1')
    # Success
    [200, {'Content-Type' => 'text/plain'}, @response]
  rescue
    body = ['ERROR', "#{$!.class}: #{$!.message}", "Backtrace:"] + $!.backtrace
    body *= "\n"
    [500, {'Content-Type' => 'text/plain'}, body]
  end
end