diff --git a/lib/crono/job.rb b/lib/crono/job.rb index e5afb7b..872f68e 100644 --- a/lib/crono/job.rb +++ b/lib/crono/job.rb @@ -9,7 +9,7 @@ module Crono attr_accessor :performer, :period, :job_args, :last_performed_at, :job_options, :next_performed_at, :job_log, :job_logger, :healthy, :execution_interval - def initialize(performer, period, job_args, job_options = nil) + def initialize(performer, period, job_args = nil, job_options = nil) self.execution_interval = 0.minutes self.performer, self.period = performer, period self.job_args = JSON.generate(job_args) if job_args.present? diff --git a/lib/crono/performer_proxy.rb b/lib/crono/performer_proxy.rb index 7bf2699..a1d8eb5 100644 --- a/lib/crono/performer_proxy.rb +++ b/lib/crono/performer_proxy.rb @@ -1,14 +1,15 @@ module Crono # Crono::PerformerProxy is a proxy used in cronotab.rb semantic class PerformerProxy - def initialize(performer, scheduler, job_args) + def initialize(performer, scheduler, job_args = nil, job_options = nil) @performer = performer @scheduler = scheduler @job_args = job_args + @job_options = job_options end def every(period, **options) - @job = Job.new(@performer, Period.new(period, **options), @job_args, @options) + @job = Job.new(@performer, Period.new(period, **options), @job_args, @job_options) @scheduler.add_job(@job) self end @@ -19,12 +20,12 @@ module Crono end def with_options(options) - @options = options + @job_options = options self end end def self.perform(performer, *job_args) - PerformerProxy.new(performer, Crono.scheduler, job_args) + PerformerProxy.new(performer, Crono.scheduler, *job_args) end end diff --git a/spec/job_spec.rb b/spec/job_spec.rb index 083f5d2..c80fcbe 100644 --- a/spec/job_spec.rb +++ b/spec/job_spec.rb @@ -12,12 +12,19 @@ class TestFailingJob end end +class TestNoArgsJob + def perform + puts 'Test!' + end +end + describe Crono::Job do let(:period) { Crono::Period.new(2.day, at: '15:00') } let(:job_args) {[{some: 'data'}]} let(:job) { Crono::Job.new(TestJob, period, []) } let(:job_with_args) { Crono::Job.new(TestJob, period, job_args) } let(:failing_job) { Crono::Job.new(TestFailingJob, period, []) } + let(:not_args_job) { Crono::Job.new(TestJob, period) } it 'should contain performer and period' do expect(job.performer).to be TestJob @@ -70,6 +77,10 @@ describe Crono::Job do test_preform_job_twice end + it 'should execute twice without args' do + test_preform_job_twice not_args_job + end + it 'should execute twice without initialize execution_interval' do test_preform_job_twice end @@ -88,10 +99,10 @@ describe Crono::Job do expect(thread).to be_stop end - def test_preform_job_twice - expect(job).to receive(:perform_job).twice - job.perform.join - thread = job.perform.join + def test_preform_job_twice(jon_instance = job) + expect(jon_instance).to receive(:perform_job).twice + jon_instance.perform.join + thread = jon_instance.perform.join expect(thread).to be_stop end end diff --git a/spec/performer_proxy_spec.rb b/spec/performer_proxy_spec.rb index 40eb90c..9668db7 100644 --- a/spec/performer_proxy_spec.rb +++ b/spec/performer_proxy_spec.rb @@ -26,13 +26,19 @@ describe Crono::PerformerProxy do end it 'should add job with args to schedule' do - expect(Crono::Job).to receive(:new).with(TestJob, kind_of(Crono::Period), [:some, {some: 'data'}], nil) + expect(Crono::Job).to receive(:new).with(TestJob, kind_of(Crono::Period), :some, { some: 'data' }) allow(Crono.scheduler).to receive(:add_job) - Crono.perform(TestJob, :some, {some: 'data'}).every(2.days, at: '15:30') + Crono.perform(TestJob, :some, { some: 'data' }).every(2.days, at: '15:30') + end + + it 'should add job without args to schedule' do + expect(Crono::Job).to receive(:new).with(TestJob, kind_of(Crono::Period), nil, nil) + allow(Crono.scheduler).to receive(:add_job) + Crono.perform(TestJob).every(2.days, at: '15:30') end it 'should add job with options to schedule' do - expect(Crono::Job).to receive(:new).with(TestJob, kind_of(Crono::Period), [], {some_option: true}) + expect(Crono::Job).to receive(:new).with(TestJob, kind_of(Crono::Period), nil, { some_option: true }) allow(Crono.scheduler).to receive(:add_job) Crono.perform(TestJob).with_options(some_option: true).every(2.days, at: '15:30') end