sidekiq-cron 1.2.0 → 1.7.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/CHANGELOG.md +122 -0
- data/Gemfile +1 -30
- data/README.md +151 -121
- data/Rakefile +3 -42
- data/lib/sidekiq/cron/job.rb +139 -104
- data/lib/sidekiq/cron/launcher.rb +7 -9
- data/lib/sidekiq/cron/locales/en.yml +2 -0
- data/lib/sidekiq/cron/locales/pt.yml +22 -0
- data/lib/sidekiq/cron/poller.rb +18 -9
- data/lib/sidekiq/cron/schedule_loader.rb +22 -0
- data/lib/sidekiq/cron/support.rb +0 -1
- data/lib/sidekiq/cron/version.rb +7 -0
- data/lib/sidekiq/cron/views/cron.erb +19 -13
- data/lib/sidekiq/cron/views/cron_show.erb +8 -8
- data/lib/sidekiq/cron/web.rb +1 -7
- data/lib/sidekiq/cron/web_extension.rb +8 -21
- data/lib/sidekiq/cron.rb +1 -0
- data/lib/sidekiq/options.rb +18 -0
- data/sidekiq-cron.gemspec +25 -115
- data/test/integration/performance_test.rb +4 -5
- data/test/test_helper.rb +11 -23
- data/test/unit/fixtures/schedule_array.yml +13 -0
- data/test/unit/fixtures/schedule_hash.yml +12 -0
- data/test/unit/fixtures/schedule_string.yml +1 -0
- data/test/unit/job_test.rb +227 -23
- data/test/unit/poller_test.rb +7 -9
- data/test/unit/schedule_loader_test.rb +45 -0
- data/test/unit/web_extension_test.rb +40 -40
- metadata +56 -197
- data/.document +0 -5
- data/.travis.yml +0 -18
- data/Changes.md +0 -82
- data/Dockerfile +0 -32
- data/VERSION +0 -1
- data/config.ru +0 -14
- data/docker-compose.yml +0 -23
- data/examples/web-cron-ui.png +0 -0
- data/lib/sidekiq/cron/views/cron.slim +0 -71
- data/lib/sidekiq/cron/views/cron_show.slim +0 -61
data/test/unit/job_test.rb
CHANGED
@@ -1,16 +1,15 @@
|
|
1
|
-
# -*- encoding : utf-8 -*-
|
2
1
|
require './test/test_helper'
|
3
2
|
|
4
3
|
describe "Cron Job" do
|
5
4
|
before do
|
6
|
-
#
|
5
|
+
# Clear all previous saved data from Redis.
|
7
6
|
Sidekiq.redis do |conn|
|
8
7
|
conn.keys("cron_job*").each do |key|
|
9
8
|
conn.del(key)
|
10
9
|
end
|
11
10
|
end
|
12
11
|
|
13
|
-
#
|
12
|
+
# Clear all queues.
|
14
13
|
Sidekiq::Queue.all.each do |queue|
|
15
14
|
queue.clear
|
16
15
|
end
|
@@ -76,7 +75,6 @@ describe "Cron Job" do
|
|
76
75
|
end
|
77
76
|
|
78
77
|
describe "invalid job" do
|
79
|
-
|
80
78
|
before do
|
81
79
|
@job = Sidekiq::Cron::Job.new()
|
82
80
|
end
|
@@ -131,6 +129,22 @@ describe "Cron Job" do
|
|
131
129
|
end
|
132
130
|
end
|
133
131
|
|
132
|
+
describe 'cron formats' do
|
133
|
+
before do
|
134
|
+
@args = {
|
135
|
+
name: "Test",
|
136
|
+
klass: "CronTestClass"
|
137
|
+
}
|
138
|
+
end
|
139
|
+
|
140
|
+
it 'should support natural language format' do
|
141
|
+
@args[:cron] = "every 3 hours"
|
142
|
+
@job = Sidekiq::Cron::Job.new(@args)
|
143
|
+
assert @job.valid?
|
144
|
+
assert_equal Fugit::Cron.new("0 */3 * * *"), @job.send(:parsed_cron)
|
145
|
+
end
|
146
|
+
end
|
147
|
+
|
134
148
|
describe 'parse_enqueue_time' do
|
135
149
|
before do
|
136
150
|
@args = {
|
@@ -233,8 +247,11 @@ describe "Cron Job" do
|
|
233
247
|
"queue"=>:super,
|
234
248
|
"backtrace"=>true,
|
235
249
|
"class"=>"CronTestClassWithQueue"}
|
236
|
-
assert job_args
|
237
|
-
|
250
|
+
assert job_args.empty?
|
251
|
+
|
252
|
+
enqueue_args = job.enqueue_args
|
253
|
+
assert enqueue_args[-1].is_a?(Float)
|
254
|
+
assert enqueue_args[-1].between?(Time.now.to_f - 1, Time.now.to_f)
|
238
255
|
end
|
239
256
|
|
240
257
|
it "be initialized with 'class', 2 arguments and date_as_argument" do
|
@@ -246,11 +263,13 @@ describe "Cron Job" do
|
|
246
263
|
"queue"=>:super,
|
247
264
|
"backtrace"=>true,
|
248
265
|
"class"=>"CronTestClassWithQueue"}
|
249
|
-
|
250
|
-
assert job_args[-1].between?(Time.now.to_f - 1, Time.now.to_f)
|
251
|
-
assert_equal job_args[0..-2], ["arg1", :arg2]
|
252
|
-
end
|
266
|
+
assert_equal job_args, ["arg1", :arg2]
|
253
267
|
|
268
|
+
enqueue_args = job.enqueue_args
|
269
|
+
assert_equal enqueue_args[0..-2], ["arg1", :arg2]
|
270
|
+
assert enqueue_args[-1].is_a?(Float)
|
271
|
+
assert enqueue_args[-1].between?(Time.now.to_f - 1, Time.now.to_f)
|
272
|
+
end
|
254
273
|
end
|
255
274
|
|
256
275
|
describe "cron test" do
|
@@ -304,6 +323,21 @@ describe "Cron Job" do
|
|
304
323
|
}
|
305
324
|
assert_equal @job.sidekiq_worker_message, payload
|
306
325
|
end
|
326
|
+
|
327
|
+
describe 'with date_as_argument' do
|
328
|
+
before do
|
329
|
+
@args.merge!(date_as_argument: true)
|
330
|
+
@job = Sidekiq::Cron::Job.new(@args)
|
331
|
+
end
|
332
|
+
|
333
|
+
let(:args) { @job.sidekiq_worker_message['args'] }
|
334
|
+
|
335
|
+
it 'should add timestamp to args' do
|
336
|
+
assert_equal args[0], {foo: 'bar'}
|
337
|
+
assert args[-1].is_a?(Float)
|
338
|
+
assert args[-1].between?(Time.now.to_f - 1, Time.now.to_f)
|
339
|
+
end
|
340
|
+
end
|
307
341
|
end
|
308
342
|
|
309
343
|
describe '#sidekiq_worker_message settings overwrite queue name' do
|
@@ -361,6 +395,124 @@ describe "Cron Job" do
|
|
361
395
|
}
|
362
396
|
assert_equal @job.active_job_message, payload
|
363
397
|
end
|
398
|
+
|
399
|
+
describe 'with date_as_argument' do
|
400
|
+
before do
|
401
|
+
@args.merge!(date_as_argument: true)
|
402
|
+
@job = Sidekiq::Cron::Job.new(@args)
|
403
|
+
end
|
404
|
+
|
405
|
+
let(:args) { @job.active_job_message['args'][0]['arguments'] }
|
406
|
+
|
407
|
+
it 'should add timestamp to args' do
|
408
|
+
args = @job.active_job_message['args'][0]['arguments']
|
409
|
+
assert_equal args[0], {foo: 'bar'}
|
410
|
+
assert args[-1].is_a?(Float)
|
411
|
+
assert args[-1].between?(Time.now.to_f - 1, Time.now.to_f)
|
412
|
+
end
|
413
|
+
end
|
414
|
+
end
|
415
|
+
|
416
|
+
describe '#active_job_message - unknown Active Job Worker class' do
|
417
|
+
before do
|
418
|
+
SecureRandom.stubs(:uuid).returns('XYZ')
|
419
|
+
ActiveJob::Base.queue_name_prefix = ''
|
420
|
+
|
421
|
+
@args = {
|
422
|
+
name: 'Test',
|
423
|
+
cron: '* * * * *',
|
424
|
+
klass: 'UnknownActiveJobCronTestClass',
|
425
|
+
active_job: true,
|
426
|
+
queue: 'super_queue',
|
427
|
+
description: nil,
|
428
|
+
args: { foo: 'bar' }
|
429
|
+
}
|
430
|
+
@job = Sidekiq::Cron::Job.new(@args)
|
431
|
+
end
|
432
|
+
|
433
|
+
it 'should return valid payload for Sidekiq::Client' do
|
434
|
+
payload = {
|
435
|
+
'class' => 'ActiveJob::QueueAdapters::SidekiqAdapter::JobWrapper',
|
436
|
+
'wrapped' => 'UnknownActiveJobCronTestClass',
|
437
|
+
'queue' => 'super_queue',
|
438
|
+
'description' => nil,
|
439
|
+
'args' => [{
|
440
|
+
'job_class' => 'UnknownActiveJobCronTestClass',
|
441
|
+
'job_id' => 'XYZ',
|
442
|
+
'queue_name' => 'super_queue',
|
443
|
+
'arguments' => [{foo: 'bar'}]
|
444
|
+
}]
|
445
|
+
}
|
446
|
+
assert_equal @job.active_job_message, payload
|
447
|
+
end
|
448
|
+
end
|
449
|
+
|
450
|
+
describe '#active_job_message with symbolize_args (hash)' do
|
451
|
+
before do
|
452
|
+
SecureRandom.stubs(:uuid).returns('XYZ')
|
453
|
+
ActiveJob::Base.queue_name_prefix = ''
|
454
|
+
|
455
|
+
@args = {
|
456
|
+
name: 'Test',
|
457
|
+
cron: '* * * * *',
|
458
|
+
klass: 'ActiveJobCronTestClass',
|
459
|
+
queue: 'super_queue',
|
460
|
+
description: nil,
|
461
|
+
symbolize_args: true,
|
462
|
+
args: { 'foo' => 'bar' }
|
463
|
+
}
|
464
|
+
@job = Sidekiq::Cron::Job.new(@args)
|
465
|
+
end
|
466
|
+
|
467
|
+
it 'should return valid payload for Sidekiq::Client' do
|
468
|
+
payload = {
|
469
|
+
'class' => 'ActiveJob::QueueAdapters::SidekiqAdapter::JobWrapper',
|
470
|
+
'wrapped' => 'ActiveJobCronTestClass',
|
471
|
+
'queue' => 'super_queue',
|
472
|
+
'description' => nil,
|
473
|
+
'args' => [{
|
474
|
+
'job_class' => 'ActiveJobCronTestClass',
|
475
|
+
'job_id' => 'XYZ',
|
476
|
+
'queue_name' => 'super_queue',
|
477
|
+
'arguments' => [{foo: 'bar'}]
|
478
|
+
}]
|
479
|
+
}
|
480
|
+
assert_equal @job.active_job_message, payload
|
481
|
+
end
|
482
|
+
end
|
483
|
+
|
484
|
+
describe '#active_job_message with symbolize_args (array)' do
|
485
|
+
before do
|
486
|
+
SecureRandom.stubs(:uuid).returns('XYZ')
|
487
|
+
ActiveJob::Base.queue_name_prefix = ''
|
488
|
+
|
489
|
+
@args = {
|
490
|
+
name: 'Test',
|
491
|
+
cron: '* * * * *',
|
492
|
+
klass: 'ActiveJobCronTestClass',
|
493
|
+
queue: 'super_queue',
|
494
|
+
description: nil,
|
495
|
+
symbolize_args: true,
|
496
|
+
args: [{ 'foo' => 'bar' }]
|
497
|
+
}
|
498
|
+
@job = Sidekiq::Cron::Job.new(@args)
|
499
|
+
end
|
500
|
+
|
501
|
+
it 'should return valid payload for Sidekiq::Client' do
|
502
|
+
payload = {
|
503
|
+
'class' => 'ActiveJob::QueueAdapters::SidekiqAdapter::JobWrapper',
|
504
|
+
'wrapped' => 'ActiveJobCronTestClass',
|
505
|
+
'queue' => 'super_queue',
|
506
|
+
'description' => nil,
|
507
|
+
'args' => [{
|
508
|
+
'job_class' => 'ActiveJobCronTestClass',
|
509
|
+
'job_id' => 'XYZ',
|
510
|
+
'queue_name' => 'super_queue',
|
511
|
+
'arguments' => [{foo: 'bar'}]
|
512
|
+
}]
|
513
|
+
}
|
514
|
+
assert_equal @job.active_job_message, payload
|
515
|
+
end
|
364
516
|
end
|
365
517
|
|
366
518
|
describe '#active_job_message with queue_name_prefix' do
|
@@ -412,6 +564,23 @@ describe "Cron Job" do
|
|
412
564
|
.returns(ActiveJobCronTestClass.new)
|
413
565
|
@job.enque!
|
414
566
|
end
|
567
|
+
|
568
|
+
describe 'with date_as_argument' do
|
569
|
+
before do
|
570
|
+
@args.merge!(date_as_argument: true)
|
571
|
+
@job = Sidekiq::Cron::Job.new(@args)
|
572
|
+
end
|
573
|
+
|
574
|
+
it 'should add timestamp to args' do
|
575
|
+
ActiveJobCronTestClass.expects(:perform_later)
|
576
|
+
.returns(ActiveJobCronTestClass.new)
|
577
|
+
.with { |*args|
|
578
|
+
assert args[-1].is_a?(Float)
|
579
|
+
assert args[-1].between?(Time.now.to_f - 1, Time.now.to_f)
|
580
|
+
}
|
581
|
+
@job.enque!
|
582
|
+
end
|
583
|
+
end
|
415
584
|
end
|
416
585
|
|
417
586
|
describe 'active job with queue_name_prefix' do
|
@@ -539,6 +708,24 @@ describe "Cron Job" do
|
|
539
708
|
.returns(true)
|
540
709
|
@job.enque!
|
541
710
|
end
|
711
|
+
|
712
|
+
describe 'with date_as_argument' do
|
713
|
+
before do
|
714
|
+
@args.merge!(date_as_argument: true)
|
715
|
+
@job = Sidekiq::Cron::Job.new(@args)
|
716
|
+
end
|
717
|
+
|
718
|
+
it 'should add timestamp to args' do
|
719
|
+
CronTestClass::Setter.any_instance
|
720
|
+
.expects(:perform_async)
|
721
|
+
.returns(true)
|
722
|
+
.with { |*args|
|
723
|
+
assert args[-1].is_a?(Float)
|
724
|
+
assert args[-1].between?(Time.now.to_f - 1, Time.now.to_f)
|
725
|
+
}
|
726
|
+
@job.enque!
|
727
|
+
end
|
728
|
+
end
|
542
729
|
end
|
543
730
|
|
544
731
|
describe 'sidekiq worker unknown class' do
|
@@ -649,7 +836,7 @@ describe "Cron Job" do
|
|
649
836
|
end
|
650
837
|
|
651
838
|
it "last_enqueue_time shouldn't be rewritten after save" do
|
652
|
-
#
|
839
|
+
# Adding last_enqueue_time to initialize is only for testing purposes.
|
653
840
|
last_enqueue_time = '2013-01-01 23:59:59 +0000'
|
654
841
|
expected_enqueue_time = DateTime.parse(last_enqueue_time).to_time.utc
|
655
842
|
Sidekiq::Cron::Job.create(@args.merge('last_enqueue_time' => last_enqueue_time))
|
@@ -675,6 +862,7 @@ describe "Cron Job" do
|
|
675
862
|
assert_equal job.name, "Test"
|
676
863
|
end
|
677
864
|
end
|
865
|
+
|
678
866
|
it "from String" do
|
679
867
|
args = {
|
680
868
|
name: "Test",
|
@@ -687,6 +875,7 @@ describe "Cron Job" do
|
|
687
875
|
assert_equal job.name, "Test"
|
688
876
|
end
|
689
877
|
end
|
878
|
+
|
690
879
|
it "from Array" do
|
691
880
|
args = {
|
692
881
|
name: "Test",
|
@@ -758,7 +947,6 @@ describe "Cron Job" do
|
|
758
947
|
Sidekiq::Cron::Job.create(@args)
|
759
948
|
assert Sidekiq::Cron::Job.find('name' => "Test"), "String keys"
|
760
949
|
end
|
761
|
-
|
762
950
|
end
|
763
951
|
|
764
952
|
describe "destroy" do
|
@@ -795,7 +983,6 @@ describe "Cron Job" do
|
|
795
983
|
Sidekiq::Cron::Job.create(@args)
|
796
984
|
assert Sidekiq::Cron::Job.destroy('name' => "Test"), "String keys"
|
797
985
|
end
|
798
|
-
|
799
986
|
end
|
800
987
|
|
801
988
|
describe "destroy_removed_jobs" do
|
@@ -827,11 +1014,11 @@ describe "Cron Job" do
|
|
827
1014
|
cron: "* * * * *",
|
828
1015
|
klass: "CronTestClass"
|
829
1016
|
}
|
830
|
-
#
|
831
|
-
#after next cron time!
|
1017
|
+
# First time is always after next cron time!
|
832
1018
|
@time = Time.now.utc + 120
|
833
1019
|
end
|
834
|
-
|
1020
|
+
|
1021
|
+
it "be always false when status is disabled" do
|
835
1022
|
refute Sidekiq::Cron::Job.new(@args.merge(status: 'disabled')).should_enque? @time
|
836
1023
|
refute Sidekiq::Cron::Job.new(@args.merge(status: 'disabled')).should_enque? @time - 60
|
837
1024
|
refute Sidekiq::Cron::Job.new(@args.merge(status: 'disabled')).should_enque? @time - 120
|
@@ -852,7 +1039,6 @@ describe "Cron Job" do
|
|
852
1039
|
assert Sidekiq::Cron::Job.new(@args).should_enque? @time + 235
|
853
1040
|
refute Sidekiq::Cron::Job.new(@args).should_enque? @time + 235
|
854
1041
|
|
855
|
-
#just for check
|
856
1042
|
refute Sidekiq::Cron::Job.new(@args).should_enque? @time
|
857
1043
|
refute Sidekiq::Cron::Job.new(@args).should_enque? @time + 135
|
858
1044
|
refute Sidekiq::Cron::Job.new(@args).should_enque? @time + 235
|
@@ -868,7 +1054,7 @@ describe "Cron Job" do
|
|
868
1054
|
assert job.test_and_enque_for_time!(@time), "should enqueue"
|
869
1055
|
|
870
1056
|
future_now = @time + 1 * 60 * 60
|
871
|
-
Time.stubs(:now).returns(future_now) #
|
1057
|
+
Time.stubs(:now).returns(future_now) # Save uses Time.now.utc
|
872
1058
|
job.save
|
873
1059
|
assert Sidekiq::Cron::Job.new(@args).test_and_enque_for_time!(future_now + 30), "should enqueue"
|
874
1060
|
end
|
@@ -885,7 +1071,7 @@ describe "Cron Job" do
|
|
885
1071
|
end
|
886
1072
|
assert_equal Sidekiq::Queue.all.first.size, 1, "Sidekiq queue 1 job in queue"
|
887
1073
|
|
888
|
-
# 20 hours after
|
1074
|
+
# 20 hours after.
|
889
1075
|
assert Sidekiq::Cron::Job.new(@args).test_and_enque_for_time! @time + 1 * 60 * 60
|
890
1076
|
refute Sidekiq::Cron::Job.new(@args).test_and_enque_for_time! @time + 1 * 60 * 60
|
891
1077
|
|
@@ -894,7 +1080,7 @@ describe "Cron Job" do
|
|
894
1080
|
end
|
895
1081
|
assert_equal Sidekiq::Queue.all.first.size, 2, "Sidekiq queue 2 jobs in queue"
|
896
1082
|
|
897
|
-
# 26 hour after
|
1083
|
+
# 26 hour after.
|
898
1084
|
assert Sidekiq::Cron::Job.new(@args).test_and_enque_for_time! @time + 26 * 60 * 60
|
899
1085
|
refute Sidekiq::Cron::Job.new(@args).test_and_enque_for_time! @time + 26 * 60 * 60
|
900
1086
|
|
@@ -906,7 +1092,6 @@ describe "Cron Job" do
|
|
906
1092
|
end
|
907
1093
|
|
908
1094
|
describe "load" do
|
909
|
-
|
910
1095
|
describe "from hash" do
|
911
1096
|
before do
|
912
1097
|
@jobs_hash = {
|
@@ -929,9 +1114,19 @@ describe "Cron Job" do
|
|
929
1114
|
assert_equal Sidekiq::Cron::Job.all.size, 2, "Should have 2 jobs after load"
|
930
1115
|
end
|
931
1116
|
|
1117
|
+
it "duplicate jobs are not loaded" do
|
1118
|
+
out = Sidekiq::Cron::Job.load_from_hash @jobs_hash
|
1119
|
+
assert_equal out.size, 0, "should have no errors"
|
1120
|
+
assert_equal Sidekiq::Cron::Job.all.size, 2, "Should have 2 jobs after load"
|
1121
|
+
|
1122
|
+
out_2 = Sidekiq::Cron::Job.load_from_hash @jobs_hash
|
1123
|
+
assert_equal out_2.size, 0, "should have no errors"
|
1124
|
+
assert_equal Sidekiq::Cron::Job.all.size, 2, "Should have 2 jobs after loading again"
|
1125
|
+
end
|
1126
|
+
|
932
1127
|
it "return errors on loaded jobs" do
|
933
1128
|
assert_equal Sidekiq::Cron::Job.all.size, 0, "Should have 0 jobs before load"
|
934
|
-
#
|
1129
|
+
# Set something bad to hash.
|
935
1130
|
@jobs_hash['name_of_job']['cron'] = "bad cron"
|
936
1131
|
out = Sidekiq::Cron::Job.load_from_hash @jobs_hash
|
937
1132
|
assert_equal 1, out.size, "should have 1 error"
|
@@ -981,6 +1176,16 @@ describe "Cron Job" do
|
|
981
1176
|
assert_equal Sidekiq::Cron::Job.all.size, 2, "Should have 2 jobs after load"
|
982
1177
|
end
|
983
1178
|
|
1179
|
+
it "duplicate jobs are not loaded" do
|
1180
|
+
out = Sidekiq::Cron::Job.load_from_array @jobs_array
|
1181
|
+
assert_equal out.size, 0, "should have no errors"
|
1182
|
+
assert_equal Sidekiq::Cron::Job.all.size, 2, "Should have 2 jobs after load"
|
1183
|
+
|
1184
|
+
out_2 = Sidekiq::Cron::Job.load_from_array @jobs_array
|
1185
|
+
assert_equal out_2.size, 0, "should have no errors"
|
1186
|
+
assert_equal Sidekiq::Cron::Job.all.size, 2, "Should have 2 jobs after loading again"
|
1187
|
+
end
|
1188
|
+
|
984
1189
|
it "create new jobs and update old one with same settings with load_from_array" do
|
985
1190
|
assert_equal Sidekiq::Cron::Job.all.size, 0, "Should have 0 jobs before load"
|
986
1191
|
out = Sidekiq::Cron::Job.load_from_array! @jobs_array
|
@@ -1018,7 +1223,6 @@ describe "Cron Job" do
|
|
1018
1223
|
|
1019
1224
|
assert_equal Sidekiq::Cron::Job.all.first.sidekiq_worker_message, payload
|
1020
1225
|
end
|
1021
|
-
|
1022
1226
|
end
|
1023
1227
|
end
|
1024
1228
|
end
|
data/test/unit/poller_test.rb
CHANGED
@@ -1,13 +1,11 @@
|
|
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
|
-
Redis.current.flushdb
|
9
7
|
|
10
|
-
#
|
8
|
+
# Clear all previous saved data from Redis.
|
11
9
|
Sidekiq.redis do |conn|
|
12
10
|
conn.keys("cron_job*").each do |key|
|
13
11
|
conn.del(key)
|
@@ -29,7 +27,7 @@ describe 'Cron Poller' do
|
|
29
27
|
now = Time.now.utc + 3600
|
30
28
|
enqueued_time = Time.new(now.year, now.month, now.day, now.hour, 5, 1)
|
31
29
|
Time.stubs(:now).returns(enqueued_time)
|
32
|
-
|
30
|
+
|
33
31
|
Sidekiq::Cron::Job.create(@args)
|
34
32
|
Sidekiq::Cron::Job.create(@args2)
|
35
33
|
|
@@ -40,7 +38,7 @@ describe 'Cron Poller' do
|
|
40
38
|
assert_equal 0, conn.llen("queue:super")
|
41
39
|
end
|
42
40
|
|
43
|
-
#30 seconds after!
|
41
|
+
# 30 seconds after!
|
44
42
|
enqueued_time = Time.new(now.year, now.month, now.day, now.hour, 5, 30)
|
45
43
|
Time.stubs(:now).returns(enqueued_time)
|
46
44
|
|
@@ -56,7 +54,7 @@ describe 'Cron Poller' do
|
|
56
54
|
now = Time.now.utc + 3600
|
57
55
|
enqueued_time = Time.new(now.year, now.month, now.day, now.hour, 5, 1)
|
58
56
|
Time.stubs(:now).returns(enqueued_time)
|
59
|
-
|
57
|
+
|
60
58
|
Sidekiq::Cron::Job.create(@args)
|
61
59
|
Sidekiq::Cron::Job.create(@args2)
|
62
60
|
|
@@ -81,7 +79,7 @@ describe 'Cron Poller' do
|
|
81
79
|
now = Time.now.utc + 3600
|
82
80
|
enqueued_time = Time.new(now.year, now.month, now.day, now.hour, 8, 1)
|
83
81
|
Time.stubs(:now).returns(enqueued_time)
|
84
|
-
|
82
|
+
|
85
83
|
Sidekiq::Cron::Job.create(@args)
|
86
84
|
Sidekiq::Cron::Job.create(@args2)
|
87
85
|
|
@@ -106,7 +104,7 @@ describe 'Cron Poller' do
|
|
106
104
|
now = Time.now.utc + 3600
|
107
105
|
enqueued_time = Time.new(now.year, now.month, now.day, now.hour, 8, 1)
|
108
106
|
Time.stubs(:now).returns(enqueued_time)
|
109
|
-
|
107
|
+
|
110
108
|
Sidekiq::Cron::Job.create(@args)
|
111
109
|
Sidekiq::Cron::Job.create(@args2)
|
112
110
|
|
@@ -0,0 +1,45 @@
|
|
1
|
+
require './test/test_helper'
|
2
|
+
|
3
|
+
describe 'ScheduleLoader' do
|
4
|
+
before do
|
5
|
+
Sidekiq.options[:lifecycle_events][:startup].clear
|
6
|
+
end
|
7
|
+
|
8
|
+
describe 'Schedule is defined in hash' do
|
9
|
+
before do
|
10
|
+
Sidekiq::Options[:cron_schedule_file] = 'test/unit/fixtures/schedule_hash.yml'
|
11
|
+
load 'sidekiq/cron/schedule_loader.rb'
|
12
|
+
end
|
13
|
+
|
14
|
+
it 'calls Sidekiq::Cron::Job.load_from_hash' do
|
15
|
+
Sidekiq::Cron::Job.expects(:load_from_hash)
|
16
|
+
Sidekiq.options[:lifecycle_events][:startup].first.call
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
describe 'Schedule is defined in array' do
|
21
|
+
before do
|
22
|
+
Sidekiq::Options[:cron_schedule_file] = 'test/unit/fixtures/schedule_array.yml'
|
23
|
+
load 'sidekiq/cron/schedule_loader.rb'
|
24
|
+
end
|
25
|
+
|
26
|
+
it 'calls Sidekiq::Cron::Job.load_from_array' do
|
27
|
+
Sidekiq::Cron::Job.expects(:load_from_array)
|
28
|
+
Sidekiq.options[:lifecycle_events][:startup].first.call
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
describe 'Schedule is not defined in hash nor array' do
|
33
|
+
before do
|
34
|
+
Sidekiq::Options[:cron_schedule_file] = 'test/unit/fixtures/schedule_string.yml'
|
35
|
+
load 'sidekiq/cron/schedule_loader.rb'
|
36
|
+
end
|
37
|
+
|
38
|
+
it 'raises an error' do
|
39
|
+
e = assert_raises StandardError do
|
40
|
+
Sidekiq.options[:lifecycle_events][:startup].first.call
|
41
|
+
end
|
42
|
+
assert_equal 'Not supported schedule format. Confirm your test/unit/fixtures/schedule_string.yml', e.message
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
@@ -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
|
@@ -45,7 +47,7 @@ describe 'Cron web' do
|
|
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
|
@@ -55,27 +57,25 @@ describe 'Cron web' do
|
|
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
|
-
@name = "TestNameOfCronJob"
|
61
|
+
@job = Sidekiq::Cron::Job.new(args.merge(status: "enabled"))
|
62
|
+
assert @job.save
|
63
63
|
|
64
|
-
@cron_job = Sidekiq::Cron::Job.new(
|
65
|
-
@cron_job.save
|
66
|
-
@cron_job_name = "TesQueueNameOfCronJob"
|
64
|
+
@cron_job = Sidekiq::Cron::Job.new(cron_args.merge(status: "enabled"))
|
65
|
+
assert @cron_job.save
|
67
66
|
end
|
68
67
|
|
69
68
|
it 'shows history of a cron job' do
|
70
69
|
@job.enque!
|
71
|
-
get "/cron/#{
|
70
|
+
get "/cron/#{job_name}"
|
72
71
|
|
73
72
|
jid =
|
74
73
|
Sidekiq.redis do |conn|
|
75
|
-
history = conn.lrange Sidekiq::Cron::Job.jid_history_key(
|
74
|
+
history = conn.lrange Sidekiq::Cron::Job.jid_history_key(job_name), 0, -1
|
76
75
|
Sidekiq.load_json(history.last)['jid']
|
77
76
|
end
|
78
77
|
|
78
|
+
assert jid
|
79
79
|
assert last_response.body.include?(jid)
|
80
80
|
end
|
81
81
|
|
@@ -87,18 +87,18 @@ describe 'Cron web' do
|
|
87
87
|
|
88
88
|
it "disable and enable all cron jobs" do
|
89
89
|
post "/cron/__all__/disable"
|
90
|
-
assert_equal Sidekiq::Cron::Job.find(
|
90
|
+
assert_equal Sidekiq::Cron::Job.find(job_name).status, "disabled"
|
91
91
|
|
92
92
|
post "/cron/__all__/enable"
|
93
|
-
assert_equal Sidekiq::Cron::Job.find(
|
93
|
+
assert_equal Sidekiq::Cron::Job.find(job_name).status, "enabled"
|
94
94
|
end
|
95
95
|
|
96
96
|
it "disable and enable cron job" do
|
97
|
-
post "/cron/#{
|
98
|
-
assert_equal Sidekiq::Cron::Job.find(
|
97
|
+
post "/cron/#{job_name}/disable"
|
98
|
+
assert_equal Sidekiq::Cron::Job.find(job_name).status, "disabled"
|
99
99
|
|
100
|
-
post "/cron/#{
|
101
|
-
assert_equal Sidekiq::Cron::Job.find(
|
100
|
+
post "/cron/#{job_name}/enable"
|
101
|
+
assert_equal Sidekiq::Cron::Job.find(job_name).status, "enabled"
|
102
102
|
end
|
103
103
|
|
104
104
|
it "enqueue all jobs" do
|
@@ -119,21 +119,21 @@ describe 'Cron web' do
|
|
119
119
|
assert_equal 0, conn.llen("queue:default"), "Queue should have no jobs"
|
120
120
|
end
|
121
121
|
|
122
|
-
post "/cron/#{
|
122
|
+
post "/cron/#{job_name}/enque"
|
123
123
|
|
124
124
|
Sidekiq.redis do |conn|
|
125
125
|
assert_equal 1, conn.llen("queue:default"), "Queue should have 1 job"
|
126
126
|
end
|
127
127
|
|
128
|
-
#
|
129
|
-
post "/cron/#{
|
128
|
+
# Should enqueue more times.
|
129
|
+
post "/cron/#{job_name}/enque"
|
130
130
|
|
131
131
|
Sidekiq.redis do |conn|
|
132
132
|
assert_equal 2, conn.llen("queue:default"), "Queue should have 2 job"
|
133
133
|
end
|
134
134
|
|
135
|
-
#
|
136
|
-
post "/cron/#{
|
135
|
+
# Should enqueue to cron job queue.
|
136
|
+
post "/cron/#{cron_job_name}/enque"
|
137
137
|
|
138
138
|
Sidekiq.redis do |conn|
|
139
139
|
assert_equal 1, conn.llen("queue:cron"), "Queue should have 1 cron job"
|
@@ -142,8 +142,8 @@ describe 'Cron web' do
|
|
142
142
|
|
143
143
|
it "destroy job" do
|
144
144
|
assert_equal Sidekiq::Cron::Job.all.size, 2, "Should have 2 job"
|
145
|
-
post "/cron/#{
|
146
|
-
post "/cron/#{
|
145
|
+
post "/cron/#{job_name}/delete"
|
146
|
+
post "/cron/#{cron_job_name}/delete"
|
147
147
|
assert_equal Sidekiq::Cron::Job.all.size, 0, "Should have zero jobs"
|
148
148
|
end
|
149
149
|
|