Class: Delayed::Worker::ConsulHealthCheck
- Inherits:
-
HealthCheck
- Object
- HealthCheck
- Delayed::Worker::ConsulHealthCheck
- 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
-
#agent_client ⇒ Object
readonly
Returns the value of attribute agent_client.
-
#catalog_client ⇒ Object
readonly
Returns the value of attribute catalog_client.
Attributes inherited from HealthCheck
Instance Method Summary collapse
-
#initialize ⇒ ConsulHealthCheck
constructor
A new instance of ConsulHealthCheck.
- #live_workers ⇒ Object
- #start ⇒ Object
- #stop ⇒ Object
Methods inherited from HealthCheck
attempt_advisory_lock, build, inherited, reschedule_abandoned_jobs
Constructor Details
#initialize ⇒ ConsulHealthCheck
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 '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_client ⇒ Object (readonly)
Returns the value of attribute agent_client.
14 15 16 |
# File 'lib/delayed/worker/consul_health_check.rb', line 14 def agent_client @agent_client end |
#catalog_client ⇒ Object (readonly)
Returns the value of attribute catalog_client.
14 15 16 |
# File 'lib/delayed/worker/consul_health_check.rb', line 14 def catalog_client @catalog_client end |
Instance Method Details
#live_workers ⇒ Object
51 52 53 54 55 56 57 58 |
# File 'lib/delayed/worker/consul_health_check.rb', line 51 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 |
#start ⇒ Object
36 37 38 39 40 41 42 43 44 |
# File 'lib/delayed/worker/consul_health_check.rb', line 36 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 |
#stop ⇒ Object
46 47 48 49 |
# File 'lib/delayed/worker/consul_health_check.rb', line 46 def stop response = @agent_client.deregister_service(worker_name) response.ok? || response.not_found? end |