sidekiq-cron 1.6.0 → 1.7.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,24 +1,21 @@
1
1
  require 'sidekiq'
2
2
  require 'sidekiq/cron'
3
3
  require 'sidekiq/scheduled'
4
+ require 'sidekiq/options'
4
5
 
5
6
  module Sidekiq
6
7
  module Cron
7
- POLL_INTERVAL = 30
8
+ POLL_INTERVAL = Sidekiq::Options[:average_scheduled_poll_interval] || 30
8
9
 
9
- # The Poller checks Redis every N seconds for sheduled cron jobs
10
+ # The Poller checks Redis every N seconds for sheduled cron jobs.
10
11
  class Poller < Sidekiq::Scheduled::Poller
11
12
  def initialize
12
- Sidekiq.configure_server do |config|
13
- if config.respond_to?(:[])
14
- config[:poll_interval_average] = config[:average_scheduled_poll_interval] || POLL_INTERVAL
15
- else
16
- config.options[:poll_interval_average] = config.options[:average_scheduled_poll_interval] || POLL_INTERVAL
17
- end
13
+ Sidekiq.configure_server do
14
+ Sidekiq::Options[:poll_interval_average] = POLL_INTERVAL
18
15
  end
19
16
 
20
- if Gem::Version.new(Sidekiq::VERSION) >= Gem::Version.new("6.5.0")
21
- # Sidekiq Poller init requires a config argument
17
+ if Gem::Version.new(Sidekiq::VERSION) >= Gem::Version.new('6.5.0')
18
+ # Sidekiq Poller init requires a config argument.
22
19
  super(Sidekiq)
23
20
  else
24
21
  super
@@ -32,7 +29,7 @@ module Sidekiq
32
29
  end
33
30
  rescue => ex
34
31
  # Most likely a problem with redis networking.
35
- # Punt and try again at the next interval
32
+ # Punt and try again at the next interval.
36
33
  Sidekiq.logger.error ex.message
37
34
  Sidekiq.logger.error ex.backtrace.first
38
35
  handle_exception(ex) if respond_to?(:handle_exception)
@@ -43,7 +40,7 @@ module Sidekiq
43
40
  def enqueue_job(job, time = Time.now.utc)
44
41
  job.test_and_enque_for_time! time if job && job.valid?
45
42
  rescue => ex
46
- # problem somewhere in one job
43
+ # Problem somewhere in one job.
47
44
  Sidekiq.logger.error "CRON JOB: #{ex.message}"
48
45
  Sidekiq.logger.error "CRON JOB: #{ex.backtrace.first}"
49
46
  handle_exception(ex) if respond_to?(:handle_exception)
@@ -1,13 +1,21 @@
1
- require "sidekiq"
2
- require "sidekiq/cron/job"
1
+ require 'sidekiq'
2
+ require 'sidekiq/cron/job'
3
+ require 'sidekiq/options'
3
4
 
4
5
  if Sidekiq.server?
5
6
  Sidekiq.configure_server do |config|
6
- schedule_file = config.options[:cron_schedule_file] || "config/schedule.yml"
7
+ schedule_file = Sidekiq::Options[:cron_schedule_file] || 'config/schedule.yml'
7
8
 
8
9
  if File.exist?(schedule_file)
9
10
  config.on(:startup) do
10
- Sidekiq::Cron::Job.load_from_hash YAML.load_file(schedule_file)
11
+ schedule = YAML.load_file(schedule_file)
12
+ if schedule.kind_of?(Hash)
13
+ Sidekiq::Cron::Job.load_from_hash schedule
14
+ elsif schedule.kind_of?(Array)
15
+ Sidekiq::Cron::Job.load_from_array schedule
16
+ else
17
+ raise "Not supported schedule format. Confirm your #{schedule_file}"
18
+ end
11
19
  end
12
20
  end
13
21
  end
@@ -28,7 +28,6 @@ module Sidekiq
28
28
  const
29
29
  end
30
30
 
31
- # owner is in Object, so raise
32
31
  constant.const_get(name, false)
33
32
  end
34
33
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Sidekiq
4
4
  module Cron
5
- VERSION = "1.6.0"
5
+ VERSION = "1.7.0"
6
6
  end
7
7
  end
@@ -1,12 +1,10 @@
1
1
  module Sidekiq
2
2
  module Cron
3
3
  module WebExtension
4
-
5
4
  def self.registered(app)
6
-
7
5
  app.settings.locales << File.join(File.expand_path("..", __FILE__), "locales")
8
6
 
9
- #index page of cron jobs
7
+ # Index page of cron jobs.
10
8
  app.get '/cron' do
