resque-scheduler 4.2.1 → 4.5.0

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of resque-scheduler might be problematic. Click here for more details.

Files changed (60) hide show
  1. checksums.yaml +5 -5
  2. data/.github/dependabot.yml +12 -0
  3. data/.github/workflows/rubocop.yml +27 -0
  4. data/.github/workflows/ruby.yml +48 -0
  5. data/AUTHORS.md +7 -0
  6. data/CHANGELOG.md +495 -0
  7. data/CODE_OF_CONDUCT.md +74 -0
  8. data/Gemfile +8 -0
  9. data/README.md +37 -17
  10. data/Rakefile +1 -5
  11. data/{bin → exe}/resque-scheduler +0 -0
  12. data/lib/resque/scheduler/cli.rb +1 -0
  13. data/lib/resque/scheduler/delaying_extensions.rb +38 -6
  14. data/lib/resque/scheduler/env.rb +8 -4
  15. data/lib/resque/scheduler/lock/resilient.rb +19 -12
  16. data/lib/resque/scheduler/locking.rb +2 -2
  17. data/lib/resque/scheduler/scheduling_extensions.rb +4 -3
  18. data/lib/resque/scheduler/server/views/delayed.erb +1 -1
  19. data/lib/resque/scheduler/server/views/search_form.erb +1 -1
  20. data/lib/resque/scheduler/server.rb +1 -1
  21. data/lib/resque/scheduler/version.rb +1 -1
  22. data/lib/resque/scheduler.rb +39 -16
  23. data/resque-scheduler.gemspec +29 -10
  24. metadata +62 -66
  25. data/.gitignore +0 -17
  26. data/.rubocop.yml +0 -18
  27. data/.rubocop_todo.yml +0 -71
  28. data/.simplecov +0 -3
  29. data/.travis.yml +0 -26
  30. data/.vagrant-provision-as-vagrant.sh +0 -15
  31. data/.vagrant-provision.sh +0 -23
  32. data/.vagrant-skel/bash_profile +0 -7
  33. data/.vagrant-skel/bashrc +0 -7
  34. data/HISTORY.md +0 -303
  35. data/Vagrantfile +0 -14
  36. data/examples/Rakefile +0 -2
  37. data/examples/config/initializers/resque-web.rb +0 -37
  38. data/examples/dynamic-scheduling/README.md +0 -28
  39. data/examples/dynamic-scheduling/app/jobs/fix_schedules_job.rb +0 -52
  40. data/examples/dynamic-scheduling/app/jobs/send_email_job.rb +0 -9
  41. data/examples/dynamic-scheduling/app/models/user.rb +0 -16
  42. data/examples/dynamic-scheduling/config/resque.yml +0 -4
  43. data/examples/dynamic-scheduling/config/static_schedule.yml +0 -7
  44. data/examples/dynamic-scheduling/lib/tasks/resque.rake +0 -48
  45. data/examples/run-resque-web +0 -3
  46. data/script/migrate_to_timestamps_set.rb +0 -16
  47. data/tasks/resque_scheduler.rake +0 -2
  48. data/test/cli_test.rb +0 -231
  49. data/test/delayed_queue_test.rb +0 -925
  50. data/test/env_test.rb +0 -47
  51. data/test/multi_process_test.rb +0 -125
  52. data/test/resque-web_test.rb +0 -364
  53. data/test/scheduler_args_test.rb +0 -222
  54. data/test/scheduler_hooks_test.rb +0 -55
  55. data/test/scheduler_locking_test.rb +0 -316
  56. data/test/scheduler_setup_test.rb +0 -141
  57. data/test/scheduler_task_test.rb +0 -72
  58. data/test/scheduler_test.rb +0 -473
  59. data/test/test_helper.rb +0 -147
  60. data/test/util_test.rb +0 -17
