sidekiq-cron 1.6.0 → 1.8.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,38 +1,41 @@
1
- # require cron poller
2
1
  require 'sidekiq/cron/poller'
3
2
 
4
3
  # For Cron we need to add some methods to Launcher
5
4
  # so look at the code bellow.
6
5
  #
7
- # we are creating new cron poller instance and
8
- # adding start and stop commands to launcher
6
+ # We are creating new cron poller instance and
7
+ # adding start and stop commands to launcher.
9
8
  module Sidekiq
10
9
  module Cron
11
10
  module Launcher
12
- # Add cron poller to launcher
11
+ DEFAULT_POLL_INTERVAL = 30
12
+
13
+ # Add cron poller to launcher.
13
14
  attr_reader :cron_poller
14
15
 
15
- # add cron poller and execute normal initialize of Sidekiq launcher
16
+ # Add cron poller and execute normal initialize of Sidekiq launcher.
16
17
  def initialize(options)
17
- @cron_poller = Sidekiq::Cron::Poller.new
18
+ options[:cron_poll_interval] = DEFAULT_POLL_INTERVAL if options[:cron_poll_interval].nil?
19
+
20
+ @cron_poller = Sidekiq::Cron::Poller.new(options) if options[:cron_poll_interval] > 0
18
21
  super(options)
19
22
  end
20
23
 
21
- # execute normal run of launcher and run cron poller
24
+ # Execute normal run of launcher and run cron poller.
22
25
  def run
23
26
  super
24
- cron_poller.start
27
+ cron_poller.start if @cron_poller
25
28
  end
26
29
 
27
- # execute normal quiet of launcher and quiet cron poller
30
+ # Execute normal quiet of launcher and quiet cron poller.
28
31
  def quiet
29
- cron_poller.terminate
32
+ cron_poller.terminate if @cron_poller
30
33
  super
31
34
  end
32
35
 
33
- # execute normal stop of launcher and stop cron poller
36
+ # Execute normal stop of launcher and stop cron poller.
34
37
  def stop
35
- cron_poller.terminate
38
+ cron_poller.terminate if @cron_poller
36
39
  super
37
40
  end
38
41
  end
@@ -40,7 +43,6 @@ module Sidekiq
40
43
  end
41
44
 
42
45
  Sidekiq.configure_server do
43
- # require Sidekiq original launcher
44
46
  require 'sidekiq/launcher'
45
47
 
46
48
  ::Sidekiq::Launcher.prepend(Sidekiq::Cron::Launcher)
@@ -0,0 +1,23 @@
1
+ it:
2
+ Job: Job
3
+ Cron: Cron
4
+ CronJobs: Cron job
5
+ EnqueueNow: Accoda
6
+ EnableAll: Attiva tutto
7
+ DisableAll: Disattiva tutto
8
+ EnqueueAll: Accoda tutto
9
+ DeleteAll: Cancella tutto
10
+ "Cron string": Cron
11
+ AreYouSureEnqueueCronJobs: Vuoi accodare TUTTI i cron job?
12
+ AreYouSureEnqueueCronJob: "Vuoi accodare il cron job '%{job}'?"
13
+ AreYouSureDeleteCronJobs: Vuoi cancellare TUTTI i cron job?
14
+ AreYouSureDeleteCronJob: "Vuoi cancellare il cron job '%{job}'?"
15
+ NoCronJobsWereFound: Nessun cron job trovato
16
+ Enable: Attiva
17
+ Disable: Disattiva
18
+ "Last enqueued": Ultimo accodamento
19
+ disabled: disattivato
20
+ enabled: attivato
21
+ NoHistoryWereFound: Nessun evento in cronologia
22
+ Description: Descrizione
23
+ Message: Payload
@@ -1,27 +1,19 @@
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
-
9
- # The Poller checks Redis every N seconds for sheduled cron jobs
8
+ # The Poller checks Redis every N seconds for sheduled cron jobs.
10
9
  class Poller < Sidekiq::Scheduled::Poller
11
- 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
18
- end
19
-
20
- if Gem::Version.new(Sidekiq::VERSION) >= Gem::Version.new("6.5.0")
21
- # Sidekiq Poller init requires a config argument
22
- super(Sidekiq)
23
- else
10
+ def initialize(options = {})
11
+ if Gem::Version.new(Sidekiq::VERSION) >= Gem::Version.new('6.5.0')
24
12
  super
13
+ else
14
+ # Old version of Sidekiq does not accept a config argument.
15
+ @config = options
16
+ super()
25
17
  end
26
18
  end
27
19
 
@@ -32,7 +24,7 @@ module Sidekiq
32
24
  end
33
25
  rescue => ex
34
26
  # Most likely a problem with redis networking.
35
- # Punt and try again at the next interval
27
+ # Punt and try again at the next interval.
36
28
  Sidekiq.logger.error ex.message
37
29
  Sidekiq.logger.error ex.backtrace.first
38
30
  handle_exception(ex) if respond_to?(:handle_exception)
@@ -43,11 +35,15 @@ module Sidekiq
43
35
  def enqueue_job(job, time = Time.now.utc)
44
36
  job.test_and_enque_for_time! time if job && job.valid?
45
37
  rescue => ex
46
- # problem somewhere in one job
38
+ # Problem somewhere in one job.
47
39
  Sidekiq.logger.error "CRON JOB: #{ex.message}"
48
40
  Sidekiq.logger.error "CRON JOB: #{ex.backtrace.first}"
49
41
  handle_exception(ex) if respond_to?(:handle_exception)
50
42
  end
43
+
44
+ def poll_interval_average(process_count = 1)
45
+ @config[:cron_poll_interval]
46
+ end
51
47
  end
52
48
  end
53
49
  end
@@ -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.8.0"
6
6
  end
7
7
  end
@@ -42,7 +42,7 @@
42
42
  <td style="<%= style %>"><%= t job.status %></td>
43
43
  <td style="<%= style %>">
44
44
  <a href="<%= root_path %>cron/<%= CGI.escape(job.name).gsub('+', '%20') %>">
45
- <b><%= job.name %></b>
45
+ <b style="<%= style %>"><%= job.name %></b>
46
46
  </a>
47
47
  <hr style="margin:3px;border:0;">
48
48
  <small>
@@ -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",
@@ -841,7 +929,7 @@ describe "Cron Job" do
841
929
  Sidekiq::Cron::Job.create(@args.merge(name: "Test3"))
842
930
 
843
931
  Sidekiq.redis do |conn|
844
- conn.sadd Sidekiq::Cron::Job.jobs_key, "some_other_key"
932
+ conn.sadd Sidekiq::Cron::Job.jobs_key, ["some_other_key"]
845
933
  end
846
934
 
847
935
  assert_equal Sidekiq::Cron::Job.all.size, 3, "All have to return only valid 3 jobs"
@@ -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