resque_worker_heartbeat 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- MjM5MGNjODExZjhhNzJjOTAxNjU0ZTJmZjE0MGM3NmViYmNkM2I4Ng==
4
+ MGQ3NzhhZWM4OGI4MzU2ZTU2NmE0YTA5YWY1OTVjYjhjMTljMmFkZQ==
5
5
  data.tar.gz: !binary |-
6
- ZTk3NDY4ZjgyNjcwOGM3YzZjZmJhM2YxYjc1ZWE2OWVlODg4OTNmZQ==
6
+ YTJiODg3ZTFiNjEwYTgwNjk0MDk0Nzc5MDQ3YTNlZmQ4Yjc5ZDY3Yw==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- MDk5NDdhMjkyMzljZGY1ZjI5MWYxZWQ3NmI5YzYxM2ZjMWUxNjlmOWEyOTAz
10
- ZTQ1MmQyNTE0NWEwMDUzYzEwZDVhN2Q3OTJlZmZjYjliMDE2YTA1YzdiOGI2
11
- NDM3ZGJiMDcwOTE0Nzg1NDk4ODI2MDZhNzM2N2U3NTkzZTBmZWY=
9
+ NGI1YWNkNjljOWRiOTFiYWFmOWViNTA3YThkZGUzNzkyNzk5MWM0YzRhYTA0
10
+ MDZlMjY2ZmRhNzA1ZDZhZmVjMjYwODU0MDk2MzkyNWZkNDU0MDJmOGU2OGI5
11
+ NmM0YWI2NzYwMTlhOTljNGEwMWE2YzdlYWRkZmM0OGEyNGNjZGM=
12
12
  data.tar.gz: !binary |-
13
- MmQ1OGY1ZTQzZTJmNTE0MDllNGJkN2I3ZjA3YTY1YzdjNDU4NDAzYzg0NjA0
14
- MzVmOWNlY2JjYjBjODI5ODI1ZmRjYTZmOWE1NGQ0NmQyODAzNjk0ZjhkMGRj
15
- NGM0ODYyNjNkMDI4YTBlMTY0ODhmZjdiODNhMDlkYjdkYmE0NWM=
13
+ Mjc1MjY3NTI3ZjNhZjkyN2FhYjI4NDNhMDUxZmYzMDYwYjc1ODY1N2M0YWFj
14
+ N2ZiMzE1NDQ3ZTk2OTU0YTE2YzkyNmMxNTIzZmFlMDBiZjZkZWM2ZDlkYWRi
15
+ NzRkN2RlMzllZjRkOTI0NDI2MDcwMDEwZjExMWI4MjEwYjcwOGY=
@@ -9,9 +9,15 @@ module Resque
9
9
  end
10
10
  alias_method(:startup, :startup_with_heartbeat)
11
11
 
12
+ def is_me?
13
+ pieces = id.split(':')
14
+ (pieces[0].casecmp(hostname)==0) && (pieces[1].to_i == Process.pid)
15
+ end
16
+
12
17
  alias_method(:unregister_worker_without_heartbeat, :unregister_worker)
13
18
  def unregister_worker_with_heartbeat(*args)
14
- heart.stop
19
+ to_stop = is_me? ? heart : Heart.new(self)
20
+ to_stop.stop
15
21
  unregister_worker_without_heartbeat(*args)
16
22
  end
17
23
  alias_method(:unregister_worker, :unregister_worker_with_heartbeat)
@@ -21,7 +27,11 @@ module Resque
21
27
  end
22
28
 
23
29
  def remote_hostname
24
- id.split(':').first
30
+ @remote_hostname ||= id.split(':').first
31
+ end
32
+
33
+ def remote_pid
34
+ @remote_pid ||= id.split(':')[1]
25
35
  end
26
36
 
27
37
  def dead?
@@ -55,7 +65,7 @@ module Resque
55
65
  @thrd ||= Thread.new do
56
66
  loop do
57
67
  begin
58
- beat! && sleep(2)
68
+ beat! && sleep(Heart.heartbeat_interval_seconds)
59
69
  rescue Exception => e
60
70
  Resque.logger.error "Error while doing heartbeat: #{e} : #{e.backtrace}"
61
71
  end
@@ -64,7 +74,7 @@ module Resque
64
74
  end
65
75
 
66
76
  def stop
67
- Thread.kill(@thrd)
77
+ Thread.kill(@thrd) if @thrd
68
78
  redis.del key
69
79
  rescue
70
80
  nil
@@ -75,19 +85,19 @@ module Resque
75
85
  end
76
86
 
77
87
  # you can send a redis wildcard to filter the workers you're looking for
78
- def Heart.heartbeat_key(worker_name)
79
- "worker:#{worker_name}:heartbeat"
88
+ def Heart.heartbeat_key(worker_name, pid)
89
+ "worker:#{worker_name}:#{pid}:heartbeat"
80
90
  end
81
91
 
82
92
  def key
83
- Heart.heartbeat_key worker.remote_hostname
93
+ Heart.heartbeat_key(worker.remote_hostname, worker.remote_pid)
84
94
  end
85
95
 
86
96
  def beat!
87
97
  redis.sadd(:workers, worker)
88
98
  redis.setex(key, Heart.heartbeat_interval_seconds * Heart.heartbeats_before_dead, '')
89
99
  rescue Exception => e
90
- Resque.logger.fatal "Unable to set the heartbeat for worker '#{worker.remote_hostname}': #{e} : #{e.backtrace}"
100
+ Resque.logger.fatal "Unable to set the heartbeat for worker '#{worker.remote_hostname}:#{worker.remote_pid}': #{e} : #{e.backtrace}"
91
101
  end
92
102
 
93
103
  def dead?
@@ -103,7 +113,7 @@ module Resque
103
113
  # NOTE: this assumes all of your workers are putting out heartbeats
104
114
  def self.prune_dead_workers!
105
115
  begin
106
- beats = Resque.redis.keys(Worker::Heart.heartbeat_key('*'))
116
+ beats = Resque.redis.keys(Worker::Heart.heartbeat_key('*', '*'))
107
117
  Worker.all.each do |worker|
108
118
  worker.prune_if_dead
109
119
 
@@ -117,7 +127,7 @@ module Resque
117
127
  Resque.redis.del key
118
128
  end
119
129
  rescue Exception => e
120
- p e
130
+ Resque.logger.error "#{e.message} #{e.backtrace}"
121
131
  end
122
132
  end
123
133
 
@@ -1,3 +1,3 @@
1
1
  module ResqueWorkerHeartbeat
2
- VERSION = '0.1.0'
2
+ VERSION = '0.2.0'
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: resque_worker_heartbeat
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sven Fuchs