mloughran-job_queue 0.0.6 → 0.0.7
Sign up to get free protection for your applications and to get access to all the features.
- data/VERSION.yml +1 -1
- data/lib/job_queue/adapters/beanstalk_adapter.rb +15 -5
- data/spec/beanstalk_adapter_spec.rb +55 -16
- metadata +2 -2
data/VERSION.yml
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
require 'beanstalk-client'
|
2
|
+
require 'timeout'
|
2
3
|
|
3
4
|
class JobQueue::BeanstalkAdapter
|
4
5
|
def initialize(options = {})
|
@@ -18,7 +19,7 @@ class JobQueue::BeanstalkAdapter
|
|
18
19
|
end
|
19
20
|
|
20
21
|
def subscribe(error_report, cleanup_task, queue, &block)
|
21
|
-
pool =
|
22
|
+
pool = BeanstalkPoolFix.new([@hosts].flatten, queue)
|
22
23
|
loop do
|
23
24
|
begin
|
24
25
|
job = pool.reserve(1)
|
@@ -64,11 +65,11 @@ class JobQueue::BeanstalkAdapter
|
|
64
65
|
def beanstalk_pool(queue='default')
|
65
66
|
@beanstalk_pools ||= {}
|
66
67
|
@beanstalk_pools[queue] ||= begin
|
67
|
-
|
68
|
+
BeanstalkPoolFix.new([@hosts].flatten, queue)
|
68
69
|
end
|
69
70
|
end
|
70
71
|
|
71
|
-
|
72
|
+
class BeanstalkPoolFix < Beanstalk::Pool
|
72
73
|
def put_and_report_conn(body, pri=65536, delay=0, ttr=120)
|
73
74
|
send_to_rand_conn_and_report(:put, body, pri, delay, ttr)
|
74
75
|
end
|
@@ -84,7 +85,16 @@ class JobQueue::BeanstalkAdapter
|
|
84
85
|
def job_stats(id)
|
85
86
|
make_hash(send_to_all_conns(:job_stats, id))
|
86
87
|
end
|
88
|
+
|
89
|
+
private
|
90
|
+
|
91
|
+
def call_wrap(c, *args)
|
92
|
+
self.last_conn = c
|
93
|
+
c.send(*args)
|
94
|
+
rescue EOFError, Errno::ECONNRESET, Errno::EPIPE, Beanstalk::UnexpectedResponse => ex
|
95
|
+
# puts "Beanstalk exception: #{ex.class}" # Useful for debugging
|
96
|
+
self.remove(c) unless ex.class == Beanstalk::TimedOut
|
97
|
+
raise ex
|
98
|
+
end
|
87
99
|
end
|
88
100
|
end
|
89
|
-
|
90
|
-
Beanstalk::Pool.send(:include, JobQueue::BeanstalkAdapter::BeanstalkExtension)
|
@@ -2,9 +2,12 @@ require File.dirname(__FILE__) + '/spec_helper'
|
|
2
2
|
|
3
3
|
describe JobQueue::BeanstalkAdapter do
|
4
4
|
before :each do
|
5
|
-
|
6
|
-
|
7
|
-
system "beanstalkd -p
|
5
|
+
# On OSX we the -d flag doesn't work for beanstalk 1.3. This is a
|
6
|
+
# workaround for that issue. We sleep a little to let processes start.
|
7
|
+
system "beanstalkd -p 10001 &"
|
8
|
+
system "beanstalkd -p 10002 &"
|
9
|
+
system "beanstalkd -p 11300 &"
|
10
|
+
sleep 0.1
|
8
11
|
end
|
9
12
|
|
10
13
|
after :each do
|
@@ -13,26 +16,36 @@ describe JobQueue::BeanstalkAdapter do
|
|
13
16
|
|
14
17
|
describe '#new' do
|
15
18
|
before(:each) do
|
16
|
-
@pool =
|
19
|
+
@pool = JobQueue::BeanstalkAdapter::BeanstalkPoolFix.new([
|
20
|
+
'localhost:11300'
|
21
|
+
])
|
17
22
|
end
|
18
23
|
|
19
24
|
it "should default to localhost:11300" do
|
20
|
-
|
25
|
+
JobQueue::BeanstalkAdapter::BeanstalkPoolFix.should_receive(:new).with(
|
26
|
+
['localhost:11300'],
|
27
|
+
"default"
|
28
|
+
).and_return @pool
|
21
29
|
JobQueue.adapter = JobQueue::BeanstalkAdapter.new
|
22
30
|
JobQueue.put('test')
|
23
31
|
end
|
24
32
|
|
25
33
|
it "should accept one beanstalk instance" do
|
26
|
-
|
27
|
-
|
34
|
+
JobQueue::BeanstalkAdapter::BeanstalkPoolFix.should_receive(:new).with(
|
35
|
+
['12.34.56.78:12345'],
|
36
|
+
'default'
|
37
|
+
).and_return(@pool)
|
38
|
+
JobQueue.adapter = JobQueue::BeanstalkAdapter.new(
|
39
|
+
:hosts => '12.34.56.78:12345'
|
40
|
+
)
|
28
41
|
JobQueue.put('test')
|
29
42
|
end
|
30
43
|
|
31
44
|
it "should allow multiple beanstalk instances" do
|
32
|
-
|
33
|
-
'12.34.56.78:12345',
|
34
|
-
'
|
35
|
-
|
45
|
+
JobQueue::BeanstalkAdapter::BeanstalkPoolFix.should_receive(:new).with(
|
46
|
+
['12.34.56.78:12345', '87.65.43.21:54321'],
|
47
|
+
'default'
|
48
|
+
).and_return(@pool)
|
36
49
|
JobQueue.adapter = JobQueue::BeanstalkAdapter.new({
|
37
50
|
:hosts => ['12.34.56.78:12345', '87.65.43.21:54321']
|
38
51
|
})
|
@@ -41,7 +54,7 @@ describe JobQueue::BeanstalkAdapter do
|
|
41
54
|
end
|
42
55
|
|
43
56
|
describe "put" do
|
44
|
-
before :
|
57
|
+
before :each do
|
45
58
|
JobQueue.adapter = JobQueue::BeanstalkAdapter.new
|
46
59
|
end
|
47
60
|
|
@@ -105,7 +118,7 @@ describe JobQueue::BeanstalkAdapter do
|
|
105
118
|
end
|
106
119
|
|
107
120
|
describe "subscribe" do
|
108
|
-
before :
|
121
|
+
before :each do
|
109
122
|
JobQueue.adapter = JobQueue::BeanstalkAdapter.new
|
110
123
|
end
|
111
124
|
|
@@ -155,10 +168,36 @@ describe JobQueue::BeanstalkAdapter do
|
|
155
168
|
|
156
169
|
File.exists?('jobcleanup').should be_false
|
157
170
|
end
|
171
|
+
|
172
|
+
# This test is for a patch that fixes a connection leaking issue in
|
173
|
+
# beanstalk-client 1.0.2
|
174
|
+
it "should not open more connections to beanstalk over time" do
|
175
|
+
# Every 1.5 seconds, add a new job to the queue and check how many
|
176
|
+
# connections are currently open according to beanstalkd.
|
177
|
+
connections = []
|
178
|
+
Thread.new do
|
179
|
+
pool = Beanstalk::Pool.new(["localhost:11300"])
|
180
|
+
loop do
|
181
|
+
sleep 1.5
|
182
|
+
JobQueue.put("job")
|
183
|
+
connections << pool.stats["total-connections"]
|
184
|
+
end
|
185
|
+
end
|
186
|
+
|
187
|
+
# Subscribe for 3 loops - gives time for a few timeouts to occur (1s)
|
188
|
+
i = 0
|
189
|
+
JobQueue.subscribe do |job|
|
190
|
+
i += 1
|
191
|
+
throw :stop if i == 3
|
192
|
+
end
|
193
|
+
|
194
|
+
# The number of connections should have been constant
|
195
|
+
connections.uniq.size.should == 1
|
196
|
+
end
|
158
197
|
end
|
159
198
|
|
160
199
|
describe "job_stats" do
|
161
|
-
before :
|
200
|
+
before :each do
|
162
201
|
JobQueue.adapter = JobQueue::BeanstalkAdapter.new
|
163
202
|
end
|
164
203
|
|
@@ -172,7 +211,7 @@ describe JobQueue::BeanstalkAdapter do
|
|
172
211
|
end
|
173
212
|
|
174
213
|
describe "when connecting to one instance" do
|
175
|
-
before :
|
214
|
+
before :each do
|
176
215
|
JobQueue.adapter = JobQueue::BeanstalkAdapter.new
|
177
216
|
end
|
178
217
|
|
@@ -244,7 +283,7 @@ describe JobQueue::BeanstalkAdapter do
|
|
244
283
|
end
|
245
284
|
|
246
285
|
describe "when connecting to multiple instances" do
|
247
|
-
before :
|
286
|
+
before :each do
|
248
287
|
JobQueue.adapter = JobQueue::BeanstalkAdapter.new({
|
249
288
|
:hosts => ['localhost:10001', 'localhost:10002']
|
250
289
|
})
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mloughran-job_queue
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.7
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Martyn Loughran
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2009-06-
|
12
|
+
date: 2009-06-22 00:00:00 -07:00
|
13
13
|
default_executable:
|
14
14
|
dependencies: []
|
15
15
|
|