sidekiq-cron 0.6.3 → 1.4.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 +5 -5
- data/Changes.md +102 -50
- data/Gemfile +1 -30
- data/README.md +162 -103
- data/Rakefile +3 -42
- data/lib/sidekiq/cron/job.rb +117 -41
- data/lib/sidekiq/cron/launcher.rb +34 -40
- data/lib/sidekiq/cron/locales/de.yml +2 -2
- data/lib/sidekiq/cron/locales/en.yml +6 -2
- data/lib/sidekiq/cron/locales/ja.yml +18 -0
- data/lib/sidekiq/cron/locales/ru.yml +2 -2
- data/lib/sidekiq/cron/locales/zh-CN.yml +19 -0
- data/lib/sidekiq/cron/poller.rb +7 -5
- data/lib/sidekiq/cron/version.rb +7 -0
- data/lib/sidekiq/cron/views/cron.erb +38 -28
- data/lib/sidekiq/cron/views/cron_show.erb +88 -0
- data/lib/sidekiq/cron/web.rb +1 -7
- data/lib/sidekiq/cron/web_extension.rb +14 -7
- data/sidekiq-cron.gemspec +19 -103
- data/test/integration/performance_test.rb +7 -10
- data/test/test_helper.rb +27 -29
- data/test/unit/job_test.rb +171 -18
- data/test/unit/poller_test.rb +22 -25
- data/test/unit/web_extension_test.rb +57 -38
- metadata +34 -179
- data/.document +0 -5
- data/.travis.yml +0 -19
- data/Dockerfile +0 -32
- data/VERSION +0 -1
- data/config.ru +0 -14
- data/docker-compose.yml +0 -21
- data/examples/web-cron-ui.png +0 -0
- data/lib/sidekiq/cron/views/cron.slim +0 -69
data/test/unit/job_test.rb
CHANGED
@@ -1,4 +1,3 @@
|
|
1
|
-
# -*- encoding : utf-8 -*-
|
2
1
|
require './test/test_helper'
|
3
2
|
|
4
3
|
describe "Cron Job" do
|
@@ -131,6 +130,28 @@ describe "Cron Job" do
|
|
131
130
|
end
|
132
131
|
end
|
133
132
|
|
133
|
+
describe 'parse_enqueue_time' do
|
134
|
+
before do
|
135
|
+
@args = {
|
136
|
+
name: "Test",
|
137
|
+
cron: "* * * * *"
|
138
|
+
}
|
139
|
+
@job = Sidekiq::Cron::Job.new(@args)
|
140
|
+
end
|
141
|
+
|
142
|
+
it 'should correctly parse new format' do
|
143
|
+
assert_equal @job.send(:parse_enqueue_time, '2017-01-02 15:23:43 UTC'), Time.new(2017, 1, 2, 15, 23, 43, '+00:00')
|
144
|
+
end
|
145
|
+
|
146
|
+
it 'should correctly parse new format with different timezone' do
|
147
|
+
assert_equal @job.send(:parse_enqueue_time, '2017-01-02 15:23:43 +01:00'), Time.new(2017, 1, 2, 15, 23, 43, '+01:00')
|
148
|
+
end
|
149
|
+
|
150
|
+
it 'should correctly parse old format' do
|
151
|
+
assert_equal @job.send(:parse_enqueue_time, '2017-01-02 15:23:43'), Time.new(2017, 1, 2, 15, 23, 43, '+00:00')
|
152
|
+
end
|
153
|
+
end
|
154
|
+
|
134
155
|
describe 'formatted time' do
|
135
156
|
before do
|
136
157
|
@args = {
|
@@ -201,6 +222,34 @@ describe "Cron Job" do
|
|
201
222
|
"class"=>"CronTestClassWithQueue",
|
202
223
|
"args"=>[]}
|
203
224
|
end
|
225
|
+
|
226
|
+
it "be initialized with 'class' and date_as_argument" do
|
227
|
+
job = Sidekiq::Cron::Job.new('class' => 'CronTestClassWithQueue', "date_as_argument" => true)
|
228
|
+
|
229
|
+
job_message = job.message
|
230
|
+
job_args = job_message.delete("args")
|
231
|
+
assert_equal job_message, {"retry"=>false,
|
232
|
+
"queue"=>:super,
|
233
|
+
"backtrace"=>true,
|
234
|
+
"class"=>"CronTestClassWithQueue"}
|
235
|
+
assert job_args[-1].is_a?(Float)
|
236
|
+
assert job_args[-1].between?(Time.now.to_f - 1, Time.now.to_f)
|
237
|
+
end
|
238
|
+
|
239
|
+
it "be initialized with 'class', 2 arguments and date_as_argument" do
|
240
|
+
job = Sidekiq::Cron::Job.new('class' => 'CronTestClassWithQueue', "date_as_argument" => true, "args"=> ["arg1", :arg2])
|
241
|
+
|
242
|
+
job_message = job.message
|
243
|
+
job_args = job_message.delete("args")
|
244
|
+
assert_equal job_message, {"retry"=>false,
|
245
|
+
"queue"=>:super,
|
246
|
+
"backtrace"=>true,
|
247
|
+
"class"=>"CronTestClassWithQueue"}
|
248
|
+
assert job_args[-1].is_a?(Float)
|
249
|
+
assert job_args[-1].between?(Time.now.to_f - 1, Time.now.to_f)
|
250
|
+
assert_equal job_args[0..-2], ["arg1", :arg2]
|
251
|
+
end
|
252
|
+
|
204
253
|
end
|
205
254
|
|
206
255
|
describe "cron test" do
|
@@ -210,24 +259,24 @@ describe "Cron Job" do
|
|
210
259
|
|
211
260
|
it "return previous minute" do
|
212
261
|
@job.cron = "* * * * *"
|
213
|
-
time = Time.
|
262
|
+
time = Time.new(2018, 8, 10, 13, 24, 56).utc
|
214
263
|
assert_equal @job.last_time(time).strftime("%Y-%m-%d-%H-%M-%S"), time.strftime("%Y-%m-%d-%H-%M-00")
|
215
264
|
end
|
216
265
|
|
217
266
|
it "return previous hour" do
|
218
267
|
@job.cron = "1 * * * *"
|
219
|
-
time = Time.
|
268
|
+
time = Time.new(2018, 8, 10, 13, 24, 56).utc
|
220
269
|
assert_equal @job.last_time(time).strftime("%Y-%m-%d-%H-%M-%S"), time.strftime("%Y-%m-%d-%H-01-00")
|
221
270
|
end
|
222
271
|
|
223
272
|
it "return previous day" do
|
224
273
|
@job.cron = "1 2 * * * Etc/GMT"
|
225
|
-
time = Time.
|
274
|
+
time = Time.new(2018, 8, 10, 13, 24, 56).utc
|
226
275
|
|
227
276
|
if time.hour >= 2
|
228
277
|
assert_equal @job.last_time(time).strftime("%Y-%m-%d-%H-%M-%S"), time.strftime("%Y-%m-%d-02-01-00")
|
229
278
|
else
|
230
|
-
yesterday =
|
279
|
+
yesterday = time - 1.day
|
231
280
|
assert_equal @job.last_time(time).strftime("%Y-%m-%d-%H-%M-%S"), yesterday.strftime("%Y-%m-%d-02-01-00")
|
232
281
|
end
|
233
282
|
end
|
@@ -299,6 +348,109 @@ describe "Cron Job" do
|
|
299
348
|
it 'should return valid payload for Sidekiq::Client' do
|
300
349
|
payload = {
|
301
350
|
'class' => 'ActiveJob::QueueAdapters::SidekiqAdapter::JobWrapper',
|
351
|
+
'wrapped' => 'ActiveJobCronTestClass',
|
352
|
+
'queue' => 'super_queue',
|
353
|
+
'description' => nil,
|
354
|
+
'args' => [{
|
355
|
+
'job_class' => 'ActiveJobCronTestClass',
|
356
|
+
'job_id' => 'XYZ',
|
357
|
+
'queue_name' => 'super_queue',
|
358
|
+
'arguments' => [{foo: 'bar'}]
|
359
|
+
}]
|
360
|
+
}
|
361
|
+
assert_equal @job.active_job_message, payload
|
362
|
+
end
|
363
|
+
end
|
364
|
+
|
365
|
+
describe '#active_job_message - unknown Active Job Worker class' do
|
366
|
+
before do
|
367
|
+
SecureRandom.stubs(:uuid).returns('XYZ')
|
368
|
+
ActiveJob::Base.queue_name_prefix = ''
|
369
|
+
|
370
|
+
@args = {
|
371
|
+
name: 'Test',
|
372
|
+
cron: '* * * * *',
|
373
|
+
klass: 'UnknownActiveJobCronTestClass',
|
374
|
+
active_job: true,
|
375
|
+
queue: 'super_queue',
|
376
|
+
description: nil,
|
377
|
+
args: { foo: 'bar' }
|
378
|
+
}
|
379
|
+
@job = Sidekiq::Cron::Job.new(@args)
|
380
|
+
end
|
381
|
+
|
382
|
+
it 'should return valid payload for Sidekiq::Client' do
|
383
|
+
payload = {
|
384
|
+
'class' => 'ActiveJob::QueueAdapters::SidekiqAdapter::JobWrapper',
|
385
|
+
'wrapped' => 'UnknownActiveJobCronTestClass',
|
386
|
+
'queue' => 'super_queue',
|
387
|
+
'description' => nil,
|
388
|
+
'args' => [{
|
389
|
+
'job_class' => 'UnknownActiveJobCronTestClass',
|
390
|
+
'job_id' => 'XYZ',
|
391
|
+
'queue_name' => 'super_queue',
|
392
|
+
'arguments' => [{foo: 'bar'}]
|
393
|
+
}]
|
394
|
+
}
|
395
|
+
assert_equal @job.active_job_message, payload
|
396
|
+
end
|
397
|
+
end
|
398
|
+
|
399
|
+
describe '#active_job_message with symbolize_args (hash)' do
|
400
|
+
before do
|
401
|
+
SecureRandom.stubs(:uuid).returns('XYZ')
|
402
|
+
ActiveJob::Base.queue_name_prefix = ''
|
403
|
+
|
404
|
+
@args = {
|
405
|
+
name: 'Test',
|
406
|
+
cron: '* * * * *',
|
407
|
+
klass: 'ActiveJobCronTestClass',
|
408
|
+
queue: 'super_queue',
|
409
|
+
description: nil,
|
410
|
+
symbolize_args: true,
|
411
|
+
args: { 'foo' => 'bar' }
|
412
|
+
}
|
413
|
+
@job = Sidekiq::Cron::Job.new(@args)
|
414
|
+
end
|
415
|
+
|
416
|
+
it 'should return valid payload for Sidekiq::Client' do
|
417
|
+
payload = {
|
418
|
+
'class' => 'ActiveJob::QueueAdapters::SidekiqAdapter::JobWrapper',
|
419
|
+
'wrapped' => 'ActiveJobCronTestClass',
|
420
|
+
'queue' => 'super_queue',
|
421
|
+
'description' => nil,
|
422
|
+
'args' => [{
|
423
|
+
'job_class' => 'ActiveJobCronTestClass',
|
424
|
+
'job_id' => 'XYZ',
|
425
|
+
'queue_name' => 'super_queue',
|
426
|
+
'arguments' => [{foo: 'bar'}]
|
427
|
+
}]
|
428
|
+
}
|
429
|
+
assert_equal @job.active_job_message, payload
|
430
|
+
end
|
431
|
+
end
|
432
|
+
|
433
|
+
describe '#active_job_message with symbolize_args (array)' do
|
434
|
+
before do
|
435
|
+
SecureRandom.stubs(:uuid).returns('XYZ')
|
436
|
+
ActiveJob::Base.queue_name_prefix = ''
|
437
|
+
|
438
|
+
@args = {
|
439
|
+
name: 'Test',
|
440
|
+
cron: '* * * * *',
|
441
|
+
klass: 'ActiveJobCronTestClass',
|
442
|
+
queue: 'super_queue',
|
443
|
+
description: nil,
|
444
|
+
symbolize_args: true,
|
445
|
+
args: [{ 'foo' => 'bar' }]
|
446
|
+
}
|
447
|
+
@job = Sidekiq::Cron::Job.new(@args)
|
448
|
+
end
|
449
|
+
|
450
|
+
it 'should return valid payload for Sidekiq::Client' do
|
451
|
+
payload = {
|
452
|
+
'class' => 'ActiveJob::QueueAdapters::SidekiqAdapter::JobWrapper',
|
453
|
+
'wrapped' => 'ActiveJobCronTestClass',
|
302
454
|
'queue' => 'super_queue',
|
303
455
|
'description' => nil,
|
304
456
|
'args' => [{
|
@@ -330,10 +482,11 @@ describe "Cron Job" do
|
|
330
482
|
|
331
483
|
it 'should return valid payload for Sidekiq::Client' do
|
332
484
|
payload = {
|
333
|
-
'class'
|
334
|
-
'
|
485
|
+
'class' => 'ActiveJob::QueueAdapters::SidekiqAdapter::JobWrapper',
|
486
|
+
'wrapped' => 'ActiveJobCronTestClass',
|
487
|
+
'queue' => 'prefix_super_queue',
|
335
488
|
'description' => nil,
|
336
|
-
'args'
|
489
|
+
'args' => [{
|
337
490
|
'job_class' => 'ActiveJobCronTestClass',
|
338
491
|
'job_id' => 'XYZ',
|
339
492
|
'queue_name' => 'prefix_super_queue',
|
@@ -357,7 +510,7 @@ describe "Cron Job" do
|
|
357
510
|
|
358
511
|
it 'pushes to queue active jobs message' do
|
359
512
|
@job.expects(:enqueue_active_job)
|
360
|
-
.returns(
|
513
|
+
.returns(ActiveJobCronTestClass.new)
|
361
514
|
@job.enque!
|
362
515
|
end
|
363
516
|
end
|
@@ -375,7 +528,7 @@ describe "Cron Job" do
|
|
375
528
|
|
376
529
|
it 'pushes to queue active jobs message with queue_name_prefix' do
|
377
530
|
@job.expects(:enqueue_active_job)
|
378
|
-
.returns(
|
531
|
+
.returns(ActiveJobCronTestClass.new)
|
379
532
|
@job.enque!
|
380
533
|
end
|
381
534
|
end
|
@@ -522,7 +675,6 @@ describe "Cron Job" do
|
|
522
675
|
assert @job.save
|
523
676
|
end
|
524
677
|
|
525
|
-
|
526
678
|
it "be saved and found by name" do
|
527
679
|
assert @job.save, "not saved"
|
528
680
|
assert Sidekiq::Cron::Job.find("Test").is_a?(Sidekiq::Cron::Job)
|
@@ -599,14 +751,15 @@ describe "Cron Job" do
|
|
599
751
|
|
600
752
|
it "last_enqueue_time shouldn't be rewritten after save" do
|
601
753
|
#adding last_enqueue_time to initialize is only for test purpose
|
602
|
-
last_enqueue_time = '2013-01-01 23:59:59'
|
754
|
+
last_enqueue_time = '2013-01-01 23:59:59 +0000'
|
755
|
+
expected_enqueue_time = DateTime.parse(last_enqueue_time).to_time.utc
|
603
756
|
Sidekiq::Cron::Job.create(@args.merge('last_enqueue_time' => last_enqueue_time))
|
604
757
|
job = Sidekiq::Cron::Job.find(@args)
|
605
|
-
assert_equal job.last_enqueue_time,
|
758
|
+
assert_equal job.last_enqueue_time, expected_enqueue_time
|
606
759
|
|
607
760
|
Sidekiq::Cron::Job.create(@args)
|
608
761
|
job = Sidekiq::Cron::Job.find(@args)
|
609
|
-
assert_equal job.last_enqueue_time,
|
762
|
+
assert_equal job.last_enqueue_time, expected_enqueue_time, "after second create should have same time"
|
610
763
|
end
|
611
764
|
end
|
612
765
|
|
@@ -775,11 +928,11 @@ describe "Cron Job" do
|
|
775
928
|
cron: "* * * * *",
|
776
929
|
klass: "CronTestClass"
|
777
930
|
}
|
778
|
-
#first time is
|
931
|
+
#first time is always
|
779
932
|
#after next cron time!
|
780
933
|
@time = Time.now.utc + 120
|
781
934
|
end
|
782
|
-
it "be
|
935
|
+
it "be always false when status is disabled" do
|
783
936
|
refute Sidekiq::Cron::Job.new(@args.merge(status: 'disabled')).should_enque? @time
|
784
937
|
refute Sidekiq::Cron::Job.new(@args.merge(status: 'disabled')).should_enque? @time - 60
|
785
938
|
refute Sidekiq::Cron::Job.new(@args.merge(status: 'disabled')).should_enque? @time - 120
|
@@ -883,7 +1036,8 @@ describe "Cron Job" do
|
|
883
1036
|
@jobs_hash['name_of_job']['cron'] = "bad cron"
|
884
1037
|
out = Sidekiq::Cron::Job.load_from_hash @jobs_hash
|
885
1038
|
assert_equal 1, out.size, "should have 1 error"
|
886
|
-
|
1039
|
+
assert_includes out['name_of_job'].first, "bad cron"
|
1040
|
+
assert_includes out['name_of_job'].first, "ArgumentError:"
|
887
1041
|
assert_equal 1, Sidekiq::Cron::Job.all.size, "Should have only 1 job after load"
|
888
1042
|
end
|
889
1043
|
|
@@ -965,7 +1119,6 @@ describe "Cron Job" do
|
|
965
1119
|
|
966
1120
|
assert_equal Sidekiq::Cron::Job.all.first.sidekiq_worker_message, payload
|
967
1121
|
end
|
968
|
-
|
969
1122
|
end
|
970
1123
|
end
|
971
1124
|
end
|
data/test/unit/poller_test.rb
CHANGED
@@ -1,13 +1,9 @@
|
|
1
|
-
# -*- encoding : utf-8 -*-
|
2
1
|
require './test/test_helper'
|
3
2
|
|
4
|
-
|
5
3
|
describe 'Cron Poller' do
|
6
4
|
before do
|
5
|
+
REDIS.with { |c| c.respond_to?(:redis) ? c.redis.flushdb : c.flushdb }
|
7
6
|
Sidekiq.redis = REDIS
|
8
|
-
Sidekiq.redis do |conn|
|
9
|
-
conn.flushdb
|
10
|
-
end
|
11
7
|
|
12
8
|
#clear all previous saved data from redis
|
13
9
|
Sidekiq.redis do |conn|
|
@@ -28,8 +24,8 @@ describe 'Cron Poller' do
|
|
28
24
|
end
|
29
25
|
|
30
26
|
it 'not enqueue any job - new jobs' do
|
31
|
-
now = Time.now.utc
|
32
|
-
enqueued_time = Time.new(now.year, now.month, now.day, now.hour
|
27
|
+
now = Time.now.utc + 3600
|
28
|
+
enqueued_time = Time.new(now.year, now.month, now.day, now.hour, 5, 1)
|
33
29
|
Time.stubs(:now).returns(enqueued_time)
|
34
30
|
#new jobs!
|
35
31
|
Sidekiq::Cron::Job.create(@args)
|
@@ -43,19 +39,20 @@ describe 'Cron Poller' do
|
|
43
39
|
end
|
44
40
|
|
45
41
|
#30 seconds after!
|
46
|
-
enqueued_time = Time.new(now.year, now.month, now.day, now.hour
|
42
|
+
enqueued_time = Time.new(now.year, now.month, now.day, now.hour, 5, 30)
|
47
43
|
Time.stubs(:now).returns(enqueued_time)
|
48
|
-
@poller.enqueue
|
49
44
|
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
45
|
+
@poller.enqueue
|
46
|
+
|
47
|
+
Sidekiq.redis do |conn|
|
48
|
+
assert_equal 0, conn.llen("queue:default")
|
49
|
+
assert_equal 0, conn.llen("queue:super")
|
50
|
+
end
|
54
51
|
end
|
55
52
|
|
56
53
|
it 'should enqueue only job with cron */2' do
|
57
|
-
now = Time.now.utc
|
58
|
-
enqueued_time = Time.new(now.year, now.month, now.day, now.hour
|
54
|
+
now = Time.now.utc + 3600
|
55
|
+
enqueued_time = Time.new(now.year, now.month, now.day, now.hour, 5, 1)
|
59
56
|
Time.stubs(:now).returns(enqueued_time)
|
60
57
|
#new jobs!
|
61
58
|
Sidekiq::Cron::Job.create(@args)
|
@@ -68,7 +65,7 @@ describe 'Cron Poller' do
|
|
68
65
|
assert_equal 0, conn.llen("queue:super")
|
69
66
|
end
|
70
67
|
|
71
|
-
enqueued_time = Time.new(now.year, now.month, now.day, now.hour
|
68
|
+
enqueued_time = Time.new(now.year, now.month, now.day, now.hour, 6, 1)
|
72
69
|
Time.stubs(:now).returns(enqueued_time)
|
73
70
|
@poller.enqueue
|
74
71
|
|
@@ -79,8 +76,8 @@ describe 'Cron Poller' do
|
|
79
76
|
end
|
80
77
|
|
81
78
|
it 'should enqueue both jobs' do
|
82
|
-
now = Time.now.utc
|
83
|
-
enqueued_time = Time.new(now.year, now.month, now.day, now.hour
|
79
|
+
now = Time.now.utc + 3600
|
80
|
+
enqueued_time = Time.new(now.year, now.month, now.day, now.hour, 8, 1)
|
84
81
|
Time.stubs(:now).returns(enqueued_time)
|
85
82
|
#new jobs!
|
86
83
|
Sidekiq::Cron::Job.create(@args)
|
@@ -93,7 +90,7 @@ describe 'Cron Poller' do
|
|
93
90
|
assert_equal 0, conn.llen("queue:super")
|
94
91
|
end
|
95
92
|
|
96
|
-
enqueued_time = Time.new(now.year, now.month, now.day, now.hour
|
93
|
+
enqueued_time = Time.new(now.year, now.month, now.day, now.hour, 10, 5)
|
97
94
|
Time.stubs(:now).returns(enqueued_time)
|
98
95
|
@poller.enqueue
|
99
96
|
|
@@ -104,8 +101,8 @@ describe 'Cron Poller' do
|
|
104
101
|
end
|
105
102
|
|
106
103
|
it 'should enqueue both jobs but only one time each' do
|
107
|
-
now = Time.now.utc
|
108
|
-
enqueued_time = Time.new(now.year, now.month, now.day, now.hour
|
104
|
+
now = Time.now.utc + 3600
|
105
|
+
enqueued_time = Time.new(now.year, now.month, now.day, now.hour, 8, 1)
|
109
106
|
Time.stubs(:now).returns(enqueued_time)
|
110
107
|
#new jobs!
|
111
108
|
Sidekiq::Cron::Job.create(@args)
|
@@ -118,7 +115,7 @@ describe 'Cron Poller' do
|
|
118
115
|
assert_equal 0, conn.llen("queue:super")
|
119
116
|
end
|
120
117
|
|
121
|
-
enqueued_time = Time.new(now.year, now.month, now.day, now.hour
|
118
|
+
enqueued_time = Time.new(now.year, now.month, now.day, now.hour, 20, 1)
|
122
119
|
Time.stubs(:now).returns(enqueued_time)
|
123
120
|
@poller.enqueue
|
124
121
|
Sidekiq.redis do |conn|
|
@@ -126,7 +123,7 @@ describe 'Cron Poller' do
|
|
126
123
|
assert_equal 1, conn.llen("queue:super")
|
127
124
|
end
|
128
125
|
|
129
|
-
enqueued_time = Time.new(now.year, now.month, now.day, now.hour
|
126
|
+
enqueued_time = Time.new(now.year, now.month, now.day, now.hour, 20, 2)
|
130
127
|
Time.stubs(:now).returns(enqueued_time)
|
131
128
|
@poller.enqueue
|
132
129
|
Sidekiq.redis do |conn|
|
@@ -134,7 +131,7 @@ describe 'Cron Poller' do
|
|
134
131
|
assert_equal 1, conn.llen("queue:super")
|
135
132
|
end
|
136
133
|
|
137
|
-
enqueued_time = Time.new(now.year, now.month, now.day, now.hour
|
134
|
+
enqueued_time = Time.new(now.year, now.month, now.day, now.hour, 20, 20)
|
138
135
|
Time.stubs(:now).returns(enqueued_time)
|
139
136
|
@poller.enqueue
|
140
137
|
Sidekiq.redis do |conn|
|
@@ -142,7 +139,7 @@ describe 'Cron Poller' do
|
|
142
139
|
assert_equal 1, conn.llen("queue:super")
|
143
140
|
end
|
144
141
|
|
145
|
-
enqueued_time = Time.new(now.year, now.month, now.day, now.hour
|
142
|
+
enqueued_time = Time.new(now.year, now.month, now.day, now.hour, 20, 50)
|
146
143
|
Time.stubs(:now).returns(enqueued_time)
|
147
144
|
@poller.enqueue
|
148
145
|
Sidekiq.redis do |conn|
|
@@ -1,37 +1,39 @@
|
|
1
1
|
require './test/test_helper'
|
2
2
|
|
3
|
-
def app
|
4
|
-
Sidekiq::Web
|
5
|
-
end
|
6
|
-
|
7
3
|
describe 'Cron web' do
|
8
4
|
include Rack::Test::Methods
|
9
5
|
|
6
|
+
TOKEN = SecureRandom.base64(32).freeze
|
7
|
+
|
8
|
+
def app
|
9
|
+
Sidekiq::Web
|
10
|
+
end
|
11
|
+
|
10
12
|
before do
|
13
|
+
env 'rack.session', { csrf: TOKEN }
|
14
|
+
env 'HTTP_X_CSRF_TOKEN', TOKEN
|
15
|
+
REDIS.with { |c| c.respond_to?(:redis) ? c.redis.flushdb : c.flushdb }
|
11
16
|
Sidekiq.redis = REDIS
|
12
|
-
|
17
|
+
end
|
13
18
|
|
14
|
-
|
15
|
-
|
16
|
-
conn.keys("cron_job*").each do |key|
|
17
|
-
conn.del(key)
|
18
|
-
end
|
19
|
-
end
|
19
|
+
let(:job_name) { "TestNameOfCronJob" }
|
20
|
+
let(:cron_job_name) { "TesQueueNameOfCronJob" }
|
20
21
|
|
21
|
-
|
22
|
-
|
22
|
+
let(:args) do
|
23
|
+
{
|
24
|
+
name: job_name,
|
23
25
|
cron: "*/2 * * * *",
|
24
26
|
klass: "CronTestClass"
|
25
27
|
}
|
28
|
+
end
|
26
29
|
|
27
|
-
|
28
|
-
|
29
|
-
name:
|
30
|
+
let(:cron_args) do
|
31
|
+
{
|
32
|
+
name: cron_job_name,
|
30
33
|
cron: "*/2 * * * *",
|
31
34
|
klass: "CronQueueTestClass",
|
32
35
|
queue: "cron"
|
33
36
|
}
|
34
|
-
|
35
37
|
end
|
36
38
|
|
37
39
|
it 'display cron web' do
|
@@ -41,45 +43,62 @@ describe 'Cron web' do
|
|
41
43
|
|
42
44
|
it 'display cron web with message - no cron jobs' do
|
43
45
|
get '/cron'
|
44
|
-
assert last_response.body.include?('No cron jobs found')
|
46
|
+
assert last_response.body.include?('No cron jobs were found')
|
45
47
|
end
|
46
48
|
|
47
49
|
it 'display cron web with cron jobs table' do
|
48
|
-
Sidekiq::Cron::Job.create(
|
50
|
+
Sidekiq::Cron::Job.create(args)
|
49
51
|
|
50
52
|
get '/cron'
|
51
53
|
assert_equal 200, last_response.status
|
52
|
-
refute last_response.body.include?('No cron jobs found')
|
54
|
+
refute last_response.body.include?('No cron jobs were found')
|
53
55
|
assert last_response.body.include?('table')
|
54
56
|
assert last_response.body.include?("TestNameOfCronJob")
|
55
57
|
end
|
56
58
|
|
57
59
|
describe "work with cron job" do
|
58
|
-
|
59
60
|
before do
|
60
|
-
@job = Sidekiq::Cron::Job.new(
|
61
|
-
@job.save
|
62
|
-
|
61
|
+
@job = Sidekiq::Cron::Job.new(args.merge(status: "enabled"))
|
62
|
+
assert @job.save
|
63
|
+
|
64
|
+
@cron_job = Sidekiq::Cron::Job.new(cron_args.merge(status: "enabled"))
|
65
|
+
assert @cron_job.save
|
66
|
+
end
|
67
|
+
|
68
|
+
it 'shows history of a cron job' do
|
69
|
+
@job.enque!
|
70
|
+
get "/cron/#{job_name}"
|
71
|
+
|
72
|
+
jid =
|
73
|
+
Sidekiq.redis do |conn|
|
74
|
+
history = conn.lrange Sidekiq::Cron::Job.jid_history_key(job_name), 0, -1
|
75
|
+
Sidekiq.load_json(history.last)['jid']
|
76
|
+
end
|
77
|
+
|
78
|
+
assert jid
|
79
|
+
assert last_response.body.include?(jid)
|
80
|
+
end
|
81
|
+
|
82
|
+
it 'redirects to cron path when name not found' do
|
83
|
+
get '/cron/some-fake-name'
|
63
84
|
|
64
|
-
|
65
|
-
@cron_job.save
|
66
|
-
@cron_job_name = "TesQueueNameOfCronJob"
|
85
|
+
assert_match %r{\/cron\z}, last_response['Location']
|
67
86
|
end
|
68
87
|
|
69
88
|
it "disable and enable all cron jobs" do
|
70
89
|
post "/cron/__all__/disable"
|
71
|
-
assert_equal Sidekiq::Cron::Job.find(
|
90
|
+
assert_equal Sidekiq::Cron::Job.find(job_name).status, "disabled"
|
72
91
|
|
73
92
|
post "/cron/__all__/enable"
|
74
|
-
assert_equal Sidekiq::Cron::Job.find(
|
93
|
+
assert_equal Sidekiq::Cron::Job.find(job_name).status, "enabled"
|
75
94
|
end
|
76
95
|
|
77
96
|
it "disable and enable cron job" do
|
78
|
-
post "/cron/#{
|
79
|
-
assert_equal Sidekiq::Cron::Job.find(
|
97
|
+
post "/cron/#{job_name}/disable"
|
98
|
+
assert_equal Sidekiq::Cron::Job.find(job_name).status, "disabled"
|
80
99
|
|
81
|
-
post "/cron/#{
|
82
|
-
assert_equal Sidekiq::Cron::Job.find(
|
100
|
+
post "/cron/#{job_name}/enable"
|
101
|
+
assert_equal Sidekiq::Cron::Job.find(job_name).status, "enabled"
|
83
102
|
end
|
84
103
|
|
85
104
|
it "enqueue all jobs" do
|
@@ -100,21 +119,21 @@ describe 'Cron web' do
|
|
100
119
|
assert_equal 0, conn.llen("queue:default"), "Queue should have no jobs"
|
101
120
|
end
|
102
121
|
|
103
|
-
post "/cron/#{
|
122
|
+
post "/cron/#{job_name}/enque"
|
104
123
|
|
105
124
|
Sidekiq.redis do |conn|
|
106
125
|
assert_equal 1, conn.llen("queue:default"), "Queue should have 1 job"
|
107
126
|
end
|
108
127
|
|
109
128
|
#should enqueue more times
|
110
|
-
post "/cron/#{
|
129
|
+
post "/cron/#{job_name}/enque"
|
111
130
|
|
112
131
|
Sidekiq.redis do |conn|
|
113
132
|
assert_equal 2, conn.llen("queue:default"), "Queue should have 2 job"
|
114
133
|
end
|
115
134
|
|
116
135
|
#should enqueue to cron job queue
|
117
|
-
post "/cron/#{
|
136
|
+
post "/cron/#{cron_job_name}/enque"
|
118
137
|
|
119
138
|
Sidekiq.redis do |conn|
|
120
139
|
assert_equal 1, conn.llen("queue:cron"), "Queue should have 1 cron job"
|
@@ -123,8 +142,8 @@ describe 'Cron web' do
|
|
123
142
|
|
124
143
|
it "destroy job" do
|
125
144
|
assert_equal Sidekiq::Cron::Job.all.size, 2, "Should have 2 job"
|
126
|
-
post "/cron/#{
|
127
|
-
post "/cron/#{
|
145
|
+
post "/cron/#{job_name}/delete"
|
146
|
+
post "/cron/#{cron_job_name}/delete"
|
128
147
|
assert_equal Sidekiq::Cron::Job.all.size, 0, "Should have zero jobs"
|
129
148
|
end
|
130
149
|
|