Class: Artery::Check

Inherits:
Object
  • Object
show all
Defined in:
lib/artery/check.rb

Constant Summary collapse

TIMEOUT =
ENV.fetch('ARTERY_CHECK_TIMEOUT', '1').to_i
ESSENTIAL_SERVICES =
ENV.fetch('ARTERY_CHECK_ESSENTIAL_SERVICES', '').split(',')

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(**options) ⇒ Check

Returns a new instance of Check.



8
9
10
# File 'lib/artery/check.rb', line 8

def initialize(**options)
  @timeout = options.fetch(:timeout, TIMEOUT)
end

Class Method Details

.run(services) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/artery/check.rb', line 33

def self.run(services)
  Artery.logger.tagged('Check') do
    result = Artery::Check.new.execute services

    errors = result.select { |_service, res| res[:status] == :error }
    return if errors.blank?

    Artery.logger.error "There were errors:\n\t#{errors.map do |service, res|
      "#{service}: #{res[:message]}"
    end.join("\n\t")}"
    exit 1
  end
end

Instance Method Details

#execute(services = ESSENTIAL_SERVICES) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/artery/check.rb', line 12

def execute(services = ESSENTIAL_SERVICES)
  all_services = Artery.subscriptions.blank? ? [] : Artery.subscriptions.keys.map(&:service).uniq
  services = all_services if services.blank?

  if services.blank?
    Artery.logger.warn 'No services privided, exiting...'
    return
  end

  result = {}

  services.each do |service|
    Artery.request "#{service}.healthz.check", {}, timeout: @timeout do |on|
      on.success { result[service] = { status: :ok } }
      on.error { |e| result[service] = { status: :error, message: e } }
    end
  end

  result
end