11
9
  view_path = File.join(File.expand_path("..", __FILE__), "views")
12
10
 
@@ -15,7 +13,7 @@ module Sidekiq
15
13
  render(:erb, File.read(File.join(view_path, "cron.erb")))
16
14
  end
17
15
 
18
- # display job detail + jid history
16
+ # Display job detail + jid history.
19
17
  app.get '/cron/:name' do
20
18
  view_path = File.join(File.expand_path("..", __FILE__), "views")
21
19
 
@@ -27,7 +25,7 @@ module Sidekiq
27
25
  end
28
26
  end
29
27
 
30
- #enque cron job
28
+ # Enqueue cron job.
31
29
  app.post '/cron/:name/enque' do
32
30
  if route_params[:name] === '__all__'
33
31
  Sidekiq::Cron::Job.all.each(&:enque!)
@@ -37,7 +35,7 @@ module Sidekiq
37
35
  redirect params['redirect'] || "#{root_path}cron"
38
36
  end
39
37
 
40
- #delete schedule
38
+ # Delete schedule.
41
39
  app.post '/cron/:name/delete' do
42
40
  if route_params[:name] === '__all__'
43
41
  Sidekiq::Cron::Job.all.each(&:destroy)
@@ -47,7 +45,7 @@ module Sidekiq
47
45
  redirect "#{root_path}cron"
48
46
  end
49
47
 
50
- #enable job
48
+ # Enable job.
51
49
  app.post '/cron/:name/enable' do
52
50
  if route_params[:name] === '__all__'
53
51
  Sidekiq::Cron::Job.all.each(&:enable!)
@@ -57,7 +55,7 @@ module Sidekiq
57
55
  redirect params['redirect'] || "#{root_path}cron"
58
56
  end
59
57
 
60
- #disable job
58
+ # Disable job.
61
59
  app.post '/cron/:name/disable' do
62
60
  if route_params[:name] === '__all__'
63
61
  Sidekiq::Cron::Job.all.each(&:disable!)
@@ -66,7 +64,6 @@ module Sidekiq
66
64
  end
67
65
  redirect params['redirect'] || "#{root_path}cron"
68
66
  end
69
-
70
67
  end
71
68
  end
72
69
  end
@@ -0,0 +1,18 @@
1
+ require 'sidekiq'
2
+
3
+ module Sidekiq
4
+ module Options
5
+ def self.[](key)
6
+ new_version? ? Sidekiq[key] : Sidekiq.options[key]
7
+ end
8
+
9
+ def self.[]=(key, value)
10
+ new_version? ? Sidekiq[key] = value : Sidekiq.options[key] = value
11
+ end
12
+
13
+ # sidekiq --version >= 6.5.0
14
+ def self.new_version?
15
+ @new_version ||= Sidekiq.respond_to?(:[])
16
+ end
17
+ end
18
+ end
data/sidekiq-cron.gemspec CHANGED
@@ -5,12 +5,12 @@ require './lib/sidekiq/cron/version'
5
5
  Gem::Specification.new do |s|
6
6
  s.name = "sidekiq-cron"
7
7
  s.version = Sidekiq::Cron::VERSION
8
-
9
- s.required_ruby_version = ">= 2.6"
10
- s.require_paths = ["lib"]
8
+ s.summary = "Scheduler/Cron for Sidekiq jobs"
9
+ s.description = "Enables to set jobs to be run in specified time (using CRON notation or natural language)"
10
+ s.homepage = "https://github.com/ondrejbartas/sidekiq-cron"
11
11
  s.authors = ["Ondrej Bartas"]
12
- s.description = "Enables to set jobs to be run in specified time (using CRON notation)"
13
12
  s.email = "ondrej@bartas.cz"
13
+ s.licenses = ["MIT"]
14
14
  s.extra_rdoc_files = [
15
15
  "LICENSE.txt",
16
16
  "README.md"
@@ -24,9 +24,7 @@ Gem::Specification.new do |s|
24
24
  "sidekiq-cron.gemspec",
25
25
  ]
26
26
 
27
- s.homepage = "https://github.com/ondrejbartas/sidekiq-cron"
28
- s.licenses = ["MIT"]
29
- s.summary = "Sidekiq-Cron helps to add repeated scheduled jobs"
27
+ s.required_ruby_version = ">= 2.6"
30
28
 
31
29
  s.add_dependency("fugit", "~> 1")
32
30
  s.add_dependency("sidekiq", ">= 4.2.1")
@@ -7,7 +7,7 @@ describe 'Performance Poller' do
7
7
  REDIS.with { |c| c.respond_to?(:redis) ? c.redis.flushdb : c.flushdb }
