resque_worker_heartbeat 0.1.0 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +8 -8
- data/lib/resque/heartbeat.rb +20 -10
- data/lib/resque_worker_heartbeat/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
---
|
2
2
|
!binary "U0hBMQ==":
|
3
3
|
metadata.gz: !binary |-
|
4
|
-
|
4
|
+
MGQ3NzhhZWM4OGI4MzU2ZTU2NmE0YTA5YWY1OTVjYjhjMTljMmFkZQ==
|
5
5
|
data.tar.gz: !binary |-
|
6
|
-
|
6
|
+
YTJiODg3ZTFiNjEwYTgwNjk0MDk0Nzc5MDQ3YTNlZmQ4Yjc5ZDY3Yw==
|
7
7
|
SHA512:
|
8
8
|
metadata.gz: !binary |-
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
NGI1YWNkNjljOWRiOTFiYWFmOWViNTA3YThkZGUzNzkyNzk5MWM0YzRhYTA0
|
10
|
+
MDZlMjY2ZmRhNzA1ZDZhZmVjMjYwODU0MDk2MzkyNWZkNDU0MDJmOGU2OGI5
|
11
|
+
NmM0YWI2NzYwMTlhOTljNGEwMWE2YzdlYWRkZmM0OGEyNGNjZGM=
|
12
12
|
data.tar.gz: !binary |-
|
13
|
-
|
14
|
-
|
15
|
-
|
13
|
+
Mjc1MjY3NTI3ZjNhZjkyN2FhYjI4NDNhMDUxZmYzMDYwYjc1ODY1N2M0YWFj
|
14
|
+
N2ZiMzE1NDQ3ZTk2OTU0YTE2YzkyNmMxNTIzZmFlMDBiZjZkZWM2ZDlkYWRi
|
15
|
+
NzRkN2RlMzllZjRkOTI0NDI2MDcwMDEwZjExMWI4MjEwYjcwOGY=
|
data/lib/resque/heartbeat.rb
CHANGED
@@ -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.
|
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(
|
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.
|
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
|
-
|
130
|
+
Resque.logger.error "#{e.message} #{e.backtrace}"
|
121
131
|
end
|
122
132
|
end
|
123
133
|
|