From f5d65b6cc741e7d29398917a2cf0cd78a1ae6b8d Mon Sep 17 00:00:00 2001 From: Dzmitry Plashchynski Date: Tue, 29 Nov 2016 15:41:39 +0200 Subject: [PATCH] Job options --- lib/crono/job.rb | 5 +++-- lib/crono/performer_proxy.rb | 7 ++++++- spec/performer_proxy_spec.rb | 8 +++++++- 3 files changed, 16 insertions(+), 4 deletions(-) diff --git a/lib/crono/job.rb b/lib/crono/job.rb index 1535351..cd9aa2d 100644 --- a/lib/crono/job.rb +++ b/lib/crono/job.rb @@ -6,15 +6,16 @@ module Crono class Job include Logging - attr_accessor :performer, :period, :job_args, :last_performed_at, + 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) + def initialize(performer, period, job_args, job_options = {}) self.execution_interval = 0.minutes self.performer, self.period = performer, period self.job_args = JSON.generate(job_args) self.job_log = StringIO.new self.job_logger = Logger.new(job_log) + self.job_options = job_options self.next_performed_at = period.next @semaphore = Mutex.new end diff --git a/lib/crono/performer_proxy.rb b/lib/crono/performer_proxy.rb index 19c9896..3c1d75e 100644 --- a/lib/crono/performer_proxy.rb +++ b/lib/crono/performer_proxy.rb @@ -8,7 +8,7 @@ module Crono end def every(period, *args) - @job = Job.new(@performer, Period.new(period, *args), @job_args) + @job = Job.new(@performer, Period.new(period, *args), @job_args, @options) @scheduler.add_job(@job) self end @@ -17,6 +17,11 @@ module Crono @job.execution_interval = execution_interval if @job self end + + def with_options(options) + @options = options + self + end end def self.perform(performer, *job_args) diff --git a/spec/performer_proxy_spec.rb b/spec/performer_proxy_spec.rb index e0e1aa5..ba054d4 100644 --- a/spec/performer_proxy_spec.rb +++ b/spec/performer_proxy_spec.rb @@ -20,8 +20,14 @@ 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'}]) + expect(Crono::Job).to receive(:new).with(TestJob, kind_of(Crono::Period), [:some, {some: 'data'}], nil) allow(Crono.scheduler).to receive(:add_job) Crono.perform(TestJob, :some, {some: 'data'}).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}) + allow(Crono.scheduler).to receive(:add_job) + Crono.perform(TestJob).with_options(some_option: true).every(2.days, at: '15:30') + end end