8
8
  Sidekiq.redis = REDIS
9
9
 
10
- #clear all previous saved data from redis
10
+ # Clear all previous saved data from Redis.
11
11
  Sidekiq.redis do |conn|
12
12
  conn.keys("cron_job*").each do |key|
13
13
  conn.del(key)
data/test/test_helper.rb CHANGED
@@ -13,6 +13,7 @@ require 'mocha/minitest'
13
13
  require 'sidekiq'
14
14
  require "sidekiq-pro" if ENV['SIDEKIQ_PRO_VERSION']
15
15
  require 'sidekiq/web'
16
+ require "sidekiq/cli"
16
17
 
17
18
  Sidekiq.logger.level = Logger::ERROR
18
19
 
@@ -0,0 +1,13 @@
1
+ ---
2
+ -
3
+ name: "my_first_job"
4
+ cron: "*/5 * * * *"
5
+ class: "HardWorker"
6
+ queue: "hard_worker"
7
+ -
8
+ name: "second_job"
9
+ cron: "*/30 * * * *"
10
+ class: "HardWorker"
11
+ queue: "hard_worker_long"
12
+ args:
13
+ hard: "stuff"
@@ -0,0 +1,12 @@
1
+ ---
2
+ my_first_job:
3
+ cron: "*/5 * * * *"
4
+ class: "HardWorker"
5
+ queue: hard_worker
6
+
7
+ second_job:
8
+ cron: "*/30 * * * *"
9
+ class: "HardWorker"
10
+ queue: hard_worker_long
11
+ args:
12
+ hard: "stuff"
@@ -0,0 +1 @@
1
+ --- string
@@ -2,14 +2,14 @@ require './test/test_helper'
2
2
 
3
3
  describe "Cron Job" do
4
4
  before do
5
- #clear all previous saved data from redis
5
+ # Clear all previous saved data from Redis.
6
6
  Sidekiq.redis do |conn|
7
7
  conn.keys("cron_job*").each do |key|
8
8
  conn.del(key)
9
9
  end
10
10
  end
11
11
 
12
- #clear all queues
12
+ # Clear all queues.
13
13
  Sidekiq::Queue.all.each do |queue|
14
14
  queue.clear
15
15
  end
@@ -75,7 +75,6 @@ describe "Cron Job" do
75
75
  end
76
76
 
77
77
  describe "invalid job" do
78
-
79
78
  before do
80
79
  @job = Sidekiq::Cron::Job.new()
81
80
  end
@@ -130,6 +129,22 @@ describe "Cron Job" do
130
129
  end
131
130
  end
132
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
+
133
148
  describe 'parse_enqueue_time' do
134
149
  before do
135
150
  @args = {
@@ -232,8 +247,11 @@ describe "Cron Job" do
232
247
  "queue"=>:super,
233
248
  "backtrace"=>true,
234
249
  "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)
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)
237
255
  end
238
256
 
239
257
  it "be initialized with 'class', 2 arguments and date_as_argument" do
@@ -245,11 +263,13 @@ describe "Cron Job" do
245
263
  "queue"=>:super,
246
264
  "backtrace"=>true,
247
265
  "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
266
+ assert_equal job_args, ["arg1", :arg2]
252
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
253
273
  end
254
274
 
255
275
  describe "cron test" do
@@ -303,6 +323,21 @@ describe "Cron Job" do
303
323
  }
304
324
  assert_equal @job.sidekiq_worker_message, payload
305
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
306
341
  end
307
342
 
308
343
  describe '#sidekiq_worker_message settings overwrite queue name' do
@@ -360,6 +395,22 @@ describe "Cron Job" do
360
395
  }
361
396
  assert_equal @job.active_job_message, payload
362
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
363
414
  end
364
415
 
365
416
  describe '#active_job_message - unknown Active Job Worker class' do
@@ -513,6 +564,23 @@ describe "Cron Job" do
513
564
  .returns(ActiveJobCronTestClass.new)
514
565
  @job.enque!
515
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
516
584
  end
517
585
 
518
586
  describe 'active job with queue_name_prefix' do
@@ -640,6 +708,24 @@ describe "Cron Job" do
640
708
  .returns(true)
641
709
  @job.enque!
642
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
643
729
  end
644
730
 
645
731
  describe 'sidekiq worker unknown class' do
@@ -750,7 +836,7 @@ describe "Cron Job" do
750
836
  end
751
837
 
752
838
  it "last_enqueue_time shouldn't be rewritten after save" do
