beaneater 1.1.4 → 1.3.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 +4 -4
- data/.github/workflows/ruby.yml +7 -3
- data/CHANGELOG.md +28 -0
- data/Gemfile +1 -1
- data/README.md +34 -1
- data/Rakefile +55 -0
- data/beaneater.gemspec +8 -1
- data/lib/beaneater/arguments.rb +66 -0
- data/lib/beaneater/configuration.rb +8 -0
- data/lib/beaneater/connection.rb +322 -61
- data/lib/beaneater/errors.rb +14 -0
- data/lib/beaneater/job/collection.rb +45 -7
- data/lib/beaneater/job/record.rb +17 -8
- data/lib/beaneater/stats/fast_struct.rb +7 -0
- data/lib/beaneater/stats/stat_struct.rb +8 -2
- data/lib/beaneater/stats.rb +6 -0
- data/lib/beaneater/tube/collection.rb +8 -6
- data/lib/beaneater/tube/record.rb +6 -5
- data/lib/beaneater/version.rb +1 -1
- data/lib/beaneater.rb +1 -1
- data/test/arguments_test.rb +174 -0
- data/test/connection_test.rb +628 -16
- data/test/job_test.rb +36 -0
- data/test/jobs_test.rb +440 -0
- data/test/stat_struct_test.rb +30 -0
- data/test/stats_test.rb +16 -0
- data/test/test_helper.rb +45 -0
- data/test/tube_test.rb +123 -0
- data/test/tubes_test.rb +30 -1
- metadata +12 -6
- data/.travis.yml +0 -15
data/test/job_test.rb
CHANGED
|
@@ -37,6 +37,14 @@ describe Beaneater::Job do
|
|
|
37
37
|
assert_equal "foo bury #{@time}", @tube.peek(:buried).body
|
|
38
38
|
end
|
|
39
39
|
|
|
40
|
+
it "should not look up the job's stats when pri is given" do
|
|
41
|
+
job = @tube.reserve
|
|
42
|
+
job.expects(:stats).never
|
|
43
|
+
job.bury(:pri => 10)
|
|
44
|
+
assert_equal 'buried', @beanstalk.jobs.find(job.id).stats.state
|
|
45
|
+
assert_equal 10, @beanstalk.jobs.find(job.id).stats.pri
|
|
46
|
+
end
|
|
47
|
+
|
|
40
48
|
it "should not bury if not reserved" do
|
|
41
49
|
job = @tube.peek(:ready)
|
|
42
50
|
assert_raises(Beaneater::JobNotReserved) { job.bury }
|
|
@@ -76,6 +84,23 @@ describe Beaneater::Job do
|
|
|
76
84
|
assert_equal 2, job.stats.delay
|
|
77
85
|
end
|
|
78
86
|
|
|
87
|
+
it "should not look up the job's stats when pri and delay are given" do
|
|
88
|
+
job = @tube.reserve
|
|
89
|
+
job.expects(:stats).never
|
|
90
|
+
job.release :pri => 10, :delay => 2
|
|
91
|
+
job_stats = @beanstalk.jobs.find(job.id).stats
|
|
92
|
+
assert_equal 'delayed', job_stats.state
|
|
93
|
+
assert_equal [10, 2], [job_stats.pri, job_stats.delay]
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
it "should look up the job's stats once for the pri and delay it keeps" do
|
|
97
|
+
job = @tube.reserve
|
|
98
|
+
job.expects(:stats).once.returns(@beanstalk.jobs.find(job.id).stats)
|
|
99
|
+
job.release :delay => 2
|
|
100
|
+
job_stats = @beanstalk.jobs.find(job.id).stats
|
|
101
|
+
assert_equal [5, 2], [job_stats.pri, job_stats.delay]
|
|
102
|
+
end
|
|
103
|
+
|
|
79
104
|
it "should not released if not reserved" do
|
|
80
105
|
job = @tube.peek(:ready)
|
|
81
106
|
assert_raises(Beaneater::JobNotReserved) { job.release }
|
|
@@ -208,6 +233,17 @@ describe Beaneater::Job do
|
|
|
208
233
|
assert_equal @tube.name, job.tube
|
|
209
234
|
job.release
|
|
210
235
|
end
|
|
236
|
+
|
|
237
|
+
it("should keep dashes and dots in the tube name") do
|
|
238
|
+
tube = @beanstalk.tubes.find('email-jobs.v1')
|
|
239
|
+
tube.put 'bar'
|
|
240
|
+
job = tube.reserve
|
|
241
|
+
assert_equal 'email-jobs.v1', job.tube
|
|
242
|
+
assert_equal 'email-jobs.v1', job.stats.tube
|
|
243
|
+
assert_equal 'reserved', job.stats.state
|
|
244
|
+
assert_kind_of Integer, job.stats.id
|
|
245
|
+
job.delete
|
|
246
|
+
end
|
|
211
247
|
end # tube
|
|
212
248
|
|
|
213
249
|
describe "for #pri" do
|
data/test/jobs_test.rb
CHANGED
|
@@ -58,6 +58,17 @@ describe Beaneater::Jobs do
|
|
|
58
58
|
end
|
|
59
59
|
end # register!
|
|
60
60
|
|
|
61
|
+
describe "for process! with no processors" do
|
|
62
|
+
it "should raise ArgumentError without sending a command" do
|
|
63
|
+
client = Beaneater.new('localhost') # not @beanstalk, which the teardown uses
|
|
64
|
+
client.connection.expects(:transmit).never
|
|
65
|
+
error = assert_raises(ArgumentError) { Beaneater::Jobs.new(client).process! }
|
|
66
|
+
assert_match(/register/, error.message)
|
|
67
|
+
ensure
|
|
68
|
+
client.close if client
|
|
69
|
+
end
|
|
70
|
+
end # process! with no processors
|
|
71
|
+
|
|
61
72
|
describe "for process!" do
|
|
62
73
|
before do
|
|
63
74
|
$foo = []
|
|
@@ -106,4 +117,433 @@ describe Beaneater::Jobs do
|
|
|
106
117
|
assert_equal 1, @beanstalk.tubes.find('tube_buried').stats.current_jobs_buried
|
|
107
118
|
end
|
|
108
119
|
end # for_process!
|
|
120
|
+
|
|
121
|
+
describe "for process! with a dashed tube name" do
|
|
122
|
+
before do
|
|
123
|
+
$ran = []
|
|
124
|
+
cleanup_tubes!(['email-jobs', 'tube_stop'])
|
|
125
|
+
@jobs.register('email-jobs') { |job| $ran << job.body }
|
|
126
|
+
@jobs.register('tube_stop') { |job| $ran << job.body; @jobs.stop! }
|
|
127
|
+
@beanstalk.tubes.find('email-jobs').put('email', :pri => 1)
|
|
128
|
+
@beanstalk.tubes.find('tube_stop').put('stop', :pri => 2)
|
|
129
|
+
@jobs.process!(:release_delay => 0)
|
|
130
|
+
end
|
|
131
|
+
|
|
132
|
+
it "should run the handler registered for the tube" do
|
|
133
|
+
assert_equal ['email', 'stop'], $ran
|
|
134
|
+
assert_nil @beanstalk.tubes.find('email-jobs').peek(:buried)
|
|
135
|
+
end
|
|
136
|
+
end # process! with a dashed tube name
|
|
137
|
+
|
|
138
|
+
describe "for process! when a failing handler no longer holds its job" do
|
|
139
|
+
before do
|
|
140
|
+
$foo = []
|
|
141
|
+
cleanup_tubes!(['tube_lost'])
|
|
142
|
+
end
|
|
143
|
+
|
|
144
|
+
after do
|
|
145
|
+
cleanup_tubes!(['tube_lost'])
|
|
146
|
+
end
|
|
147
|
+
|
|
148
|
+
# The handler loses 'first', puts 'second' and raises. 'second' outranks 'first', so it is
|
|
149
|
+
# reserved next whatever happened to 'first'.
|
|
150
|
+
def process_until_second_job(ttr = 120, &lose_job)
|
|
151
|
+
tube = @beanstalk.tubes.find('tube_lost')
|
|
152
|
+
@jobs.register('tube_lost') do |job|
|
|
153
|
+
$foo << job.body
|
|
154
|
+
if job.body == 'second'
|
|
155
|
+
@jobs.stop!
|
|
156
|
+
else
|
|
157
|
+
lose_job.call(job)
|
|
158
|
+
tube.put('second', :pri => 0)
|
|
159
|
+
raise RuntimeError
|
|
160
|
+
end
|
|
161
|
+
end
|
|
162
|
+
tube.put('first', :pri => 1, :ttr => ttr)
|
|
163
|
+
Timeout.timeout(15) { @jobs.process!(:reserve_timeout => 1) }
|
|
164
|
+
end
|
|
165
|
+
|
|
166
|
+
it "keeps processing when the handler deleted the job" do
|
|
167
|
+
process_until_second_job { |job| job.delete }
|
|
168
|
+
assert_equal ['first', 'second'], $foo
|
|
169
|
+
end
|
|
170
|
+
|
|
171
|
+
it "keeps processing when the handler released the job" do
|
|
172
|
+
process_until_second_job { |job| job.release(:delay => 5) }
|
|
173
|
+
assert_equal ['first', 'second'], $foo
|
|
174
|
+
assert_equal 1, @beanstalk.tubes.find('tube_lost').stats.current_jobs_delayed
|
|
175
|
+
end
|
|
176
|
+
|
|
177
|
+
it "keeps processing when the handler overran the job's TTR" do
|
|
178
|
+
process_until_second_job(1) do |job|
|
|
179
|
+
# the server releases a job some time after its TTR runs out
|
|
180
|
+
deadline = Time.now + 10
|
|
181
|
+
sleep 0.1 while job.stats.state == 'reserved' && Time.now < deadline
|
|
182
|
+
refute_equal 'reserved', job.stats.state
|
|
183
|
+
end
|
|
184
|
+
assert_equal ['first', 'second'], $foo
|
|
185
|
+
end
|
|
186
|
+
end
|
|
187
|
+
|
|
188
|
+
describe "for process! with stop!" do
|
|
189
|
+
before do
|
|
190
|
+
cleanup_tubes!(['tube_stop'])
|
|
191
|
+
end
|
|
192
|
+
|
|
193
|
+
after do
|
|
194
|
+
cleanup_tubes!(['tube_stop'])
|
|
195
|
+
end
|
|
196
|
+
|
|
197
|
+
# Runs process! in a thread and yields. Returns :returned, the exception process! raised, or
|
|
198
|
+
# :still_running if it hadn't finished within seconds.
|
|
199
|
+
def process_in_thread(seconds)
|
|
200
|
+
worker = Thread.new do
|
|
201
|
+
begin
|
|
202
|
+
@jobs.process!(:reserve_timeout => 1)
|
|
203
|
+
:returned
|
|
204
|
+
rescue StandardError => ex
|
|
205
|
+
ex
|
|
206
|
+
end
|
|
207
|
+
end
|
|
208
|
+
yield if block_given?
|
|
209
|
+
return worker.value if worker.join(seconds)
|
|
210
|
+
worker.kill
|
|
211
|
+
:still_running
|
|
212
|
+
end
|
|
213
|
+
|
|
214
|
+
it "returns within one reserve_timeout when stop! is called while idle" do
|
|
215
|
+
@jobs.register('tube_stop') { |job| }
|
|
216
|
+
result = process_in_thread(3) do
|
|
217
|
+
sleep 0.5
|
|
218
|
+
@jobs.stop!
|
|
219
|
+
end
|
|
220
|
+
assert_equal :returned, result
|
|
221
|
+
end
|
|
222
|
+
|
|
223
|
+
it "returns when stop! is called by a handler whose job another worker took over" do
|
|
224
|
+
other = Beaneater.new('localhost')
|
|
225
|
+
other.tubes.watch!('tube_stop')
|
|
226
|
+
runs = 0
|
|
227
|
+
@jobs.register('tube_stop') do |job|
|
|
228
|
+
runs += 1
|
|
229
|
+
job.release # as the server does once a job's TTR runs out
|
|
230
|
+
other.tubes.reserve(1)
|
|
231
|
+
@jobs.stop!
|
|
232
|
+
end
|
|
233
|
+
@beanstalk.tubes.find('tube_stop').put('taken over')
|
|
234
|
+
|
|
235
|
+
assert_equal :returned, process_in_thread(5)
|
|
236
|
+
assert_equal 1, runs
|
|
237
|
+
ensure
|
|
238
|
+
other.close if other
|
|
239
|
+
end
|
|
240
|
+
end
|
|
241
|
+
|
|
242
|
+
describe "for process! when the job_parser rejects a job" do
|
|
243
|
+
before do
|
|
244
|
+
cleanup_tubes!(['tube_bad_body'])
|
|
245
|
+
Beaneater.configure.job_parser = lambda { |body| JSON.parse(body) }
|
|
246
|
+
end
|
|
247
|
+
|
|
248
|
+
after do
|
|
249
|
+
Beaneater.configure.job_parser = lambda { |body| body }
|
|
250
|
+
cleanup_tubes!(['tube_bad_body'])
|
|
251
|
+
end
|
|
252
|
+
|
|
253
|
+
it "buries the job and goes on to the next one" do
|
|
254
|
+
handled = []
|
|
255
|
+
@jobs.register('tube_bad_body') do |job|
|
|
256
|
+
handled << job.body
|
|
257
|
+
@jobs.stop!
|
|
258
|
+
end
|
|
259
|
+
tube = @beanstalk.tubes.find('tube_bad_body')
|
|
260
|
+
tube.put('not json', :pri => 1)
|
|
261
|
+
tube.put('{"ok":1}', :pri => 2)
|
|
262
|
+
|
|
263
|
+
Timeout.timeout(10) { @jobs.process!(:reserve_timeout => 1) }
|
|
264
|
+
assert_equal [{ 'ok' => 1 }], handled
|
|
265
|
+
Beaneater.configure.job_parser = lambda { |body| body }
|
|
266
|
+
buried = tube.peek(:buried)
|
|
267
|
+
refute_nil buried, "the job the parser rejected was not buried"
|
|
268
|
+
assert_equal 'not json', buried.body
|
|
269
|
+
end
|
|
270
|
+
|
|
271
|
+
# process! goes on after NotFoundError (and JobNotReserved, TimedOutError); the job it had
|
|
272
|
+
# must not be taken for the bad job that follows.
|
|
273
|
+
def put_after_retry_jobs
|
|
274
|
+
tube = @beanstalk.tubes.find('tube_bad_body')
|
|
275
|
+
first = tube.put('{"first":1}', :pri => 1)[:id]
|
|
276
|
+
bad = tube.put('not json', :pri => 2)[:id]
|
|
277
|
+
tube.put('{"stop":1}', :pri => 3)
|
|
278
|
+
[tube, first, bad]
|
|
279
|
+
end
|
|
280
|
+
|
|
281
|
+
def stats_of(id)
|
|
282
|
+
Beaneater.configure.job_parser = lambda { |body| body }
|
|
283
|
+
@beanstalk.connection.transmit("stats-job #{id}")[:body]
|
|
284
|
+
end
|
|
285
|
+
|
|
286
|
+
it "buries the bad job after a handler deleted its own job" do
|
|
287
|
+
@jobs.register('tube_bad_body') do |job|
|
|
288
|
+
job.body == { 'stop' => 1 } ? @jobs.stop! : job.delete # process!'s own delete then gets NOT_FOUND
|
|
289
|
+
end
|
|
290
|
+
tube, _first, bad = put_after_retry_jobs
|
|
291
|
+
Timeout.timeout(10) { @jobs.process!(:reserve_timeout => 1) }
|
|
292
|
+
assert_equal 'buried', stats_of(bad)['state']
|
|
293
|
+
end
|
|
294
|
+
|
|
295
|
+
it "buries both the failed job and the bad job after a handler raised NotFoundError" do
|
|
296
|
+
@jobs.register('tube_bad_body') do |job|
|
|
297
|
+
if job.body == { 'stop' => 1 }
|
|
298
|
+
@jobs.stop!
|
|
299
|
+
else
|
|
300
|
+
@beanstalk.connection.transmit('delete 999999') # NOT_FOUND for some other job
|
|
301
|
+
end
|
|
302
|
+
end
|
|
303
|
+
tube, first, bad = put_after_retry_jobs
|
|
304
|
+
Timeout.timeout(10) { @jobs.process!(:reserve_timeout => 1) }
|
|
305
|
+
assert_equal 'buried', stats_of(bad)['state']
|
|
306
|
+
assert_equal 'buried', stats_of(first)['state'] # its handler failed, with the job still reserved
|
|
307
|
+
ensure
|
|
308
|
+
begin
|
|
309
|
+
@beanstalk.connection.transmit("delete #{first}") if first
|
|
310
|
+
rescue Beaneater::NotFoundError
|
|
311
|
+
nil
|
|
312
|
+
end
|
|
313
|
+
end
|
|
314
|
+
|
|
315
|
+
it "buries the job when the parser raises an error process! handles itself" do
|
|
316
|
+
Beaneater.configure.job_parser = lambda do |body|
|
|
317
|
+
raise Beaneater::NotFoundError.new('NOT_FOUND', 'parse') if body == 'not found'
|
|
318
|
+
raise Beaneater::TimedOutError.new('TIMED_OUT', 'parse') if body == 'timed out'
|
|
319
|
+
body
|
|
320
|
+
end
|
|
321
|
+
handled = []
|
|
322
|
+
@jobs.register('tube_bad_body') do |job|
|
|
323
|
+
handled << job.body
|
|
324
|
+
@jobs.stop!
|
|
325
|
+
end
|
|
326
|
+
tube = @beanstalk.tubes.find('tube_bad_body')
|
|
327
|
+
not_found = tube.put('not found', :pri => 1, :ttr => 2)[:id]
|
|
328
|
+
timed_out = tube.put('timed out', :pri => 2, :ttr => 2)[:id]
|
|
329
|
+
tube.put('stop', :pri => 3)
|
|
330
|
+
|
|
331
|
+
Timeout.timeout(10) { @jobs.process!(:reserve_timeout => 1) }
|
|
332
|
+
assert_equal ['stop'], handled
|
|
333
|
+
[not_found, timed_out].each do |id|
|
|
334
|
+
stats = stats_of(id)
|
|
335
|
+
assert_equal 'buried', stats['state'], "job #{id}"
|
|
336
|
+
assert_equal 0, stats['timeouts'], "job #{id}"
|
|
337
|
+
end
|
|
338
|
+
end
|
|
339
|
+
|
|
340
|
+
it "buries the job and stops when the parser raises AbortProcessingError" do
|
|
341
|
+
Beaneater.configure.job_parser = lambda { |body| raise Beaneater::AbortProcessingError if body == 'abort'; body }
|
|
342
|
+
@jobs.register('tube_bad_body') { |job| }
|
|
343
|
+
id = @beanstalk.tubes.find('tube_bad_body').put('abort')[:id]
|
|
344
|
+
Timeout.timeout(10) { @jobs.process!(:reserve_timeout => 1) }
|
|
345
|
+
assert_equal 'buried', stats_of(id)['state']
|
|
346
|
+
end
|
|
347
|
+
|
|
348
|
+
it "buries the job when the parser raises a frozen exception" do
|
|
349
|
+
frozen = ArgumentError.new('frozen').freeze
|
|
350
|
+
Beaneater.configure.job_parser = lambda { |body| body == 'bad' ? raise(frozen) : body }
|
|
351
|
+
handled = []
|
|
352
|
+
@jobs.register('tube_bad_body') do |job|
|
|
353
|
+
handled << job.body
|
|
354
|
+
@jobs.stop!
|
|
355
|
+
end
|
|
356
|
+
tube = @beanstalk.tubes.find('tube_bad_body')
|
|
357
|
+
tube.put('bad', :pri => 1)
|
|
358
|
+
tube.put('good', :pri => 2)
|
|
359
|
+
|
|
360
|
+
Timeout.timeout(10) { @jobs.process!(:reserve_timeout => 1) }
|
|
361
|
+
assert_equal ['good'], handled
|
|
362
|
+
Beaneater.configure.job_parser = lambda { |body| body }
|
|
363
|
+
assert_equal 'bad', (tube.peek(:buried) || flunk("the bad job was not buried")).body
|
|
364
|
+
end
|
|
365
|
+
|
|
366
|
+
it "buries the handler's job, not the one its own reserve rejected, when a handler raises it" do
|
|
367
|
+
cleanup_tubes!(['tube_bad_body_other'])
|
|
368
|
+
other_id = @beanstalk.tubes.find('tube_bad_body_other').put('not json')[:id]
|
|
369
|
+
@jobs.register('tube_bad_body') do |job|
|
|
370
|
+
@jobs.stop!
|
|
371
|
+
@beanstalk.tubes.watch('tube_bad_body_other')
|
|
372
|
+
@beanstalk.tubes.reserve(0) # raises: the parser rejects the other tube's job
|
|
373
|
+
end
|
|
374
|
+
@beanstalk.tubes.find('tube_bad_body').put('{"ok":1}')
|
|
375
|
+
|
|
376
|
+
Timeout.timeout(10) { @jobs.process!(:reserve_timeout => 1) }
|
|
377
|
+
Beaneater.configure.job_parser = lambda { |body| body }
|
|
378
|
+
assert_equal '{"ok":1}', (@beanstalk.tubes.find('tube_bad_body').peek(:buried) || flunk("the handler's job was not buried")).body
|
|
379
|
+
assert_equal 'reserved', @beanstalk.connection.transmit("stats-job #{other_id}")[:body]['state']
|
|
380
|
+
ensure
|
|
381
|
+
Beaneater.configure.job_parser = lambda { |body| body }
|
|
382
|
+
begin
|
|
383
|
+
@beanstalk.connection.transmit("delete #{other_id}") if other_id
|
|
384
|
+
rescue Beaneater::NotFoundError
|
|
385
|
+
nil
|
|
386
|
+
end
|
|
387
|
+
cleanup_tubes!(['tube_bad_body_other'])
|
|
388
|
+
end
|
|
389
|
+
end
|
|
390
|
+
|
|
391
|
+
describe "for process! when a handler raises NotFoundError with its job reserved" do
|
|
392
|
+
before do
|
|
393
|
+
cleanup_tubes!(['tube_not_found'])
|
|
394
|
+
end
|
|
395
|
+
|
|
396
|
+
after do
|
|
397
|
+
cleanup_tubes!(['tube_not_found'])
|
|
398
|
+
end
|
|
399
|
+
|
|
400
|
+
it "buries the job once instead of reserving in a loop near its TTR" do
|
|
401
|
+
runs = 0
|
|
402
|
+
@jobs.register('tube_not_found') do |job|
|
|
403
|
+
runs += 1
|
|
404
|
+
@beanstalk.connection.transmit('delete 999999') # NOT_FOUND for some other job
|
|
405
|
+
end
|
|
406
|
+
id = @beanstalk.tubes.find('tube_not_found').put('x', :ttr => 2)[:id]
|
|
407
|
+
worker = Thread.new { @jobs.process!(:reserve_timeout => 1) }
|
|
408
|
+
sleep 3 # past the job's TTR
|
|
409
|
+
@jobs.stop!
|
|
410
|
+
assert worker.join(5), "process! did not stop"
|
|
411
|
+
checker = Beaneater.new('localhost')
|
|
412
|
+
stats = checker.connection.transmit("stats-job #{id}")[:body]
|
|
413
|
+
checker.close
|
|
414
|
+
assert_equal 1, runs
|
|
415
|
+
assert_equal 'buried', stats['state']
|
|
416
|
+
assert_equal 0, stats['timeouts']
|
|
417
|
+
end
|
|
418
|
+
|
|
419
|
+
it "goes on when the job stops being reserved just before it is buried" do
|
|
420
|
+
@jobs.register('tube_not_found') do |job|
|
|
421
|
+
@jobs.stop!
|
|
422
|
+
raise 'handler failed'
|
|
423
|
+
end
|
|
424
|
+
id = @beanstalk.tubes.find('tube_not_found').put('x')[:id]
|
|
425
|
+
Beaneater::Job.any_instance.stubs(:bury).raises(Beaneater::JobNotReserved) # another client's reservation ended
|
|
426
|
+
Timeout.timeout(10) { @jobs.process!(:reserve_timeout => 1) } # must not raise JobNotReserved
|
|
427
|
+
assert_nil @jobs.current_job
|
|
428
|
+
ensure
|
|
429
|
+
Beaneater::Job.any_instance.unstub(:bury)
|
|
430
|
+
begin
|
|
431
|
+
@beanstalk.connection.transmit("delete #{id}") if id
|
|
432
|
+
rescue Beaneater::NotFoundError
|
|
433
|
+
nil
|
|
434
|
+
end
|
|
435
|
+
end
|
|
436
|
+
|
|
437
|
+
it "releases the job for another try instead when the error is in retry_on" do
|
|
438
|
+
runs = 0
|
|
439
|
+
@jobs.register('tube_not_found', :retry_on => [Beaneater::NotFoundError], :max_retries => 1) do |job|
|
|
440
|
+
runs += 1
|
|
441
|
+
@jobs.stop! if runs == 2
|
|
442
|
+
@beanstalk.connection.transmit('delete 999999') # NOT_FOUND for some other job
|
|
443
|
+
end
|
|
444
|
+
id = @beanstalk.tubes.find('tube_not_found').put('x')[:id]
|
|
445
|
+
Timeout.timeout(10) { @jobs.process!(:reserve_timeout => 1, :release_delay => 0) }
|
|
446
|
+
stats = @beanstalk.connection.transmit("stats-job #{id}")[:body]
|
|
447
|
+
assert_equal 2, runs
|
|
448
|
+
assert_equal 1, stats['releases'] # released once, then buried once the retries ran out
|
|
449
|
+
assert_equal 'buried', stats['state']
|
|
450
|
+
end
|
|
451
|
+
end
|
|
452
|
+
|
|
453
|
+
describe "for process! when a handler raises NotConnected for another server" do
|
|
454
|
+
before do
|
|
455
|
+
cleanup_tubes!(['tube_other_down'])
|
|
456
|
+
end
|
|
457
|
+
|
|
458
|
+
it "buries the job and keeps processing" do
|
|
459
|
+
seen = []
|
|
460
|
+
@jobs.register('tube_other_down') do |job|
|
|
461
|
+
seen << job.body
|
|
462
|
+
@jobs.stop! if job.body == 'second'
|
|
463
|
+
raise Beaneater::NotConnected, "the other server is down" if job.body == 'first'
|
|
464
|
+
end
|
|
465
|
+
tube = @beanstalk.tubes.find('tube_other_down')
|
|
466
|
+
tube.put('first', :pri => 1)
|
|
467
|
+
tube.put('second', :pri => 2)
|
|
468
|
+
|
|
469
|
+
Timeout.timeout(10) { @jobs.process!(:reserve_timeout => 1) }
|
|
470
|
+
assert_equal ['first', 'second'], seen
|
|
471
|
+
assert_equal 'first', tube.peek(:buried).body
|
|
472
|
+
end
|
|
473
|
+
end
|
|
474
|
+
|
|
475
|
+
describe "for process! when the server goes away" do
|
|
476
|
+
before do
|
|
477
|
+
@port = free_port
|
|
478
|
+
@pid = start_beanstalkd(@port)
|
|
479
|
+
@client = Beaneater.new("127.0.0.1:#{@port}")
|
|
480
|
+
end
|
|
481
|
+
|
|
482
|
+
after do
|
|
483
|
+
@client.close if @client
|
|
484
|
+
stop_beanstalkd(@pid) if @pid
|
|
485
|
+
end
|
|
486
|
+
|
|
487
|
+
it "forgets the job when burying it fails, so the next run buries its own bad job" do
|
|
488
|
+
Beaneater.configure.job_parser = lambda do |body|
|
|
489
|
+
stop_beanstalkd(@pid) if body == 'kill' # the server dies before the bad job is buried
|
|
490
|
+
JSON.parse(body)
|
|
491
|
+
end
|
|
492
|
+
handled = []
|
|
493
|
+
@client.jobs.register('bury_fails') do |job|
|
|
494
|
+
handled << job.body
|
|
495
|
+
@client.jobs.stop!
|
|
496
|
+
end
|
|
497
|
+
@client.tubes.find('bury_fails').put('kill')
|
|
498
|
+
assert_raises(Beaneater::NotConnected) { Timeout.timeout(10) { @client.jobs.process!(:reserve_timeout => 1) } }
|
|
499
|
+
assert_nil @client.jobs.current_job
|
|
500
|
+
|
|
501
|
+
@pid = start_beanstalkd(@port) # a new server: job ids start again at 1
|
|
502
|
+
producer = Beaneater.new("127.0.0.1:#{@port}")
|
|
503
|
+
producer.tubes.find('bury_fails_filler').put('filler') # takes the id the lost job had
|
|
504
|
+
producer.tubes.find('bury_fails').put('not json', :pri => 1)
|
|
505
|
+
producer.tubes.find('bury_fails').put('{"ok":1}', :pri => 2)
|
|
506
|
+
Timeout.timeout(10) { @client.jobs.process!(:reserve_timeout => 1) }
|
|
507
|
+
assert_equal [{ 'ok' => 1 }], handled
|
|
508
|
+
Beaneater.configure.job_parser = lambda { |body| body }
|
|
509
|
+
buried = producer.tubes.find('bury_fails').peek(:buried)
|
|
510
|
+
refute_nil buried, "the bad job was not buried"
|
|
511
|
+
assert_equal 'not json', buried.body
|
|
512
|
+
producer.close
|
|
513
|
+
ensure
|
|
514
|
+
Beaneater.configure.job_parser = lambda { |body| body }
|
|
515
|
+
end
|
|
516
|
+
|
|
517
|
+
it "raises NotConnected instead of looping, and the client resumes once the server is back" do
|
|
518
|
+
seen = []
|
|
519
|
+
@client.jobs.register('reconnect') do |job|
|
|
520
|
+
seen << job.body
|
|
521
|
+
@client.jobs.stop! if job.body == 'after restart'
|
|
522
|
+
end
|
|
523
|
+
@client.tubes.find('reconnect').put('before outage')
|
|
524
|
+
|
|
525
|
+
worker = Thread.new do
|
|
526
|
+
begin
|
|
527
|
+
@client.jobs.process!(:reserve_timeout => 1)
|
|
528
|
+
rescue StandardError => ex
|
|
529
|
+
ex
|
|
530
|
+
end
|
|
531
|
+
end
|
|
532
|
+
Timeout.timeout(5) { sleep 0.05 while seen.empty? }
|
|
533
|
+
stop_beanstalkd(@pid)
|
|
534
|
+
|
|
535
|
+
finished = worker.join(10)
|
|
536
|
+
worker.kill unless finished
|
|
537
|
+
assert finished, "process! was still running 10s after the server went away"
|
|
538
|
+
assert_kind_of Beaneater::NotConnected, worker.value
|
|
539
|
+
|
|
540
|
+
@pid = start_beanstalkd(@port)
|
|
541
|
+
producer = Beaneater.new("127.0.0.1:#{@port}")
|
|
542
|
+
producer.tubes.find('reconnect').put('after restart')
|
|
543
|
+
producer.close
|
|
544
|
+
|
|
545
|
+
Timeout.timeout(10) { @client.jobs.process!(:reserve_timeout => 1) }
|
|
546
|
+
assert_equal ['before outage', 'after restart'], seen
|
|
547
|
+
end
|
|
548
|
+
end
|
|
109
549
|
end # Beaneater::Jobs
|
data/test/stat_struct_test.rb
CHANGED
|
@@ -29,6 +29,24 @@ describe Beaneater::StatStruct do
|
|
|
29
29
|
assert_equal 'foo', @struct[:baz]
|
|
30
30
|
assert_equal 'demo', @struct[:"under_score"]
|
|
31
31
|
end
|
|
32
|
+
|
|
33
|
+
it "should look up hyphenated beanstalkd keys" do
|
|
34
|
+
assert_equal 'demo', @struct['under-score']
|
|
35
|
+
assert_equal 'demo', @struct[:"under-score"]
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
it "should read a stat whose name is also a method name" do
|
|
39
|
+
struct = Beaneater::StatStruct.from_hash('hash' => 'h', 'class' => 'c')
|
|
40
|
+
assert_equal 'h', struct['hash']
|
|
41
|
+
assert_equal 'c', struct[:class]
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
it "should return nil for a key that is not a stat" do
|
|
45
|
+
[:object_id, 'send', 'freeze', 'foo=', 'no such key'].each do |key|
|
|
46
|
+
assert_nil @struct[key], "Expected nil for #{key.inspect}"
|
|
47
|
+
end
|
|
48
|
+
refute @struct.frozen?
|
|
49
|
+
end
|
|
32
50
|
end # []
|
|
33
51
|
|
|
34
52
|
describe "for #keys" do
|
|
@@ -41,6 +59,18 @@ describe Beaneater::StatStruct do
|
|
|
41
59
|
end
|
|
42
60
|
end # keys
|
|
43
61
|
|
|
62
|
+
describe "for #respond_to?" do
|
|
63
|
+
it "should respond to a key before it is first read" do
|
|
64
|
+
struct = Beaneater::StatStruct.from_hash('rusage-utime' => 0.5)
|
|
65
|
+
assert struct.respond_to?(:rusage_utime), "Expected struct to respond to rusage_utime"
|
|
66
|
+
assert_equal 0.5, struct.method(:rusage_utime).call
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
it "should not respond to a name that is not a key" do
|
|
70
|
+
refute @struct.respond_to?(:no_such_key)
|
|
71
|
+
end
|
|
72
|
+
end # respond_to?
|
|
73
|
+
|
|
44
74
|
describe "for #to_h" do
|
|
45
75
|
it "should return 4 keys / values" do
|
|
46
76
|
assert_equal 4, @struct.to_h.size
|
data/test/stats_test.rb
CHANGED
|
@@ -42,4 +42,20 @@ describe Beaneater::Stats do
|
|
|
42
42
|
assert_raises(NoMethodError) { @stats.cmd }
|
|
43
43
|
end
|
|
44
44
|
end # method_missing
|
|
45
|
+
|
|
46
|
+
describe 'for #respond_to?' do
|
|
47
|
+
before do
|
|
48
|
+
connection = stub(:transmit => { :body => { 'max-job-size' => 65535 }, :status => "OK" })
|
|
49
|
+
@stats = Beaneater::Stats.new(stub(:connection => connection))
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
it "should respond to a stats key before it is first read" do
|
|
53
|
+
assert @stats.respond_to?(:max_job_size), "Expected stats to respond to max_job_size"
|
|
54
|
+
assert_equal 65535, @stats.method(:max_job_size).call
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
it "should not respond to a name that is not a stats key" do
|
|
58
|
+
refute @stats.respond_to?(:no_such_stat)
|
|
59
|
+
end
|
|
60
|
+
end # respond_to?
|
|
45
61
|
end # Beaneater::Stats
|
data/test/test_helper.rb
CHANGED
|
@@ -45,4 +45,49 @@ class MiniTest::Unit::TestCase
|
|
|
45
45
|
def teardown
|
|
46
46
|
flush_all
|
|
47
47
|
end
|
|
48
|
+
|
|
49
|
+
# The beanstalkd binary for tests that stop and restart their own server: BEANSTALKD_BIN, or
|
|
50
|
+
# `beanstalkd` on PATH. Skips the test when neither is available.
|
|
51
|
+
def beanstalkd_bin
|
|
52
|
+
bin = ENV['BEANSTALKD_BIN']
|
|
53
|
+
return bin if bin && !bin.empty?
|
|
54
|
+
on_path = ENV['PATH'].to_s.split(File::PATH_SEPARATOR).any? do |dir|
|
|
55
|
+
File.executable?(File.join(dir, 'beanstalkd'))
|
|
56
|
+
end
|
|
57
|
+
skip("beanstalkd not found: put it on PATH or set BEANSTALKD_BIN") unless on_path
|
|
58
|
+
'beanstalkd'
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
# A free local port for a private beanstalkd
|
|
62
|
+
def free_port
|
|
63
|
+
server = TCPServer.new('127.0.0.1', 0)
|
|
64
|
+
server.addr[1]
|
|
65
|
+
ensure
|
|
66
|
+
server.close if server
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
# Starts a private beanstalkd on port and waits until it accepts connections.
|
|
70
|
+
#
|
|
71
|
+
# @return [Integer] the server's pid
|
|
72
|
+
def start_beanstalkd(port)
|
|
73
|
+
pid = Process.spawn(beanstalkd_bin, '-l', '127.0.0.1', '-p', port.to_s, [:out, :err] => File::NULL)
|
|
74
|
+
50.times do
|
|
75
|
+
begin
|
|
76
|
+
TCPSocket.new('127.0.0.1', port).close
|
|
77
|
+
return pid
|
|
78
|
+
rescue Errno::ECONNREFUSED
|
|
79
|
+
sleep 0.05
|
|
80
|
+
end
|
|
81
|
+
end
|
|
82
|
+
stop_beanstalkd(pid)
|
|
83
|
+
flunk "beanstalkd did not start on port #{port}"
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
# Kills a server started by start_beanstalkd
|
|
87
|
+
def stop_beanstalkd(pid)
|
|
88
|
+
Process.kill('KILL', pid)
|
|
89
|
+
Process.wait(pid)
|
|
90
|
+
rescue Errno::ESRCH, Errno::ECHILD
|
|
91
|
+
nil
|
|
92
|
+
end
|
|
48
93
|
end
|