Class: Delayed::Worker::ConsulHealthCheck

Inherits:
HealthCheck
  • Object
show all
Defined in:
lib/delayed/worker/consul_health_check.rb

Constant Summary collapse

CONSUL_CONFIG_KEYS =
%w{url acl_token}.map(&:freeze).freeze
DEFAULT_SERVICE_NAME =
'inst-jobs_worker'.freeze

Instance Attribute Summary collapse

Attributes inherited from HealthCheck

#config, #worker_name

Instance Method Summary collapse

Methods inherited from HealthCheck

attempt_advisory_lock, build, inherited, reschedule_abandoned_jobs

Constructor Details

#initializeConsulHealthCheck

Returns a new instance of ConsulHealthCheck.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/delayed/worker/consul_health_check.rb', line 16

def initialize(*, **)
  super
  # Because we don't want the consul client to be a hard dependency we're
  # only requiring it once it's absolutely needed
  require 'diplomat'

  if config.keys.any? { |k| CONSUL_CONFIG_KEYS.include?(k) }
    consul_config = Diplomat::Configuration.new.tap do |conf|
      CONSUL_CONFIG_KEYS.each do |key|
        conf.send("#{key}=", config[key]) if config[key]
      end
    end
    @service_client = Diplomat::Service.new(configuration: consul_config)
    @health_client = Diplomat::Health.new(configuration: consul_config)
  else
    @service_client = Diplomat::Service.new
    @health_client = Diplomat::Health.new
  end
end

Instance Attribute Details

#health_clientObject (readonly)

Returns the value of attribute health_client.



14
15
16
# File 'lib/delayed/worker/consul_health_check.rb', line 14

def health_client
  @health_client
end

#service_clientObject (readonly)

Returns the value of attribute service_client.



14
15
16
# File 'lib/delayed/worker/consul_health_check.rb', line 14

def service_client
  @service_client
end

Instance Method Details

#live_workersObject



48
49
50
51
52
53
54
55
# File 'lib/delayed/worker/consul_health_check.rb', line 48

def live_workers
  # Filter out critical workers (probably nodes failing their serf health check)
  live_nodes = @health_client.service(service_name, {
    filter: 'not Checks.Status == critical'
  })

  live_nodes.map { |n| n.Service['ID']}
end

#startObject



36
37
38
39
40
41
42
# File 'lib/delayed/worker/consul_health_check.rb', line 36

def start
  @service_client.register({
    id: worker_name,
    name: service_name,
    check: check_attributes
  })
end

#stopObject



44
45
46
# File 'lib/delayed/worker/consul_health_check.rb', line 44

def stop
  @service_client.deregister(worker_name)
end