forked from resque/resque
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathworker_test.rb
More file actions
429 lines (346 loc) · 11.6 KB
/
worker_test.rb
File metadata and controls
429 lines (346 loc) · 11.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
require 'test_helper'
describe "Resque::Worker" do
include Test::Unit::Assertions
before do
Resque.redis = Resque.redis # reset state in Resque object
Resque.redis.flushall
Resque.before_first_fork = nil
Resque.before_fork = nil
Resque.after_fork = nil
@worker = Resque::Worker.new(:jobs)
Resque::Job.create(:jobs, SomeJob, 20, '/tmp')
end
it "can fail jobs" do
Resque::Job.create(:jobs, BadJob)
@worker.work(0)
assert_equal 1, Resque::Failure.count
end
it "failed jobs report exception and message" do
Resque::Job.create(:jobs, BadJobWithSyntaxError)
@worker.work(0)
assert_equal('SyntaxError', Resque::Failure.all['exception'])
assert_equal('Extra Bad job!', Resque::Failure.all['error'])
end
it "does not allow exceptions from failure backend to escape" do
job = Resque::Job.new(:jobs, {})
with_failure_backend BadFailureBackend do
@worker.perform job
end
end
it "fails uncompleted jobs on exit" do
job = Resque::Job.new(:jobs, {'class' => 'GoodJob', 'args' => "blah"})
@worker.working_on(job)
@worker.unregister_worker
assert_equal 1, Resque::Failure.count
end
class ::SimpleJobWithFailureHandling
def self.on_failure_record_failure(exception, *job_args)
@@exception = exception
end
def self.exception
@@exception
end
end
it "fails uncompleted jobs on exit, and calls failure hook" do
job = Resque::Job.new(:jobs, {'class' => 'SimpleJobWithFailureHandling', 'args' => ""})
@worker.working_on(job)
@worker.unregister_worker
assert_equal 1, Resque::Failure.count
assert(SimpleJobWithFailureHandling.exception.kind_of?(Resque::DirtyExit))
end
it "can peek at failed jobs" do
10.times { Resque::Job.create(:jobs, BadJob) }
@worker.work(0)
assert_equal 10, Resque::Failure.count
assert_equal 10, Resque::Failure.all(0, 20).size
end
it "can clear failed jobs" do
Resque::Job.create(:jobs, BadJob)
@worker.work(0)
assert_equal 1, Resque::Failure.count
Resque::Failure.clear
assert_equal 0, Resque::Failure.count
end
it "catches exceptional jobs" do
Resque::Job.create(:jobs, BadJob)
Resque::Job.create(:jobs, BadJob)
@worker.process
@worker.process
@worker.process
assert_equal 2, Resque::Failure.count
end
it "strips whitespace from queue names" do
queues = "critical, high, low".split(',')
worker = Resque::Worker.new(*queues)
assert_equal %w( critical high low ), worker.queues
end
it "can work on multiple queues" do
Resque::Job.create(:high, GoodJob)
Resque::Job.create(:critical, GoodJob)
worker = Resque::Worker.new(:critical, :high)
worker.process
assert_equal 1, Resque.size(:high)
assert_equal 0, Resque.size(:critical)
worker.process
assert_equal 0, Resque.size(:high)
end
it "can work on all queues" do
Resque::Job.create(:high, GoodJob)
Resque::Job.create(:critical, GoodJob)
Resque::Job.create(:blahblah, GoodJob)
worker = Resque::Worker.new("*")
worker.work(0)
assert_equal 0, Resque.size(:high)
assert_equal 0, Resque.size(:critical)
assert_equal 0, Resque.size(:blahblah)
end
it "can work with wildcard at the end of the list" do
Resque::Job.create(:high, GoodJob)
Resque::Job.create(:critical, GoodJob)
Resque::Job.create(:blahblah, GoodJob)
Resque::Job.create(:beer, GoodJob)
worker = Resque::Worker.new(:critical, :high, "*")
worker.work(0)
assert_equal 0, Resque.size(:high)
assert_equal 0, Resque.size(:critical)
assert_equal 0, Resque.size(:blahblah)
assert_equal 0, Resque.size(:beer)
end
it "can work with wildcard at the middle of the list" do
Resque::Job.create(:high, GoodJob)
Resque::Job.create(:critical, GoodJob)
Resque::Job.create(:blahblah, GoodJob)
Resque::Job.create(:beer, GoodJob)
worker = Resque::Worker.new(:critical, "*", :high)
worker.work(0)
assert_equal 0, Resque.size(:high)
assert_equal 0, Resque.size(:critical)
assert_equal 0, Resque.size(:blahblah)
assert_equal 0, Resque.size(:beer)
end
it "processes * queues in alphabetical order" do
Resque::Job.create(:high, GoodJob)
Resque::Job.create(:critical, GoodJob)
Resque::Job.create(:blahblah, GoodJob)
worker = Resque::Worker.new("*")
processed_queues = []
worker.work(0) do |job|
processed_queues << job.queue
end
assert_equal %w( jobs high critical blahblah ).sort, processed_queues
end
it "can work with dynamically added queues when using wildcard" do
worker = Resque::Worker.new("*")
assert_equal ["jobs"], Resque.queues
Resque::Job.create(:high, GoodJob)
Resque::Job.create(:critical, GoodJob)
Resque::Job.create(:blahblah, GoodJob)
processed_queues = []
worker.work(0) do |job|
processed_queues << job.queue
end
assert_equal %w( jobs high critical blahblah ).sort, processed_queues
end
it "has a unique id" do
assert_equal "#{`hostname`.chomp}:#{$$}:jobs", @worker.to_s
end
it "complains if no queues are given" do
assert_raise Resque::NoQueueError do
Resque::Worker.new
end
end
it "fails if a job class has no `perform` method" do
worker = Resque::Worker.new(:perform_less)
Resque::Job.create(:perform_less, Object)
assert_equal 0, Resque::Failure.count
worker.work(0)
assert_equal 1, Resque::Failure.count
end
it "inserts itself into the 'workers' list on startup" do
@worker.work(0) do
assert_equal @worker, Resque.workers[0]
end
end
it "removes itself from the 'workers' list on shutdown" do
@worker.work(0) do
assert_equal @worker, Resque.workers[0]
end
assert_equal [], Resque.workers
end
it "removes worker with stringified id" do
@worker.work(0) do
worker_id = Resque.workers[0].to_s
Resque.remove_worker(worker_id)
assert_equal [], Resque.workers
end
end
it "records what it is working on" do
@worker.work(0) do
task = @worker.job
assert_equal({"args"=>[20, "/tmp"], "class"=>"SomeJob"}, task['payload'])
assert task['run_at']
assert_equal 'jobs', task['queue']
end
end
it "clears its status when not working on anything" do
@worker.work(0)
assert_equal Hash.new, @worker.job
end
it "knows when it is working" do
@worker.work(0) do
assert @worker.working?
end
end
it "knows when it is idle" do
@worker.work(0)
assert @worker.idle?
end
it "knows who is working" do
@worker.work(0) do
assert_equal [@worker], Resque.working
end
end
it "keeps track of how many jobs it has processed" do
Resque::Job.create(:jobs, BadJob)
Resque::Job.create(:jobs, BadJob)
3.times do
job = @worker.reserve
@worker.process job
end
assert_equal 3, @worker.processed
end
it "keeps track of how many failures it has seen" do
Resque::Job.create(:jobs, BadJob)
Resque::Job.create(:jobs, BadJob)
3.times do
job = @worker.reserve
@worker.process job
end
assert_equal 2, @worker.failed
end
it "stats are erased when the worker goes away" do
@worker.work(0)
assert_equal 0, @worker.processed
assert_equal 0, @worker.failed
end
it "knows when it started" do
time = Time.now
@worker.work(0) do
assert_equal time.to_s, @worker.started.to_s
end
end
it "knows whether it exists or not" do
@worker.work(0) do
assert Resque::Worker.exists?(@worker)
assert !Resque::Worker.exists?('blah-blah')
end
end
it "sets $0 while working" do
@worker.work(0) do
ver = Resque::Version
assert_equal "resque-#{ver}: Processing jobs since #{Time.now.to_i}", $0
end
end
it "can be found" do
@worker.work(0) do
found = Resque::Worker.find(@worker.to_s)
assert_equal @worker.to_s, found.to_s
assert found.working?
assert_equal @worker.job, found.job
end
end
it "doesn't find fakes" do
@worker.work(0) do
found = Resque::Worker.find('blah-blah')
assert_equal nil, found
end
end
it "cleans up dead worker info on start (crash recovery)" do
# first we fake out two dead workers
workerA = Resque::Worker.new(:jobs)
workerA.instance_variable_set(:@to_s, "#{`hostname`.chomp}:1:jobs")
workerA.register_worker
workerB = Resque::Worker.new(:high, :low)
workerB.instance_variable_set(:@to_s, "#{`hostname`.chomp}:2:high,low")
workerB.register_worker
assert_equal 2, Resque.workers.size
# then we prune them
@worker.work(0) do
assert_equal 1, Resque.workers.size
end
end
it "worker_pids returns pids" do
known_workers = @worker.worker_pids
assert !known_workers.empty?
end
it "Processed jobs count" do
@worker.work(0)
assert_equal 1, Resque.info[:processed]
end
it "Will call a before_first_fork hook only once" do
$BEFORE_FORK_CALLED = 0
Resque.before_first_fork = Proc.new { $BEFORE_FORK_CALLED += 1 }
workerA = Resque::Worker.new(:jobs)
Resque::Job.create(:jobs, SomeJob, 20, '/tmp')
assert_equal 0, $BEFORE_FORK_CALLED
workerA.work(0)
assert_equal 1, $BEFORE_FORK_CALLED
# TODO: Verify it's only run once. Not easy.
# workerA.work(0)
# assert_equal 1, $BEFORE_FORK_CALLED
end
it "Will call a before_fork hook before forking" do
$BEFORE_FORK_CALLED = false
Resque.before_fork = Proc.new { $BEFORE_FORK_CALLED = true }
workerA = Resque::Worker.new(:jobs)
assert !$BEFORE_FORK_CALLED
Resque::Job.create(:jobs, SomeJob, 20, '/tmp')
workerA.work(0)
assert $BEFORE_FORK_CALLED
end
it "very verbose works in the afternoon" do
begin
require 'time'
last_puts = ""
Time.fake_time = Time.parse("15:44:33 2011-03-02")
@worker.extend(Module.new {
define_method(:puts) { |thing| last_puts = thing }
})
@worker.very_verbose = true
@worker.log("some log text")
assert_match /\*\* \[15:44:33 2011-03-02\] \d+: some log text/, last_puts
ensure
Time.fake_time = nil
end
end
it "Will call an after_fork hook after forking" do
$AFTER_FORK_CALLED = false
Resque.after_fork = Proc.new { $AFTER_FORK_CALLED = true }
workerA = Resque::Worker.new(:jobs)
assert !$AFTER_FORK_CALLED
Resque::Job.create(:jobs, SomeJob, 20, '/tmp')
workerA.work(0)
assert $AFTER_FORK_CALLED
end
it "returns PID of running process" do
assert_equal @worker.to_s.split(":")[1].to_i, @worker.pid
end
it "requeue failed queue" do
queue = 'good_job'
Resque::Failure.create(:exception => Exception.new, :worker => Resque::Worker.new(queue), :queue => queue, :payload => {'class' => 'GoodJob'})
Resque::Failure.create(:exception => Exception.new, :worker => Resque::Worker.new(queue), :queue => 'some_job', :payload => {'class' => 'SomeJob'})
Resque::Failure.requeue_queue(queue)
assert Resque::Failure.all(0).has_key?('retried_at')
assert !Resque::Failure.all(1).has_key?('retried_at')
end
it "remove failed queue" do
queue = 'good_job'
queue2 = 'some_job'
Resque::Failure.create(:exception => Exception.new, :worker => Resque::Worker.new(queue), :queue => queue, :payload => {'class' => 'GoodJob'})
Resque::Failure.create(:exception => Exception.new, :worker => Resque::Worker.new(queue2), :queue => queue2, :payload => {'class' => 'SomeJob'})
Resque::Failure.create(:exception => Exception.new, :worker => Resque::Worker.new(queue), :queue => queue, :payload => {'class' => 'GoodJob'})
Resque::Failure.remove_queue(queue)
assert_equal queue2, Resque::Failure.all(0)['queue']
assert_equal 1, Resque::Failure.count
end
end