Class: Resque::Worker::Heart
- Inherits:
-
Object
- Object
- Resque::Worker::Heart
- Defined in:
- lib/resque/heartbeat.rb
Instance Attribute Summary collapse
-
#worker ⇒ Object
readonly
Returns the value of attribute worker.
Class Method Summary collapse
- .heartbeat_interval_seconds ⇒ Object
-
.heartbeat_key(worker_name, pid) ⇒ Object
you can send a redis wildcard to filter the workers you’re looking for.
- .heartbeats_before_dead ⇒ Object
Instance Method Summary collapse
- #beat! ⇒ Object
- #dead? ⇒ Boolean
-
#initialize(worker) ⇒ Heart
constructor
A new instance of Heart.
- #key ⇒ Object
- #redis ⇒ Object
- #run ⇒ Object
- #stop ⇒ Object
- #ttl ⇒ Object
Constructor Details
#initialize(worker) ⇒ Heart
Returns a new instance of Heart.
60 61 62 |
# File 'lib/resque/heartbeat.rb', line 60 def initialize(worker) @worker = worker end |
Instance Attribute Details
#worker ⇒ Object (readonly)
Returns the value of attribute worker.
49 50 51 |
# File 'lib/resque/heartbeat.rb', line 49 def worker @worker end |
Class Method Details
.heartbeat_interval_seconds ⇒ Object
51 52 53 |
# File 'lib/resque/heartbeat.rb', line 51 def self.heartbeat_interval_seconds ENV['HEARTBEAT_INTERVAL_SECONDS'] || 2 end |
.heartbeat_key(worker_name, pid) ⇒ Object
you can send a redis wildcard to filter the workers you’re looking for
88 89 90 |
# File 'lib/resque/heartbeat.rb', line 88 def Heart.heartbeat_key(worker_name, pid) "worker:#{worker_name}:#{pid}:heartbeat" end |
.heartbeats_before_dead ⇒ Object
55 56 57 |
# File 'lib/resque/heartbeat.rb', line 55 def self.heartbeats_before_dead ENV['HEARTBEATS_BEFORE_DEAD'] || 25 end |
Instance Method Details
#beat! ⇒ Object
96 97 98 99 100 101 |
# File 'lib/resque/heartbeat.rb', line 96 def beat! redis.sadd(:workers, worker) redis.setex(key, Heart.heartbeat_interval_seconds * Heart.heartbeats_before_dead, '') rescue Exception => e Resque.logger.fatal "Unable to set the heartbeat for worker '#{worker.remote_hostname}:#{worker.remote_pid}': #{e} : #{e.backtrace}" end |
#dead? ⇒ Boolean
103 104 105 |
# File 'lib/resque/heartbeat.rb', line 103 def dead? !redis.exists(key) end |
#key ⇒ Object
92 93 94 |
# File 'lib/resque/heartbeat.rb', line 92 def key Heart.heartbeat_key(worker.remote_hostname, worker.remote_pid) end |
#redis ⇒ Object
83 84 85 |
# File 'lib/resque/heartbeat.rb', line 83 def redis Resque.redis end |
#run ⇒ Object
64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/resque/heartbeat.rb', line 64 def run @thrd ||= Thread.new do loop do begin beat! && sleep(Heart.heartbeat_interval_seconds) rescue Exception => e Resque.logger.error "Error while doing heartbeat: #{e} : #{e.backtrace}" end end end end |
#stop ⇒ Object
76 77 78 79 80 81 |
# File 'lib/resque/heartbeat.rb', line 76 def stop Thread.kill(@thrd) if @thrd redis.del key rescue nil end |
#ttl ⇒ Object
107 108 109 |
# File 'lib/resque/heartbeat.rb', line 107 def ttl Resque.redis.ttl key end |