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 host port ssl token connect_timeout receive_timeout send_timeout}.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

build, inherited, reschedule_abandoned_jobs

Constructor Details

#initialize(*args) ⇒ ConsulHealthCheck

Returns a new instance of ConsulHealthCheck.



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

def initialize(*args)
  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 'imperium'

  if config.keys.any? { |k| CONSUL_CONFIG_KEYS.include?(k) }
    consul_config = Imperium::Configuration.new.tap do |conf|
      CONSUL_CONFIG_KEYS.each do |key|
        conf.send("#{key}=", config[key]) if config[key]
      end
    end
    @agent_client = Imperium::Agent.new(consul_config)
    @catalog_client = Imperium::Catalog.new(consul_config)
  else
    @agent_client = Imperium::Agent.default_client
    @catalog_client = Imperium::Catalog.default_client
  end
end

Instance Attribute Details

#agent_clientObject (readonly)

Returns the value of attribute agent_client.



12
13
14
# File 'lib/delayed/worker/consul_health_check.rb', line 12

def agent_client
  @agent_client
end

#catalog_clientObject (readonly)

Returns the value of attribute catalog_client.



12
13
14
# File 'lib/delayed/worker/consul_health_check.rb', line 12

def catalog_client
  @catalog_client
end

Instance Method Details

#live_workersObject



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

def live_workers
  live_nodes = @catalog_client.list_nodes_for_service(service_name)
  if live_nodes.ok?
    live_nodes.map(&:service_id)
  else
    raise "Unable to read from Consul catalog: #{live_nodes.content}"
  end
end

#startObject



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

def start
  service = Imperium::Service.new({
    id: worker_name,
    name: service_name,
  })
  service.add_check(check_attributes)
  response = @agent_client.register_service(service)
  response.ok?
end

#stopObject



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

def stop
  response = @agent_client.deregister_service(worker_name)
  response.ok? || response.not_found?
end