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
- SCRIPT_TEMPLATE =
" WORKER_PID=\"%<pid>d\" # an example, filled from ruby when the check is created\n ORIGINAL_MTIME=\"%<mtime>d\" # an example, filled from ruby when the check is created\n\n if [ -d \"/proc/$WORKER_PID\" ]; then\n CURRENT_MTIME=$(stat -f %%Y /proc/$WORKER_PID)\n\n if [ \"$ORIGINAL_MTIME\" = \"$CURRENT_MTIME\" ]; then\n exit 0 # Happy day\n else\n echo \"PID still exists but procfs entry has changed, current command:\"\n ps -p $PID -o 'command='\n exit 1 # Something is wrong, trigger a \"warning\" state\n fi\n else\n exit 255 # The process is no more, trigger a \"critical\" state.\n fi\n".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(*args) ⇒ ConsulHealthCheck
constructor
A new instance of ConsulHealthCheck.
- #live_workers ⇒ Object
- #start ⇒ Object
- #stop ⇒ Object
Methods inherited from HealthCheck
build, inherited, reschedule_abandoned_jobs
Constructor Details
#initialize(*args) ⇒ ConsulHealthCheck
Returns a new instance of ConsulHealthCheck.
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/delayed/worker/consul_health_check.rb', line 32 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_client ⇒ Object (readonly)
Returns the value of attribute agent_client.
11 12 13 |
# File 'lib/delayed/worker/consul_health_check.rb', line 11 def agent_client @agent_client end |
#catalog_client ⇒ Object (readonly)
Returns the value of attribute catalog_client.
11 12 13 |
# File 'lib/delayed/worker/consul_health_check.rb', line 11 def catalog_client @catalog_client end |
Instance Method Details
#live_workers ⇒ Object
67 68 69 70 71 72 73 74 |
# File 'lib/delayed/worker/consul_health_check.rb', line 67 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
52 53 54 55 56 57 58 59 60 |
# File 'lib/delayed/worker/consul_health_check.rb', line 52 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
62 63 64 65 |
# File 'lib/delayed/worker/consul_health_check.rb', line 62 def stop response = @agent_client.deregister_service(worker_name) response.ok? || response.not_found? end |