Class: HealthCheck::Utils

Inherits:
Object
  • Object
show all
Defined in:
lib/health_check/utils.rb

Constant Summary collapse

@@default_smtp_settings =
{
  address: 'localhost',
  port: 25,
  domain: 'localhost.localdomain',
  user_name: nil,
  password: nil,
  authentication: nil,
  enable_starttls_auto: true
}

Class Method Summary collapse

Class Method Details

.check_cacheObject



165
166
167
# File 'lib/health_check/utils.rb', line 165

def self.check_cache
  Rails.cache.write('__health_check_cache_test__', 'ok', expires_in: 1.second) ? '' : 'Unable to write to cache. '
end

.check_emailObject



122
123
124
125
126
127
128
129
130
131
# File 'lib/health_check/utils.rb', line 122

def self.check_email
  case ActionMailer::Base.delivery_method
  when :smtp
    HealthCheck::Utils.check_smtp(ActionMailer::Base.smtp_settings, HealthCheck.smtp_timeout)
  when :sendmail
    HealthCheck::Utils.check_sendmail(ActionMailer::Base.sendmail_settings)
  else
    ''
  end
end

.check_sendmail(settings) ⇒ Object



133
134
135
# File 'lib/health_check/utils.rb', line 133

def self.check_sendmail(settings)
  File.executable?(settings[:location]) ? '' : 'no sendmail executable found. '
end

.check_smtp(settings, timeout) ⇒ Object



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/health_check/utils.rb', line 137

def self.check_smtp(settings, timeout)
  status = ''
  begin
    if @skip_external_checks
      status = '221'
    else
      Timeout.timeout(timeout) do |_timeout_length|
        t = TCPSocket.new(settings[:address], settings[:port])
        begin
          status = t.gets
          status = t.gets while !status.nil? && status !~ /^2/
          t.puts "HELO #{settings[:domain]}\r"
          status = t.gets while !status.nil? && status !~ /^250/
          t.puts "QUIT\r"
          status = t.gets
        ensure
          t.close
        end
      end
    end
  rescue Errno::EBADF
    status = 'Unable to connect to service'
  rescue Exception => ex
    status = ex.to_s
  end
  /^221/.match?(status) ? '' : "SMTP: #{status || 'unexpected EOF on socket'}. "
end

.db_migrate_pathObject



92
93
94
95
# File 'lib/health_check/utils.rb', line 92

def self.db_migrate_path
  # Lazy initialisation so Rails.root will be defined
  @@db_migrate_path ||= Rails.root.join('db', 'migrate')
end

.db_migrate_path=(value) ⇒ Object



97
98
99
# File 'lib/health_check/utils.rb', line 97

def self.db_migrate_path=(value)
  @@db_migrate_path = value
end

.get_database_versionObject



105
106
107
# File 'lib/health_check/utils.rb', line 105

def self.get_database_version
  ActiveRecord::Migrator.current_version if defined?(ActiveRecord)
end

.get_migration_version(dir = db_migrate_path) ⇒ Object



109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/health_check/utils.rb', line 109

def self.get_migration_version(dir = db_migrate_path)
  latest_migration = nil
  Dir[File.join(dir, '[0-9]*_*.rb')].each do |f|
    l = begin
          f.scan(/0*([0-9]+)_[_.a-zA-Z0-9]*.rb/).first.first
        rescue StandardError
          -1
        end
    latest_migration = l if !latest_migration || l.to_i > latest_migration.to_i
  end
  latest_migration
end

.mailer_configured?Boolean

Returns:

  • (Boolean)


101
102
103
# File 'lib/health_check/utils.rb', line 101

def self.mailer_configured?
  defined?(ActionMailer::Base) && (ActionMailer::Base.delivery_method != :smtp || HealthCheck::Utils.default_smtp_settings != ActionMailer::Base.smtp_settings)
end

.process_checks(checks, called_from_middleware = false) ⇒ Object

process an array containing a list of checks



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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/health_check/utils.rb', line 22

def self.process_checks(checks, called_from_middleware = false)
  errors = ''
  checks.each do |check|
    case check
    when 'and', 'site'
    # do nothing
    when 'database'
      HealthCheck::Utils.get_database_version
    when 'email'
      errors << HealthCheck::Utils.check_email
    when 'emailconf'
      errors << HealthCheck::Utils.check_email if HealthCheck::Utils.mailer_configured?
    when 'migrations', 'migration'
      if defined?(ActiveRecord::Migration) && ActiveRecord::Migration.respond_to?(:check_pending!)
        # Rails 4+
        begin
          ActiveRecord::Migration.check_pending!
        rescue ActiveRecord::PendingMigrationError => ex
          errors << ex.message
        end
      else
        database_version  = HealthCheck::Utils.get_database_version
        migration_version = HealthCheck::Utils.get_migration_version
        errors << "Current database version (#{database_version}) does not match latest migration (#{migration_version}). " if database_version.to_i != migration_version.to_i
      end
    when 'cache'
      errors << HealthCheck::Utils.check_cache
    when 'resque-redis-if-present'
      errors << HealthCheck::ResqueHealthCheck.check if defined?(::Resque)
    when 'sidekiq-redis-if-present'
      errors << HealthCheck::SidekiqHealthCheck.check if defined?(::Sidekiq)
    when 'redis-if-present'
      errors << HealthCheck::RedisHealthCheck.check if defined?(::Redis)
    when 's3-if-present'
      errors << HealthCheck::S3HealthCheck.check if defined?(::Aws)
    when 'resque-redis'
      errors << HealthCheck::ResqueHealthCheck.check
    when 'sidekiq-redis'
      errors << HealthCheck::SidekiqHealthCheck.check
    when 'redis'
      errors << HealthCheck::RedisHealthCheck.check
    when 's3'
      errors << HealthCheck::S3HealthCheck.check
    when 'standard'
      errors << HealthCheck::Utils.process_checks(HealthCheck.standard_checks, called_from_middleware)
    when 'middleware'
      errors << 'Health check not called from middleware - probably not installed as middleware.' unless called_from_middleware
    when 'custom'
      HealthCheck.custom_checks.each do |_name, list|
        list.each do |custom_check|
          errors << custom_check.call(self)
        end
      end
    when 'all', 'full'
      errors << HealthCheck::Utils.process_checks(HealthCheck.full_checks, called_from_middleware)
    else
      if HealthCheck.custom_checks.include? check
        HealthCheck.custom_checks[check].each do |custom_check|
          errors << custom_check.call(self)
        end
      else
        return 'invalid argument to health_test.'
      end
    end
  end
  errors
rescue StandardError => e
  e.message
end