Class: Rack::ActiveRecordStatus

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

Instance Method Summary collapse

Constructor Details

#initialize(app, path = '/active_record_status') ⇒ ActiveRecordStatus

Returns a new instance of ActiveRecordStatus.



3
4
5
# File 'lib/rack/active_record_status.rb', line 3

def initialize(app, path='/active_record_status')
  @app, @path = app, path
end

Instance Method Details

#call(env) ⇒ Object



7
8
9
10
11
12
13
# File 'lib/rack/active_record_status.rb', line 7

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

#get_statusObject



15
16
17
18
19
20
21
22
23
24
25
# File 'lib/rack/active_record_status.rb', line 15

def get_status
  begin
    ActiveRecord::Base.connection.select_all('select 1')
    body = "OK #{Time.now}"
    [200, {'Content-Type' => 'text/plain'}, body]
  rescue
    body = ['ERROR', "#{$!.class}: #{$!.message}", "Backtrace:"] + $!.backtrace
    body *= "\n"
    [500, {'Content-Type' => 'text/plain'}, body]
  end
end