Class: Rack::ECG

Inherits:
Object
  • Object
show all
Defined in:
lib/rack/ecg.rb,
lib/rack/ecg/check.rb,
lib/rack/ecg/version.rb,
lib/rack/ecg/check/http.rb,
lib/rack/ecg/check/error.rb,
lib/rack/ecg/check_factory.rb,
lib/rack/ecg/check_registry.rb,
lib/rack/ecg/check/git_revision.rb,
lib/rack/ecg/check/redis_connection.rb,
lib/rack/ecg/check/migration_version.rb,
lib/rack/ecg/check/sequel_connection.rb,
lib/rack/ecg/check/active_record_connection.rb

Defined Under Namespace

Modules: Check Classes: CheckFactory, CheckRegistry

Constant Summary collapse

DEFAULT_MOUNT_AT =
"/_ecg"
DEFAULT_CHECKS =
[ :http ]
VERSION =
"0.0.5"

Instance Method Summary collapse

Constructor Details

#initialize(app = nil, options = {}) ⇒ ECG

Returns a new instance of ECG.



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

def initialize(app=nil, options={})
  @app = app

  check_configuration = options.delete(:checks) || []
  @check_factory = CheckFactory.new(check_configuration, DEFAULT_CHECKS)
  @at = options.delete(:at) || DEFAULT_MOUNT_AT

  @hook = options.delete(:hook)
end

Instance Method Details

#call(env) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/rack/ecg.rb', line 21

def call(env)
  if env["PATH_INFO"] == @at
    check_results = @check_factory.build_all.inject({}) do |results, check|
      results.merge(check.result.to_json)
    end

    success = check_results.none? { |check| check[1][:status] == Check::Status::ERROR }

    response_status = success ? 200 : 500

    @hook.call(success, check_results) if @hook

    response_headers = {
      "X-Rack-ECG-Version"  => Rack::ECG::VERSION,
      "Content-Type"        => "application/json"
    }

    response_body = JSON.pretty_generate(check_results)

    [response_status, response_headers, [response_body]]
  elsif @app
    @app.call(env)
  else
    [404, {},[]]
  end
end