Class: ServerHealthCheck

Inherits:
Object
  • Object
show all
Defined in:
lib/server_health_check.rb,
lib/server_health_check/version.rb

Constant Summary collapse

OK =
'OK'.freeze
VERSION =
"1.0.2".freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ ServerHealthCheck

Returns a new instance of ServerHealthCheck.



7
8
9
10
# File 'lib/server_health_check.rb', line 7

def initialize(options = {})
  @logger = options[:logger]
  @results = {}
end

Instance Attribute Details

#loggerObject (readonly)

Returns the value of attribute logger.



5
6
7
# File 'lib/server_health_check.rb', line 5

def logger
  @logger
end

#resultsObject (readonly)

Returns the value of attribute results.



89
90
91
# File 'lib/server_health_check.rb', line 89

def results
  @results
end

Instance Method Details

#active_record!Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/server_health_check.rb', line 26

def active_record!
  begin
    if ActiveRecord::Base.connection_pool.with_connection { |connection| connection.active? }
      @results[:active_record] = OK
      true
    else
      @results[:active_record] = "Failed: unable to connect to database"
      false
    end
  rescue NameError
    raise
  rescue StandardError => e
    @results[:active_record] = "Failed: error while connecting to database"
    false
  end
end

#aws_creds!Object



56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/server_health_check.rb', line 56

def aws_creds!
  options = {}
  options[:logger] = logger if logger
  aws = Aws::S3::Client.new(options)
  begin
    aws.list_buckets
    @results[:AWS] = OK
    true
  rescue Aws::S3::Errors::InvalidAccessKeyId, Aws::S3::Errors::SignatureDoesNotMatch, NoMethodError => e
    @results[:AWS] = e.to_s
    false
  end
end

#aws_s3!(bucket = nil) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/server_health_check.rb', line 43

def aws_s3!(bucket = nil)
  options = {}
  options[:logger] = logger if logger
  bucket = Aws::S3::Bucket.new(bucket, options)
  if bucket.exists?
    @results[:S3] = OK
    true
  else
    @results[:S3] = "Failed: bucket does not exists"
    false
  end
end

#check!(name = 'custom_check') ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/server_health_check.rb', line 70

def check!(name = 'custom_check')
  success = yield
  if success
    @results[name.to_sym] = OK
    true
  else
    @results[name.to_sym] = "Failed"
    false
  end
rescue => e
  @results[name.to_sym] = e.to_s
end

#ok?Boolean

Returns:

  • (Boolean)


83
84
85
86
87
# File 'lib/server_health_check.rb', line 83

def ok?
  @results.all? do |key, value|
    value == OK
  end
end

#redis!(host: nil, port: 6379) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/server_health_check.rb', line 12

def redis!(host: nil, port: 6379)
  host ||= ENV['REDIS_HOST'] || 'localhost'
  @results[:redis] = 'The Redis gem is not loaded'
  redis = Redis.new(host: host, port: port)
  begin
    redis.ping
    @results[:redis] = OK
    true
  rescue Redis::CannotConnectError => e
    @results[:redis] = e.to_s
    false
  end
end