@@ -1,925 +0,0 @@
1
- # vim:fileencoding=utf-8
2
- require_relative 'test_helper'
3
-
4
- context 'DelayedQueue' do
5
- setup do
6
- Resque::Scheduler.quiet = true
7
- Resque.redis.flushall
8
- end
9
-
10
- test 'enqueue_at adds correct list and zset' do
11
- timestamp = Time.now + 1
12
- encoded_job = Resque.encode(
13
- class: SomeIvarJob.to_s,
14
- args: ['path'],
15
- queue: Resque.queue_from_class(SomeIvarJob)
16
- )
17
-
18
- assert_equal(0, Resque.redis.llen("delayed:#{timestamp.to_i}").to_i,
19
- 'delayed queue should be empty to start')
20
- assert_equal(0, Resque.redis.scard("timestamps:#{encoded_job}"),
21
- 'job timestamps set should be empty to start')
22
-
23
- Resque.enqueue_at(timestamp, SomeIvarJob, 'path')
24
-
25
- # Confirm the correct keys were added
26
- assert_equal(1, Resque.redis.llen("delayed:#{timestamp.to_i}").to_i,
27
- 'delayed queue should have one entry now')
28
- assert_equal(1, Resque.redis.scard("timestamps:#{encoded_job}"),
29
- 'job timestamps should have one entry now')
30
- assert_equal(1, Resque.redis.zcard(:delayed_queue_schedule),
31
- 'The delayed_queue_schedule should have 1 entry now')
32
-
33
- read_timestamp = timestamp.to_i
34
-
35
- item = Resque.next_item_for_timestamp(read_timestamp)
36
-
37
- # Confirm the item came out correctly
38
- assert_equal('SomeIvarJob', item['class'],
39
- 'Should be the same class that we queued')
40
- assert_equal(['path'], item['args'],
41
- 'Should have the same arguments that we queued')
42
-
43
- # And now confirm the keys are gone
44
- assert(!Resque.redis.exists("delayed:#{timestamp.to_i}"))
45
- assert_equal(0, Resque.redis.zcard(:delayed_queue_schedule),
46
- 'delayed queue should be empty')
47
- assert_equal(0, Resque.redis.scard("timestamps:#{encoded_job}"),
48
- 'job timestamps set should be empty')
49
- end
50
-
51
- test 'enqueue_at with queue adds correct list and zset and queue' do
52
- timestamp = Time.now + 1
53
- encoded_job = Resque.encode(
54
- class: SomeIvarJob.to_s,
55
- args: ['path'],
56
- queue: 'critical'
57
- )
58
-
59
- assert_equal(0, Resque.redis.llen("delayed:#{timestamp.to_i}").to_i,
60
- 'delayed queue should be empty to start')
61
- assert_equal(0, Resque.redis.scard("timestamps:#{encoded_job}"),
62
- 'job timestamps set should be empty to start')
63
-
64
- Resque.enqueue_at_with_queue('critical', timestamp, SomeIvarJob, 'path')
65
-
66
- # Confirm the correct keys were added
67
- assert_equal(1, Resque.redis.llen("delayed:#{timestamp.to_i}").to_i,
68
- 'delayed queue should have one entry now')
69
- assert_equal(1, Resque.redis.scard("timestamps:#{encoded_job}"),
70
- 'job timestamps should have one entry now')
71
- assert_equal(1, Resque.redis.zcard(:delayed_queue_schedule),
72
- 'The delayed_queue_schedule should have 1 entry now')
73
-
74
- read_timestamp = timestamp.to_i
75
-
76
- item = Resque.next_item_for_timestamp(read_timestamp)
77
-
78
- # Confirm the item came out correctly
79
- assert_equal('SomeIvarJob', item['class'],
80
- 'Should be the same class that we queued')
81
- assert_equal(['path'], item['args'],
82
- 'Should have the same arguments that we queued')
83
- assert_equal('critical', item['queue'],
84
- 'Should have the queue that we asked for')
85
-
86
- # And now confirm the keys are gone
87
- assert(!Resque.redis.exists("delayed:#{timestamp.to_i}"))
88
- assert_equal(0, Resque.redis.zcard(:delayed_queue_schedule),
89
- 'delayed queue should be empty')
90
- assert_equal(0, Resque.redis.scard("timestamps:#{encoded_job}"),
91
- 'job timestamps set should be empty')
92
- end
93
-
94
- test "a job in the future doesn't come out" do
95
- timestamp = Time.now + 600
96
- encoded_job = Resque.encode(
97
- class: SomeIvarJob.to_s,
98
- args: ['path'],
99
- queue: Resque.queue_from_class(SomeIvarJob)
100
- )
101
-
102
- assert_equal(0, Resque.redis.llen("delayed:#{timestamp.to_i}").to_i,
103
- 'delayed queue should be empty to start')
104
- assert_equal(0, Resque.redis.scard("timestamps:#{encoded_job}"),
105
- 'job timestamps set should be empty to start')
106
-
107
- Resque.enqueue_at(timestamp, SomeIvarJob, 'path')
108
-
109
- # Confirm the correct keys were added
110
- assert_equal(1, Resque.redis.llen("delayed:#{timestamp.to_i}").to_i,
111
- 'delayed queue should have one entry now')
112
- assert_equal(1, Resque.redis.scard("timestamps:#{encoded_job}"),
113
- 'job timestamps should have one entry now')
114
- assert_equal(1, Resque.redis.zcard(:delayed_queue_schedule),
115
- 'The delayed_queue_schedule should have 1 entry now')
116
-
117
- read_timestamp = Resque.next_delayed_timestamp
118
-
119
- assert_nil(read_timestamp, 'No timestamps should be ready for queueing')
120
- end
121
-
122
- test 'a job in the future comes out if you want it to' do
123
- timestamp = Time.now + 600 # 10 minutes from now
124
-
125
- Resque.enqueue_at(timestamp, SomeIvarJob, 'path')
126
-
127
- read_timestamp = Resque.next_delayed_timestamp(timestamp)
128
-
129
- assert_equal(timestamp.to_i, read_timestamp,
130
- 'The timestamp we pull out of redis should match the ' \
131
- 'one we put in')
132
- end
133
-
134
- test 'enqueue_at and enqueue_in are equivelent' do
135
- timestamp = Time.now + 60
136
- encoded_job = Resque.encode(
137
- class: SomeIvarJob.to_s,
138
- args: ['path'],
139
- queue: Resque.queue_from_class(SomeIvarJob)
140
- )
141
-
142
- Resque.enqueue_at(timestamp, SomeIvarJob, 'path')
143
- Resque.enqueue_in(timestamp - Time.now, SomeIvarJob, 'path')
144
-
145
- assert_equal(1, Resque.redis.zcard(:delayed_queue_schedule),
146
- 'should have one timestamp in the delayed queue')
147
- assert_equal(2, Resque.redis.llen("delayed:#{timestamp.to_i}"),
148
- 'should have 2 items in the timestamp queue')
149
- assert_equal(1, Resque.redis.scard("timestamps:#{encoded_job}"),
150
- 'job timestamps should have one entry now')
151
- end
152
-
153
- test 'empty delayed_queue_peek returns empty array' do
154
- assert_equal([], Resque.delayed_queue_peek(0, 20))
155
- end
156
-
157
- test 'delayed_queue_peek returns stuff' do
158
- t = Time.now
159
- expected_timestamps = (1..5).to_a.map do |i|
160
- (t + 60 + i).to_i
161
- end
162
-
163
- expected_timestamps.each do |timestamp|
164
- Resque.delayed_push(timestamp, class: SomeIvarJob, args: 'blah1 ')
165
- end
166
-
167
- timestamps = Resque.delayed_queue_peek(1, 2)
168
-
169
- assert_equal(expected_timestamps[1, 2], timestamps)
170
- end
171
-
172
- test 'delayed_queue_schedule_size returns correct size' do
173
- assert_equal(0, Resque.delayed_queue_schedule_size)
174
- Resque.enqueue_at(Time.now + 60, SomeIvarJob)
175
- assert_equal(1, Resque.delayed_queue_schedule_size)
176
- end
177
-
178
- test 'delayed_timestamp_size returns 0 when nothing is queue' do
179
- t = Time.now + 60
180
- assert_equal(0, Resque.delayed_timestamp_size(t))
181
- end
182
-
183
- test 'delayed_timestamp_size returns 1 when one thing is queued' do
184
- t = Time.now + 60
185
- Resque.enqueue_at(t, SomeIvarJob)
186
- assert_equal(1, Resque.delayed_timestamp_size(t))
187
- end
188
-
189
- test 'delayed_timestamp_peek returns empty array when nothings in it' do
190
- t = Time.now + 60
191
- assert_equal([], Resque.delayed_timestamp_peek(t, 0, 1),
192
- "make sure it's an empty array, not nil")
193
- end
194
-
195
- test 'delayed_timestamp_peek returns an array containing one job ' \
196
- 'when one thing is queued' do
197
- t = Time.now + 60
198
- Resque.enqueue_at(t, SomeIvarJob)
199
- assert_equal(
200
- [{ 'args' => [], 'class' => 'SomeIvarJob', 'queue' => 'ivar' }],
201
- Resque.delayed_timestamp_peek(t, 0, 1)
202
- )
203
- end
204
-
205
- test 'delayed_timestamp_peek returns an array of multiple jobs ' \
206
- 'when more than one job is queued' do
207
- t = Time.now + 60
208
- Resque.enqueue_at(t, SomeIvarJob)
209
- Resque.enqueue_at(t, SomeIvarJob)
210
- job = { 'args' => [], 'class' => 'SomeIvarJob', 'queue' => 'ivar' }
211
- assert_equal([job, job], Resque.delayed_timestamp_peek(t, 0, 2))
212
- end
213
-
214
- test 'delayed_timestamp_peek only returns an array of one job ' \
215
- 'if only asked for 1' do
216
- t = Time.now + 60
217
- Resque.enqueue_at(t, SomeIvarJob)
218
- Resque.enqueue_at(t, SomeIvarJob)
219
- job = { 'args' => [], 'class' => 'SomeIvarJob', 'queue' => 'ivar' }
220
- assert_equal([job], Resque.delayed_timestamp_peek(t, 0, 1))
221
- end
222
-
223
- test 'handle_delayed_items with no items' do
224
- Resque::Scheduler.expects(:enqueue).never
225
- Resque::Scheduler.handle_delayed_items
226
- end
227
-
228
- test 'handle_delayed_item with items' do
229
- t = Time.now - 60 # in the past
230
-
231
- # 2 SomeIvarJob jobs should be created in the "ivar" queue
232
- Resque::Job.expects(:create).twice.with(:ivar, SomeIvarJob)
233
- Resque.enqueue_at(t, SomeIvarJob)
234
- Resque.enqueue_at(t, SomeIvarJob)
235
- end
236
-
237
- test 'handle_delayed_items with items in the future' do
238
- t = Time.now + 60 # in the future
239
- Resque.enqueue_at(t, SomeIvarJob)
240
- Resque.enqueue_at(t, SomeIvarJob)
241
-
242
- # 2 SomeIvarJob jobs should be created in the "ivar" queue
243
- Resque::Job.expects(:create).twice.with('ivar', SomeIvarJob, nil)
244
- Resque::Scheduler.handle_delayed_items(t)
245
- end
246
-
247
- test 'calls klass#scheduled when enqueuing jobs if it exists' do
248
- t = Time.now - 60
249
- FakeCustomJobClassEnqueueAt.expects(:scheduled)
250
- .once.with(:test, FakeCustomJobClassEnqueueAt.to_s, foo: 'bar')
251
- Resque.enqueue_at(t, FakeCustomJobClassEnqueueAt, foo: 'bar')
252
- end
253
-
254
- test 'when Resque.inline = true, calls klass#scheduled ' \
255
- 'when enqueuing jobs if it exists' do
256
- old_val = Resque.inline
257
- begin
258
- Resque.inline = true
259
- t = Time.now - 60
260
- FakeCustomJobClassEnqueueAt.expects(:scheduled)
261
- .once.with(:test, FakeCustomJobClassEnqueueAt.to_s, foo: 'bar')
262
- Resque.enqueue_at(t, FakeCustomJobClassEnqueueAt, foo: 'bar')
263
- ensure
264
- Resque.inline = old_val
265
- end
266
- end
267
-
268
- test 'enqueue_next_item picks one job' do
269
- t = Time.now + 60
270
-
271
- Resque.enqueue_at(t, SomeIvarJob)
272
- Resque.enqueue_at(t, SomeIvarJob)
273
- Resque::Scheduler.enqueue_next_item(t)
274
- assert_equal(1, Resque.delayed_timestamp_peek(t, 0, 3).length)
275
- end
276
-
277
- test 'enqueue_delayed_items_for_timestamp creates jobs ' \
278
- 'and empties the delayed queue' do
279
- t = Time.now + 60
280
-
281
- Resque.enqueue_at(t, SomeIvarJob)
282
- Resque.enqueue_at(t, SomeIvarJob)
283
-
284
- # 2 SomeIvarJob jobs should be created in the "ivar" queue
285
- Resque::Job.expects(:create).twice.with('ivar', SomeIvarJob, nil)
286
-
287
- Resque::Scheduler.enqueue_delayed_items_for_timestamp(t)
288
-
289
- # delayed queue for timestamp should be empty
290
- assert_equal(0, Resque.delayed_timestamp_peek(t, 0, 3).length)
291
- end
292
-
293
- test 'enqueue_delayed creates jobs and empties the delayed queue' do
294
- t = Time.now + 60
295
-
296
- Resque.enqueue_at(t, SomeIvarJob, 'foo')
297
- Resque.enqueue_at(t, SomeIvarJob, 'bar')
298
- Resque.enqueue_at(t, SomeIvarJob, 'bar')
299
-
300
- # 3 SomeIvarJob jobs should be created in the "ivar" queue
301
- Resque::Job.expects(:create).never.with(:ivar, SomeIvarJob, 'foo')
302
- Resque::Job.expects(:create).twice.with(:ivar, SomeIvarJob, 'bar')
303
-
304
- # 2 SomeIvarJob jobs should be enqueued
305
- assert_equal(2, Resque.enqueue_delayed(SomeIvarJob, 'bar'))
306
-
307
- # delayed queue for timestamp should have one remaining
308
- assert_equal(1, Resque.delayed_timestamp_peek(t, 0, 3).length)
309
- end
310
-
311
- test 'handle_delayed_items works with out specifying queue ' \
312
- '(upgrade case)' do
313
- t = Time.now - 60
314
- Resque.delayed_push(t, class: 'SomeIvarJob')
315
-
316
- # Since we didn't specify :queue when calling delayed_push, it will be
317
- # forced to load the class to figure out the queue. This is the upgrade
318
- # case from 1.0.4 to 1.0.5.
319
- Resque::Job.expects(:create).once.with(:ivar, SomeIvarJob, nil)
320
-
321
- Resque::Scheduler.handle_delayed_items
322
- end
323
-
324
- test 'reset_delayed_queue clears the queue' do
325
- t = Time.now + 120
326
- 4.times { Resque.enqueue_at(t, SomeIvarJob) }
327
- 4.times { Resque.enqueue_at(Time.now + rand(100), SomeIvarJob) }
328
-
329
- Resque.reset_delayed_queue
330
- assert_equal(0, Resque.delayed_queue_schedule_size)
331
- assert_equal(0, Resque.redis.keys('timestamps:*').size)
332
- end
333
-
334
- test 'remove_delayed removes job and returns the count' do
335
- t = Time.now + 120
336
- encoded_job = Resque.encode(
337
- class: SomeIvarJob.to_s,
338
- args: [],
339
- queue: Resque.queue_from_class(SomeIvarJob)
340
- )
341
- Resque.enqueue_at(t, SomeIvarJob)
342
-
343
- assert_equal(1, Resque.remove_delayed(SomeIvarJob))
344
- assert_equal(0, Resque.redis.scard("timestamps:#{encoded_job}"))
345
- end
346
-
347
- test "when Resque.inline = true, remove_delayed doesn't remove the job" \
348
- 'and returns 0' do
349
- begin
350
- Resque.inline = true
351
-
352
- timestamp = Time.now + 120
353
- Resque.enqueue_at(timestamp, SomeIvarJob, 'foo', 'bar')
354
-
355
- assert_equal(0, Resque.remove_delayed(SomeIvarJob))
356
- ensure
357
- Resque.inline = false
358
- end
359
- end
360
-
361
- test 'scheduled_at returns an array containing job schedule time' do
362
- t = Time.now + 120
363
- Resque.enqueue_at(t, SomeIvarJob)
364
-
365
- assert_equal([t.to_i], Resque.scheduled_at(SomeIvarJob))
366
- end
367
-
368
- test "remove_delayed doesn't remove things it shouldn't" do
369
- t = Time.now + 120
370
- Resque.enqueue_at(t, SomeIvarJob, 'foo')
371
- Resque.enqueue_at(t, SomeIvarJob, 'bar')
372
- Resque.enqueue_at(t, SomeIvarJob, 'bar')
373
- Resque.enqueue_at(t, SomeIvarJob, 'baz')
374
-
375
- assert_equal(0, Resque.remove_delayed(SomeIvarJob))
376
- end
377
-
378
- test 'remove_delayed respected param' do
379
- t = Time.now + 120
380
- Resque.enqueue_at(t, SomeIvarJob, 'foo')
381
- Resque.enqueue_at(t, SomeIvarJob, 'bar')
382
- Resque.enqueue_at(t, SomeIvarJob, 'bar')
383
- Resque.enqueue_at(t, SomeIvarJob, 'baz')
384
-
385
- assert_equal(2, Resque.remove_delayed(SomeIvarJob, 'bar'))
386
- assert_equal(1, Resque.delayed_queue_schedule_size)
387
- end
388
-
389
- test 'remove_delayed removes items in different timestamps' do
390
- t = Time.now + 120
391
- Resque.enqueue_at(t, SomeIvarJob, 'foo')
392
- Resque.enqueue_at(t + 1, SomeIvarJob, 'bar')
393
- Resque.enqueue_at(t + 2, SomeIvarJob, 'bar')
394
- Resque.enqueue_at(t + 3, SomeIvarJob, 'baz')
395
-
396
- assert_equal(2, Resque.remove_delayed(SomeIvarJob, 'bar'))
397
- assert_equal(2, Resque.count_all_scheduled_jobs)
398
- end
399
-
400
- test 'remove_delayed_selection removes multiple items matching ' \
401
- 'arguments at same timestamp' do
402
- t = Time.now + 120
403
- Resque.enqueue_at(t, SomeIvarJob, 'bar', 'llama')
404
- Resque.enqueue_at(t, SomeIvarJob, 'foo')
405
- Resque.enqueue_at(t, SomeIvarJob, 'bar', 'monkey')
406
- Resque.enqueue_at(t, SomeIvarJob, 'bar', 'platypus')
407
- Resque.enqueue_at(t, SomeIvarJob, 'baz')
408
- Resque.enqueue_at(t, SomeIvarJob, 'bar', 'llama')
409
- Resque.enqueue_at(t, SomeIvarJob, 'bar', 'llama')
410
-
411
- assert_equal(5, Resque.remove_delayed_selection { |a| a.first == 'bar' })
412
- assert_equal(2, Resque.count_all_scheduled_jobs)
413
- end
414
-
415
- test 'remove_delayed_selection removes single item matching arguments' do
416
- t = Time.now + 120
417
- Resque.enqueue_at(t, SomeIvarJob, 'foo')
418
- Resque.enqueue_at(t + 1, SomeIvarJob, 'bar')
419
- Resque.enqueue_at(t + 2, SomeIvarJob, 'bar')
420
- Resque.enqueue_at(t + 3, SomeIvarJob, 'baz')
421
-
422
- assert_equal(1, Resque.remove_delayed_selection { |a| a.first == 'foo' })
423
- assert_equal(3, Resque.count_all_scheduled_jobs)
424
- end
425
-
426
- test 'remove_delayed_selection removes multiple items matching arguments' do
427
- t = Time.now + 120
428
- Resque.enqueue_at(t, SomeIvarJob, 'foo')
429
- Resque.enqueue_at(t + 1, SomeIvarJob, 'bar')
430
- Resque.enqueue_at(t + 2, SomeIvarJob, 'bar')
431
- Resque.enqueue_at(t + 3, SomeIvarJob, 'baz')
432
-
433
- assert_equal(2, Resque.remove_delayed_selection { |a| a.first == 'bar' })
434
- assert_equal(2, Resque.count_all_scheduled_jobs)
435
- end
436
-
437
- test 'remove_delayed_selection removes multiple items matching ' \
438
- 'arguments as hash' do
439
- t = Time.now + 120
440
- Resque.enqueue_at(t, SomeIvarJob, foo: 'foo')
441
- Resque.enqueue_at(t + 1, SomeIvarJob, foo: 'bar')
442
- Resque.enqueue_at(t + 2, SomeIvarJob, foo: 'bar')
443
- Resque.enqueue_at(t + 3, SomeIvarJob, foo: 'baz')
444
-
445
- assert_equal(
446
- 2, Resque.remove_delayed_selection { |a| a.first['foo'] == 'bar' }
447
- )
448
- assert_equal(2, Resque.count_all_scheduled_jobs)
449
- end
450
-
451
- test 'remove_delayed_selection ignores jobs with no arguments' do
452
- t = Time.now + 120
453
- Resque.enqueue_at(t, SomeIvarJob)
454
- Resque.enqueue_at(t + 1, SomeIvarJob)
455
- Resque.enqueue_at(t + 2, SomeIvarJob)
456
- Resque.enqueue_at(t + 3, SomeIvarJob)
457
-
458
- assert_equal(0, Resque.remove_delayed_selection { |a| a.first == 'bar' })
459
- assert_equal(4, Resque.count_all_scheduled_jobs)
460
- end
461
-
462
- test "remove_delayed_selection doesn't remove items it shouldn't" do
463
- t = Time.now + 120
464
- Resque.enqueue_at(t, SomeIvarJob, 'foo')
465
- Resque.enqueue_at(t + 1, SomeIvarJob, 'bar')
466
- Resque.enqueue_at(t + 2, SomeIvarJob, 'bar')
467
- Resque.enqueue_at(t + 3, SomeIvarJob, 'baz')
468
-
469
- assert_equal(0, Resque.remove_delayed_selection { |a| a.first == 'qux' })
470
- assert_equal(4, Resque.count_all_scheduled_jobs)
471
- end
472
-
473
- test 'remove_delayed_selection ignores last_enqueued_at redis key' do
474
- t = Time.now + 120
475
- Resque.enqueue_at(t, SomeIvarJob)
476
- Resque.last_enqueued_at(SomeIvarJob, t)
477
-
478
- assert_equal(0, Resque.remove_delayed_selection { |a| a.first == 'bar' })
479
- assert_equal(t.to_s, Resque.get_last_enqueued_at(SomeIvarJob))
480
- end
481
-
482
- test 'remove_delayed_selection removes item by class' do
483
- t = Time.now + 120
484
- Resque.enqueue_at(t, SomeIvarJob, 'foo')
485
- Resque.enqueue_at(t, SomeQuickJob, 'foo')
486
-
487
- assert_equal(1, Resque.remove_delayed_selection(SomeIvarJob) do |a|
488
- a.first == 'foo'
489
- end)
490
- assert_equal(1, Resque.count_all_scheduled_jobs)
491
- end
492
-
493
- test 'remove_delayed_selection removes item by class name as a string' do
494
- t = Time.now + 120
495
- Resque.enqueue_at(t, SomeIvarJob, 'foo')
496
- Resque.enqueue_at(t, SomeQuickJob, 'foo')
497
-
498
- assert_equal(1, Resque.remove_delayed_selection('SomeIvarJob') do |a|
499
- a.first == 'foo'
500
- end)
501
- assert_equal(1, Resque.count_all_scheduled_jobs)
502
- end
503
-
504
- test 'remove_delayed_selection removes item by class name as a symbol' do
505
- t = Time.now + 120
506
- Resque.enqueue_at(t, SomeIvarJob, 'foo')
507
- Resque.enqueue_at(t, SomeQuickJob, 'foo')
508
-
509
- assert_equal(1, Resque.remove_delayed_selection(:SomeIvarJob) do |a|
510
- a.first == 'foo'
511
- end)
512
- assert_equal(1, Resque.count_all_scheduled_jobs)
513
- end
514
-
515
- test 'remove_delayed_selection removes items only from matching job class' do
516
- t = Time.now + 120
517
- Resque.enqueue_at(t, SomeIvarJob, 'foo')
518
- Resque.enqueue_at(t, SomeQuickJob, 'foo')
519
- Resque.enqueue_at(t + 1, SomeIvarJob, 'bar')
520
- Resque.enqueue_at(t + 1, SomeQuickJob, 'bar')
521
- Resque.enqueue_at(t + 1, SomeIvarJob, 'foo')
522
- Resque.enqueue_at(t + 2, SomeQuickJob, 'foo')
523
-
524
- assert_equal(2, Resque.remove_delayed_selection(SomeIvarJob) do |a|
525
- a.first == 'foo'
526
- end)
527
- assert_equal(4, Resque.count_all_scheduled_jobs)
528
- end
529
-
530
- test 'remove_delayed_selection removes items from matching job class ' \
531
- 'without params' do
532
- t = Time.now + 120
533
- Resque.enqueue_at(t, SomeIvarJob)
534
- Resque.enqueue_at(t + 1, SomeQuickJob)
535
- Resque.enqueue_at(t + 2, SomeIvarJob)
536
- Resque.enqueue_at(t + 3, SomeQuickJob)
537
-
538
- assert_equal(2, Resque.remove_delayed_selection(SomeQuickJob) { true })
539
- assert_equal(2, Resque.count_all_scheduled_jobs)
540
- end
541
-
542
- test 'enqueue_delayed_selection enqueues multiple items matching ' \
543
- 'arguments at same timestamp' do
544
- t = Time.now + 120
545
- Resque.enqueue_at(t, SomeIvarJob, 'bar', 'llama')
546
- Resque.enqueue_at(t, SomeIvarJob, 'foo')
547
- Resque.enqueue_at(t, SomeIvarJob, 'bar', 'monkey')
548
- Resque.enqueue_at(t, SomeIvarJob, 'bar', 'platypus')
549
- Resque.enqueue_at(t, SomeIvarJob, 'baz')
550
- Resque.enqueue_at(t, SomeIvarJob, 'bar', 'llama')
551
- Resque.enqueue_at(t, SomeIvarJob, 'bar', 'llama')
552
-
553
- assert_equal(5, Resque.enqueue_delayed_selection { |a| a.first == 'bar' })
554
- assert_equal(2, Resque.count_all_scheduled_jobs)
555
- end
556
-
557
- test 'enqueue_delayed_selection enqueues single item matching arguments' do
558
- t = Time.now + 120
559
- Resque.enqueue_at(t, SomeIvarJob, 'foo')
560
- Resque.enqueue_at(t + 1, SomeIvarJob, 'bar')
561
- Resque.enqueue_at(t + 2, SomeIvarJob, 'bar')
562
- Resque.enqueue_at(t + 3, SomeIvarJob, 'baz')
563
-
564
- assert_equal(1, Resque.enqueue_delayed_selection { |a| a.first == 'foo' })
565
- assert_equal(3, Resque.count_all_scheduled_jobs)
566
- end
567
-
568
- test 'enqueue_delayed_selection enqueues multiple items matching arguments' do
569
- t = Time.now + 120
570
- Resque.enqueue_at(t, SomeIvarJob, 'foo')
571
- Resque.enqueue_at(t + 1, SomeIvarJob, 'bar')
572
- Resque.enqueue_at(t + 2, SomeIvarJob, 'bar')
573
- Resque.enqueue_at(t + 3, SomeIvarJob, 'baz')
574
-
575
- assert_equal(2, Resque.enqueue_delayed_selection { |a| a.first == 'bar' })
576
- assert_equal(2, Resque.count_all_scheduled_jobs)
577
- end
578
-
579
- test 'enqueue_delayed_selection enqueues multiple items matching ' \
580
- 'arguments as hash' do
581
- t = Time.now + 120
582
- Resque.enqueue_at(t, SomeIvarJob, foo: 'foo')
583
- Resque.enqueue_at(t + 1, SomeIvarJob, foo: 'bar')
584
- Resque.enqueue_at(t + 2, SomeIvarJob, foo: 'bar')
585
- Resque.enqueue_at(t + 3, SomeIvarJob, foo: 'baz')
586
-
587
- assert_equal(
588
- 2, Resque.enqueue_delayed_selection { |a| a.first['foo'] == 'bar' }
589
- )
590
- assert_equal(2, Resque.count_all_scheduled_jobs)
591
- end
592
-
593
- test 'enqueue_delayed_selection ignores jobs with no arguments' do
594
- t = Time.now + 120
595
- Resque.enqueue_at(t, SomeIvarJob)
596
- Resque.enqueue_at(t + 1, SomeIvarJob)
597
- Resque.enqueue_at(t + 2, SomeIvarJob)
598
- Resque.enqueue_at(t + 3, SomeIvarJob)
599
-
600
- assert_equal(0, Resque.enqueue_delayed_selection { |a| a.first == 'bar' })
601
- assert_equal(4, Resque.count_all_scheduled_jobs)
602
- end
603
-
604
- test "enqueue_delayed_selection doesn't enqueue items it shouldn't" do
605
- t = Time.now + 120
606
- Resque.enqueue_at(t, SomeIvarJob, 'foo')
607
- Resque.enqueue_at(t + 1, SomeIvarJob, 'bar')
608
- Resque.enqueue_at(t + 2, SomeIvarJob, 'bar')
609
- Resque.enqueue_at(t + 3, SomeIvarJob, 'baz')
610
-
611
- assert_equal(0, Resque.enqueue_delayed_selection { |a| a.first == 'qux' })
612
- assert_equal(4, Resque.count_all_scheduled_jobs)
613
- end
614
-
615
- test 'enqueue_delayed_selection ignores last_enqueued_at redis key' do
616
- t = Time.now + 120
617
- Resque.enqueue_at(t, SomeIvarJob)
618
- Resque.last_enqueued_at(SomeIvarJob, t)
619
-
620
- assert_equal(0, Resque.enqueue_delayed_selection { |a| a.first == 'bar' })
621
- assert_equal(t.to_s, Resque.get_last_enqueued_at(SomeIvarJob))
622
- end
623
-
624
- test 'enqueue_delayed_selection enqueues item by class' do
625
- t = Time.now + 120
626
- Resque.enqueue_at(t, SomeIvarJob, 'foo')
627
- Resque.enqueue_at(t, SomeQuickJob, 'foo')
628
-
629
- assert_equal(1, Resque.enqueue_delayed_selection(SomeIvarJob) do |a|
630
- a.first == 'foo'
631
- end)
632
- assert_equal(1, Resque.count_all_scheduled_jobs)
633
- end
634
-
635
- test 'enqueue_delayed_selection enqueues item by class name as a string' do
636
- t = Time.now + 120
637
- Resque.enqueue_at(t, SomeIvarJob, 'foo')
638
- Resque.enqueue_at(t, SomeQuickJob, 'foo')
639
-
640
- assert_equal(1, Resque.enqueue_delayed_selection('SomeIvarJob') do |a|
641
- a.first == 'foo'
642
- end)
643
- assert_equal(1, Resque.count_all_scheduled_jobs)
644
- end
645
-
646
- test 'enqueue_delayed_selection enqueues item by class name as a symbol' do
647
- t = Time.now + 120
648
- Resque.enqueue_at(t, SomeIvarJob, 'foo')
649
- Resque.enqueue_at(t, SomeQuickJob, 'foo')
650
-
651
- assert_equal(1, Resque.enqueue_delayed_selection(:SomeIvarJob) do |a|
652
- a.first == 'foo'
653
- end)
654
- assert_equal(1, Resque.count_all_scheduled_jobs)
655
- end
656
-
657
- test 'enqueue_delayed_selection enqueues items only from' \
658
- 'matching job class' do
659
- t = Time.now + 120
660
- Resque.enqueue_at(t, SomeIvarJob, 'foo')
661
- Resque.enqueue_at(t, SomeQuickJob, 'foo')
662
- Resque.enqueue_at(t + 1, SomeIvarJob, 'bar')
663
- Resque.enqueue_at(t + 1, SomeQuickJob, 'bar')
664
- Resque.enqueue_at(t + 1, SomeIvarJob, 'foo')
665
- Resque.enqueue_at(t + 2, SomeQuickJob, 'foo')
666
-
667
- assert_equal(2, Resque.enqueue_delayed_selection(SomeIvarJob) do |a|
668
- a.first == 'foo'
669
- end)
670
- assert_equal(4, Resque.count_all_scheduled_jobs)
671
- end
672
-
673
- test 'enqueue_delayed_selection enqueues items from matching job class ' \
674
- 'without params' do
675
- t = Time.now + 120
676
- Resque.enqueue_at(t, SomeIvarJob)
677
- Resque.enqueue_at(t + 1, SomeQuickJob)
678
- Resque.enqueue_at(t + 2, SomeIvarJob)
679
- Resque.enqueue_at(t + 3, SomeQuickJob)
680
-
681
- assert_equal(2, Resque.enqueue_delayed_selection(SomeQuickJob) { true })
682
- assert_equal(2, Resque.count_all_scheduled_jobs)
683
- end
684
-
685
- test 'find_delayed_selection finds multiple items matching ' \
686
- 'arguments at same timestamp' do
687
- t = Time.now + 120
688
- Resque.enqueue_at(t, SomeIvarJob, 'bar', 'llama')
689
- Resque.enqueue_at(t, SomeIvarJob, 'foo')
690
- Resque.enqueue_at(t, SomeIvarJob, 'bar', 'monkey')
691
- Resque.enqueue_at(t, SomeIvarJob, 'bar', 'platypus')
692
- Resque.enqueue_at(t, SomeIvarJob, 'baz')
693
- Resque.enqueue_at(t, SomeIvarJob, 'bar', 'llama')
694
- Resque.enqueue_at(t, SomeIvarJob, 'bar', 'llama')
695
-
696
- assert_equal(5, (Resque.find_delayed_selection do |a|
697
- a.first == 'bar'
698
- end).length)
699
- end
700
-
701
- test 'find_delayed_selection finds single item matching arguments' do
702
- t = Time.now + 120
703
- Resque.enqueue_at(t, SomeIvarJob, 'foo')
704
- Resque.enqueue_at(t + 1, SomeIvarJob, 'bar')
705
- Resque.enqueue_at(t + 2, SomeIvarJob, 'bar')
706
- Resque.enqueue_at(t + 3, SomeIvarJob, 'baz')
707
-
708
- assert_equal(1, (Resque.find_delayed_selection do |a|
709
- a.first == 'foo'
710
- end).length)
711
- end
712
-
713
- test 'find_delayed_selection finds multiple items matching arguments' do
714
- t = Time.now + 120
715
- Resque.enqueue_at(t, SomeIvarJob, 'foo')
716
- Resque.enqueue_at(t + 1, SomeIvarJob, 'bar')
717
- Resque.enqueue_at(t + 2, SomeIvarJob, 'bar')
718
- Resque.enqueue_at(t + 3, SomeIvarJob, 'baz')
719
-
720
- assert_equal(2, (Resque.find_delayed_selection do |a|
721
- a.first == 'bar'
722
- end).length)
723
- end
724
-
725
- test 'find_delayed_selection finds multiple items matching ' \
726
- 'arguments as hash' do
727
- t = Time.now + 120
728
- Resque.enqueue_at(t, SomeIvarJob, foo: 'foo')
729
- Resque.enqueue_at(t + 1, SomeIvarJob, foo: 'bar')
730
- Resque.enqueue_at(t + 2, SomeIvarJob, foo: 'bar')
731
- Resque.enqueue_at(t + 3, SomeIvarJob, foo: 'baz')
732
-
733
- len = Resque.find_delayed_selection { |a| a.first['foo'] == 'bar' }.length
734
- assert_equal(2, len)
735
- end
736
-
737
- test 'find_delayed_selection ignores jobs with no arguments' do
738
- t = Time.now + 120
739
- Resque.enqueue_at(t, SomeIvarJob)
740
- Resque.enqueue_at(t + 1, SomeIvarJob)
741
- Resque.enqueue_at(t + 2, SomeIvarJob)
742
- Resque.enqueue_at(t + 3, SomeIvarJob)
743
-
744
- assert_equal(0, (Resque.find_delayed_selection do |a|
745
- a.first == 'bar'
746
- end).length)
747
- end
748
-
749
- test "find_delayed_selection doesn't find items it shouldn't" do
750
- t = Time.now + 120
751
- Resque.enqueue_at(t, SomeIvarJob, 'foo')
752
- Resque.enqueue_at(t + 1, SomeIvarJob, 'bar')
753
- Resque.enqueue_at(t + 2, SomeIvarJob, 'bar')
754
- Resque.enqueue_at(t + 3, SomeIvarJob, 'baz')
755
-
756
- assert_equal(0, (Resque.find_delayed_selection do |a|
757
- a.first == 'qux'
758
- end).length)
759
- end
760
-
761
- test 'find_delayed_selection finds item by class' do
762
- t = Time.now + 120
763
- Resque.enqueue_at(t, SomeIvarJob, 'foo')
764
- Resque.enqueue_at(t, SomeQuickJob, 'foo')
765
-
766
- assert_equal(1, (Resque.find_delayed_selection(SomeIvarJob) do |a|
767
- a.first == 'foo'
768
- end).length)
769
- end
770
-
771
- test 'find_delayed_selection finds item by class name as a string' do
772
- t = Time.now + 120
773
- Resque.enqueue_at(t, SomeIvarJob, 'foo')
774
- Resque.enqueue_at(t, SomeQuickJob, 'foo')
775
-
776
- assert_equal(1, (Resque.find_delayed_selection('SomeIvarJob') do |a|
777
- a.first == 'foo'
778
- end).length)
779
- end
780
-
781
- test 'find_delayed_selection finds item by class name as a symbol' do
782
- t = Time.now + 120
783
- Resque.enqueue_at(t, SomeIvarJob, 'foo')
784
- Resque.enqueue_at(t, SomeQuickJob, 'foo')
785
-
786
- assert_equal(1, (Resque.find_delayed_selection(:SomeIvarJob) do |a|
787
- a.first == 'foo'
788
- end).length)
789
- end
790
-
791
- test 'find_delayed_selection finds items only from' \
792
- 'matching job class' do
793
- t = Time.now + 120
794
- Resque.enqueue_at(t, SomeIvarJob, 'foo')
795
- Resque.enqueue_at(t, SomeQuickJob, 'foo')
796
- Resque.enqueue_at(t + 1, SomeIvarJob, 'bar')
797
- Resque.enqueue_at(t + 1, SomeQuickJob, 'bar')
798
- Resque.enqueue_at(t + 1, SomeIvarJob, 'foo')
799
- Resque.enqueue_at(t + 2, SomeQuickJob, 'foo')
800
-
801
- assert_equal(2, (Resque.find_delayed_selection(SomeIvarJob) do |a|
802
- a.first == 'foo'
803
- end).length)
804
- end
805
-
806
- test 'find_delayed_selection finds items from matching job class ' \
807
- 'without params' do
808
- t = Time.now + 120
809
- Resque.enqueue_at(t, SomeIvarJob)
810
- Resque.enqueue_at(t + 1, SomeQuickJob)
811
- Resque.enqueue_at(t + 2, SomeIvarJob)
812
- Resque.enqueue_at(t + 3, SomeQuickJob)
813
-
814
- assert_equal(
815
- 2, (Resque.find_delayed_selection(SomeQuickJob) { true }).length
816
- )
817
- end
818
-
819
- test 'remove_delayed_job_from_timestamp removes instances of jobs ' \
820
- 'at a given timestamp' do
821
- t = Time.now + 120
822
- Resque.enqueue_at(t, SomeIvarJob, 'foo')
823
- assert_equal(
824
- 1, Resque.remove_delayed_job_from_timestamp(t, SomeIvarJob, 'foo')
825
- )
826
- assert_equal 0, Resque.delayed_timestamp_size(t)
827
- end
828
-
829
- test "remove_delayed_job_from_timestamp doesn't remove items from " \
830
- 'other timestamps' do
831
- t1 = Time.now + 120
832
- t2 = t1 + 1
833
- Resque.enqueue_at(t1, SomeIvarJob, 'foo')
834
- Resque.enqueue_at(t2, SomeIvarJob, 'foo')
835
- assert_equal(
836
- 1, Resque.remove_delayed_job_from_timestamp(t2, SomeIvarJob, 'foo')
837
- )
838
- assert_equal 1, Resque.delayed_timestamp_size(t1)
839
- assert_equal 0, Resque.delayed_timestamp_size(t2)
840
- end
841
-
842
- test 'when Resque.inline = true, remove_delayed_job_from_timestamp' \
843
- "doesn't remove any jobs and returns 0" do
844
- begin
845
- Resque.inline = true
846
-
847
- timestamp = Time.now + 120
848
- Resque.enqueue_at(timestamp, SomeIvarJob, 'foo', 'bar')
849
-
850
- assert_equal(0, Resque.delayed_timestamp_size(timestamp))
851
- ensure
852
- Resque.inline = false
853
- end
854
- end
855
-
856
- test 'remove_delayed_job_from_timestamp removes nothing if there ' \
857
- 'are no matches' do
858
- t = Time.now + 120
859
- assert_equal(
860
- 0, Resque.remove_delayed_job_from_timestamp(t, SomeIvarJob, 'foo')
861
- )
862
- end
863
-
864
- test 'remove_delayed_job_from_timestamp only removes items that ' \
865
- 'match args' do
866
- t = Time.now + 120
867
- Resque.enqueue_at(t, SomeIvarJob, 'foo')
868
- Resque.enqueue_at(t, SomeIvarJob, 'bar')
869
- assert_equal(
870
- 1, Resque.remove_delayed_job_from_timestamp(t, SomeIvarJob, 'foo')
871
- )
872
- assert_equal 1, Resque.delayed_timestamp_size(t)
873
- end
874
-
875
- test 'remove_delayed_job_from_timestamp returns the number of ' \
876
- 'items removed' do
877
- t = Time.now + 120
878
- Resque.enqueue_at(t, SomeIvarJob, 'foo')
879
- assert_equal(
880
- 1, Resque.remove_delayed_job_from_timestamp(t, SomeIvarJob, 'foo')
881
- )
882
- end
883
-
884
- test 'remove_delayed_job_from_timestamp should cleanup the delayed ' \
885
- 'timestamp list if not jobs are left' do
886
- t = Time.now + 120
887
- Resque.enqueue_at(t, SomeIvarJob, 'foo')
888
- assert_equal(
889
- 1, Resque.remove_delayed_job_from_timestamp(t, SomeIvarJob, 'foo')
890
- )
891
- assert !Resque.redis.exists("delayed:#{t.to_i}")
892
- assert Resque.delayed_queue_peek(0, 100).empty?
893
- end
894
-
895
- test 'invalid job class' do
896
- assert_raises Resque::NoQueueError do
897
- Resque.enqueue_in(10, String)
898
- end
899
- end
900
-
901
- test 'inlining jobs with Resque.inline config' do
902
- begin
903
- Resque.inline = true
904
- Resque::Job.expects(:create).once.with(:ivar, SomeIvarJob, 'foo', 'bar')
905
-
906
- timestamp = Time.now + 120
907
- Resque.enqueue_at(timestamp, SomeIvarJob, 'foo', 'bar')
908
-
909
- assert_equal 0, Resque.count_all_scheduled_jobs
910
- assert !Resque.redis.exists("delayed:#{timestamp.to_i}")
911
- ensure
912
- Resque.inline = false
913
- end
914
- end
915
-
916
- test 'delayed?' do
917
- Resque.enqueue_at Time.now + 1, SomeIvarJob
918
- Resque.enqueue_at Time.now + 1, SomeIvarJob, id: 1
919
-
920
- assert Resque.delayed?(SomeIvarJob, id: 1)
921
- assert !Resque.delayed?(SomeIvarJob, id: 2)
922
- assert Resque.delayed?(SomeIvarJob)
923
- assert !Resque.delayed?(SomeJob)
924
- end
925
- end