753
- #adding last_enqueue_time to initialize is only for test purpose
839
+ # Adding last_enqueue_time to initialize is only for testing purposes.
754
840
  last_enqueue_time = '2013-01-01 23:59:59 +0000'
755
841
  expected_enqueue_time = DateTime.parse(last_enqueue_time).to_time.utc
756
842
  Sidekiq::Cron::Job.create(@args.merge('last_enqueue_time' => last_enqueue_time))
@@ -776,6 +862,7 @@ describe "Cron Job" do
776
862
  assert_equal job.name, "Test"
777
863
  end
778
864
  end
865
+
779
866
  it "from String" do
780
867
  args = {
781
868
  name: "Test",
@@ -788,6 +875,7 @@ describe "Cron Job" do
788
875
  assert_equal job.name, "Test"
789
876
  end
790
877
  end
878
+
791
879
  it "from Array" do
792
880
  args = {
793
881
  name: "Test",
@@ -859,7 +947,6 @@ describe "Cron Job" do
859
947
  Sidekiq::Cron::Job.create(@args)
860
948
  assert Sidekiq::Cron::Job.find('name' => "Test"), "String keys"
861
949
  end
862
-
863
950
  end
864
951
 
865
952
  describe "destroy" do
@@ -896,7 +983,6 @@ describe "Cron Job" do
896
983
  Sidekiq::Cron::Job.create(@args)
897
984
  assert Sidekiq::Cron::Job.destroy('name' => "Test"), "String keys"
898
985
  end
899
-
900
986
  end
901
987
 
902
988
  describe "destroy_removed_jobs" do
@@ -928,10 +1014,10 @@ describe "Cron Job" do
928
1014
  cron: "* * * * *",
929
1015
  klass: "CronTestClass"
930
1016
  }
931
- #first time is always
932
- #after next cron time!
1017
+ # First time is always after next cron time!
933
1018
  @time = Time.now.utc + 120
934
1019
  end
1020
+
935
1021
  it "be always false when status is disabled" do
936
1022
  refute Sidekiq::Cron::Job.new(@args.merge(status: 'disabled')).should_enque? @time
937
1023
  refute Sidekiq::Cron::Job.new(@args.merge(status: 'disabled')).should_enque? @time - 60
@@ -953,7 +1039,6 @@ describe "Cron Job" do
953
1039
  assert Sidekiq::Cron::Job.new(@args).should_enque? @time + 235
954
1040
  refute Sidekiq::Cron::Job.new(@args).should_enque? @time + 235
955
1041
 
956
- #just for check
957
1042
  refute Sidekiq::Cron::Job.new(@args).should_enque? @time
958
1043
  refute Sidekiq::Cron::Job.new(@args).should_enque? @time + 135
959
1044
  refute Sidekiq::Cron::Job.new(@args).should_enque? @time + 235
@@ -969,7 +1054,7 @@ describe "Cron Job" do
969
1054
  assert job.test_and_enque_for_time!(@time), "should enqueue"
970
1055
 
971
1056
  future_now = @time + 1 * 60 * 60
972
- Time.stubs(:now).returns(future_now) # save uses Time.now.utc
1057
+ Time.stubs(:now).returns(future_now) # Save uses Time.now.utc
973
1058
  job.save
974
1059
  assert Sidekiq::Cron::Job.new(@args).test_and_enque_for_time!(future_now + 30), "should enqueue"
975
1060
  end
@@ -986,7 +1071,7 @@ describe "Cron Job" do
986
1071
  end
987
1072
  assert_equal Sidekiq::Queue.all.first.size, 1, "Sidekiq queue 1 job in queue"
988
1073
 
989
- # 20 hours after
1074
+ # 20 hours after.
990
1075
  assert Sidekiq::Cron::Job.new(@args).test_and_enque_for_time! @time + 1 * 60 * 60
991
1076
  refute Sidekiq::Cron::Job.new(@args).test_and_enque_for_time! @time + 1 * 60 * 60
992
1077
 
@@ -995,7 +1080,7 @@ describe "Cron Job" do
995
1080
  end
996
1081
  assert_equal Sidekiq::Queue.all.first.size, 2, "Sidekiq queue 2 jobs in queue"
997
1082
 
998
- # 26 hour after
1083
+ # 26 hour after.
999
1084
  assert Sidekiq::Cron::Job.new(@args).test_and_enque_for_time! @time + 26 * 60 * 60
1000
1085
  refute Sidekiq::Cron::Job.new(@args).test_and_enque_for_time! @time + 26 * 60 * 60
1001
1086
 
@@ -1007,7 +1092,6 @@ describe "Cron Job" do
1007
1092
  end
1008
1093
 
1009
1094
  describe "load" do
1010
-
1011
1095
  describe "from hash" do
1012
1096
  before do
1013
1097
  @jobs_hash = {
@@ -1042,7 +1126,7 @@ describe "Cron Job" do
1042
1126
 
1043
1127
  it "return errors on loaded jobs" do
1044
1128
  assert_equal Sidekiq::Cron::Job.all.size, 0, "Should have 0 jobs before load"
1045
- #set something bag to hash
1129
+ # Set something bad to hash.
1046
1130
  @jobs_hash['name_of_job']['cron'] = "bad cron"
1047
1131
  out = Sidekiq::Cron::Job.load_from_hash @jobs_hash
1048
1132
  assert_equal 1, out.size, "should have 1 error"
@@ -1092,6 +1176,16 @@ describe "Cron Job" do
1092
1176
  assert_equal Sidekiq::Cron::Job.all.size, 2, "Should have 2 jobs after load"
1093
1177
  end
1094
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
+
1095
1189
  it "create new jobs and update old one with same settings with load_from_array" do
1096
1190
  assert_equal Sidekiq::Cron::Job.all.size, 0, "Should have 0 jobs before load"
1097
1191
  out = Sidekiq::Cron::Job.load_from_array! @jobs_array
@@ -5,7 +5,7 @@ describe 'Cron Poller' do
5
5
  REDIS.with { |c| c.respond_to?(:redis) ? c.redis.flushdb : c.flushdb }
6
6
  Sidekiq.redis = REDIS
7
7
 
8
- #clear all previous saved data from redis
8
+ # Clear all previous saved data from Redis.
9
9
  Sidekiq.redis do |conn|
10
10
  conn.keys("cron_job*").each do |key|
11
11
  conn.del(key)
@@ -27,7 +27,7 @@ describe 'Cron Poller' do
27
27
  now = Time.now.utc + 3600
28
28
  enqueued_time = Time.new(now.year, now.month, now.day, now.hour, 5, 1)
29
29
  Time.stubs(:now).returns(enqueued_time)
30
- #new jobs!
30
+
31
31
  Sidekiq::Cron::Job.create(@args)
32
32
  Sidekiq::Cron::Job.create(@args2)
33
33
 
@@ -38,7 +38,7 @@ describe 'Cron Poller' do
38
38
  assert_equal 0, conn.llen("queue:super")
39
39
  end
40
40
 
41
- #30 seconds after!
41
+ # 30 seconds after!
42
42
  enqueued_time = Time.new(now.year, now.month, now.day, now.hour, 5, 30)
43
43
  Time.stubs(:now).returns(enqueued_time)
44
44
 
@@ -54,7 +54,7 @@ describe 'Cron Poller' do
54
54
  now = Time.now.utc + 3600
55
55
  enqueued_time = Time.new(now.year, now.month, now.day, now.hour, 5, 1)
56
56
  Time.stubs(:now).returns(enqueued_time)
57
- #new jobs!
57
+
58
58
  Sidekiq::Cron::Job.create(@args)
59
59
  Sidekiq::Cron::Job.create(@args2)
60
60
 
@@ -79,7 +79,7 @@ describe 'Cron Poller' do
79
79
  now = Time.now.utc + 3600
80
80
  enqueued_time = Time.new(now.year, now.month, now.day, now.hour, 8, 1)
81
81
  Time.stubs(:now).returns(enqueued_time)
82
- #new jobs!
82
+
83
83
  Sidekiq::Cron::Job.create(@args)
84
84
  Sidekiq::Cron::Job.create(@args2)
85
85
 
@@ -104,7 +104,7 @@ describe 'Cron Poller' do
104
104
  now = Time.now.utc + 3600
105
105
  enqueued_time = Time.new(now.year, now.month, now.day, now.hour, 8, 1)
106
106
  Time.stubs(:now).returns(enqueued_time)
107
- #new jobs!
107
+
108
108
  Sidekiq::Cron::Job.create(@args)
109
109
  Sidekiq::Cron::Job.create(@args2)
110
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
@@ -125,14 +125,14 @@ describe 'Cron web' do
125
125
  assert_equal 1, conn.llen("queue:default"), "Queue should have 1 job"
126
126
  end
127
127
 
128
- #should enqueue more times
128
+ # Should enqueue more times.
129
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
- #should enqueue to cron job queue
135
+ # Should enqueue to cron job queue.
136
136
  post "/cron/#{cron_job_name}/enque"
137
137
 
138
138
  Sidekiq.redis do |conn|