From dd4f92b5696bd926ac2a956bdce04ea5aaddc643 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jannis=20H=C3=BCbl?= Date: Wed, 13 Jan 2016 17:44:27 +0100 Subject: [PATCH 1/3] add data to Cron.perform which will be passed to ExampleJob.new(data).perform --- lib/crono/job.rb | 11 +++++--- lib/crono/performer_proxy.rb | 9 ++++--- .../templates/migrations/create_crono_jobs.rb | 1 + spec/job_spec.rb | 26 ++++++++++++++++--- spec/performer_proxy_spec.rb | 6 +++++ spec/scheduler_spec.rb | 8 +++--- 6 files changed, 46 insertions(+), 15 deletions(-) diff --git a/lib/crono/job.rb b/lib/crono/job.rb index 8a86b95..746648c 100644 --- a/lib/crono/job.rb +++ b/lib/crono/job.rb @@ -6,12 +6,13 @@ module Crono class Job include Logging - attr_accessor :performer, :period, :last_performed_at, + attr_accessor :performer, :period, :data, :last_performed_at, :next_performed_at, :job_log, :job_logger, :healthy, :execution_interval - def initialize(performer, period) + def initialize(performer, period, data) self.execution_interval = 0.minutes self.performer, self.period = performer, period + self.data = JSON.generate(data) if data self.job_log = StringIO.new self.job_logger = Logger.new(job_log) self.next_performed_at = period.next @@ -64,11 +65,13 @@ module Crono saved_log = model.reload.log || '' log_to_save = saved_log + job_log.string model.update(last_performed_at: last_performed_at, log: log_to_save, - healthy: healthy) + healthy: healthy, data: data) end def perform_job - performer.new.perform + args = [] + args << JSON.parse(data) if data + performer.new(*args).perform rescue StandardError => e handle_job_fail(e) else diff --git a/lib/crono/performer_proxy.rb b/lib/crono/performer_proxy.rb index e632e95..c03b272 100644 --- a/lib/crono/performer_proxy.rb +++ b/lib/crono/performer_proxy.rb @@ -1,13 +1,14 @@ module Crono # Crono::PerformerProxy is a proxy used in cronotab.rb semantic class PerformerProxy - def initialize(performer, scheduler) + def initialize(performer, scheduler, data) @performer = performer @scheduler = scheduler + @data = data end def every(period, *args) - @job = Job.new(@performer, Period.new(period, *args)) + @job = Job.new(@performer, Period.new(period, *args), @data) @scheduler.add_job(@job) self end @@ -18,7 +19,7 @@ module Crono end end - def self.perform(performer) - PerformerProxy.new(performer, Crono.scheduler) + def self.perform(performer, data=nil) + PerformerProxy.new(performer, Crono.scheduler, data) end end diff --git a/lib/generators/crono/install/templates/migrations/create_crono_jobs.rb b/lib/generators/crono/install/templates/migrations/create_crono_jobs.rb index b7d9543..2641cd5 100644 --- a/lib/generators/crono/install/templates/migrations/create_crono_jobs.rb +++ b/lib/generators/crono/install/templates/migrations/create_crono_jobs.rb @@ -5,6 +5,7 @@ class CreateCronoJobs < ActiveRecord::Migration t.text :log t.datetime :last_performed_at t.boolean :healthy + t.text :data t.timestamps null: false end add_index :crono_jobs, [:job_id], unique: true diff --git a/spec/job_spec.rb b/spec/job_spec.rb index 1488a3b..e1efc8a 100644 --- a/spec/job_spec.rb +++ b/spec/job_spec.rb @@ -2,14 +2,20 @@ require 'spec_helper' describe Crono::Job do let(:period) { Crono::Period.new(2.day, at: '15:00') } - let(:job) { Crono::Job.new(TestJob, period) } - let(:failing_job) { Crono::Job.new(TestFailingJob, period) } + let(:data) {{some: 'data'}} + let(:job) { Crono::Job.new(TestJob, period, nil) } + let(:job_with_data) { Crono::Job.new(TestJob, period, data) } + let(:failing_job) { Crono::Job.new(TestFailingJob, period, nil) } it 'should contain performer and period' do expect(job.performer).to be TestJob expect(job.period).to be period end + it 'should contain data as JSON String' do + expect(job_with_data.data).to eq '{"some":"data"}' + end + describe '#next' do it 'should return next performing time according to period' do expect(job.next).to be_eql period.next @@ -56,6 +62,18 @@ describe Crono::Job do test_preform_job_twice end + it 'should call perform of performer' do + expect(TestJob).to receive(:new).with(no_args) + thread = job.perform.join + expect(thread).to be_stop + end + + it 'should call perform of performer with data' do + expect(TestJob).to receive(:new).with({"some" => "data"}) + thread = job_with_data.perform.join + expect(thread).to be_stop + end + def test_preform_job_twice expect(job).to receive(:perform_job).twice job.perform.join @@ -80,10 +98,12 @@ describe Crono::Job do it 'should update saved job' do job.last_performed_at = Time.now job.healthy = true + job.data = JSON.generate({some: 'data'}) job.save @crono_job = Crono::CronoJob.find_by(job_id: job.job_id) expect(@crono_job.last_performed_at.utc.to_s).to be_eql job.last_performed_at.utc.to_s expect(@crono_job.healthy).to be true + expect(@crono_job.data).to eq '{"some":"data"}' end it 'should save and truncate job log' do @@ -102,7 +122,7 @@ describe Crono::Job do end it 'should load last_performed_at from DB' do - @job = Crono::Job.new(TestJob, period) + @job = Crono::Job.new(TestJob, period, data) @job.load expect(@job.last_performed_at.utc.to_s).to be_eql @saved_last_performed_at.utc.to_s end diff --git a/spec/performer_proxy_spec.rb b/spec/performer_proxy_spec.rb index 02f39e7..c2f0cfc 100644 --- a/spec/performer_proxy_spec.rb +++ b/spec/performer_proxy_spec.rb @@ -18,4 +18,10 @@ describe Crono::PerformerProxy do expect_any_instance_of(described_class).to receive(:once_per) Crono.perform(TestJob).once_per 10.minutes end + + it 'should add job with data to schedule' do + expect(Crono::Job).to receive(:new).with(TestJob, kind_of(Crono::Period), {some: 'data'}) + allow(Crono.scheduler).to receive(:add_job) + Crono.perform(TestJob, {some: 'data'}).every(2.days, at: '15:30') + end end diff --git a/spec/scheduler_spec.rb b/spec/scheduler_spec.rb index bef9e88..8597aa0 100644 --- a/spec/scheduler_spec.rb +++ b/spec/scheduler_spec.rb @@ -5,7 +5,7 @@ describe Crono::Scheduler do describe '#add_job' do it 'should call Job#load on Job' do - @job = Crono::Job.new(TestJob, Crono::Period.new(10.day, at: '04:05')) + @job = Crono::Job.new(TestJob, Crono::Period.new(10.day, at: '04:05'), nil) expect(@job).to receive(:load) scheduler.add_job(@job) end @@ -17,7 +17,7 @@ describe Crono::Scheduler do Crono::Period.new(3.days, at: 10.minutes.from_now.strftime('%H:%M')), Crono::Period.new(1.day, at: 20.minutes.from_now.strftime('%H:%M')), Crono::Period.new(7.days, at: 40.minutes.from_now.strftime('%H:%M')) - ].map { |period| Crono::Job.new(TestJob, period) } + ].map { |period| Crono::Job.new(TestJob, period, nil) } time, jobs = scheduler.next_jobs expect(jobs).to be_eql [jobs[0]] @@ -29,7 +29,7 @@ describe Crono::Scheduler do Crono::Period.new(1.day, at: time.strftime('%H:%M')), Crono::Period.new(1.day, at: time.strftime('%H:%M')), Crono::Period.new(1.day, at: 10.minutes.from_now.strftime('%H:%M')) - ].map { |period| Crono::Job.new(TestJob, period) } + ].map { |period| Crono::Job.new(TestJob, period, nil) } time, jobs = scheduler.next_jobs expect(jobs).to be_eql [jobs[0], jobs[1]] @@ -40,7 +40,7 @@ describe Crono::Scheduler do Crono::Period.new(10.seconds), Crono::Period.new(10.seconds), Crono::Period.new(1.day, at: 10.minutes.from_now.strftime('%H:%M')) - ].map { |period| Crono::Job.new(TestJob, period) } + ].map { |period| Crono::Job.new(TestJob, period, nil) } _, next_jobs = scheduler.next_jobs expect(next_jobs).to be_eql [jobs[0]] From 89f3b9a8a1096b68bc84e32bb933ef0ef4ec6bff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jannis=20H=C3=BCbl?= Date: Thu, 14 Jan 2016 09:33:14 +0100 Subject: [PATCH 2/3] change to args which will be passed to Job#perform --- lib/crono/job.rb | 12 +++++----- lib/crono/performer_proxy.rb | 10 ++++----- .../templates/migrations/create_crono_jobs.rb | 2 +- spec/job_spec.rb | 22 ++++++++++--------- spec/performer_proxy_spec.rb | 6 ++--- spec/scheduler_spec.rb | 8 +++---- 6 files changed, 30 insertions(+), 30 deletions(-) diff --git a/lib/crono/job.rb b/lib/crono/job.rb index 746648c..909e045 100644 --- a/lib/crono/job.rb +++ b/lib/crono/job.rb @@ -6,13 +6,13 @@ module Crono class Job include Logging - attr_accessor :performer, :period, :data, :last_performed_at, + attr_accessor :performer, :period, :job_args, :last_performed_at, :next_performed_at, :job_log, :job_logger, :healthy, :execution_interval - def initialize(performer, period, data) + def initialize(performer, period, job_args) self.execution_interval = 0.minutes self.performer, self.period = performer, period - self.data = JSON.generate(data) if data + self.job_args = JSON.generate(job_args) self.job_log = StringIO.new self.job_logger = Logger.new(job_log) self.next_performed_at = period.next @@ -65,13 +65,11 @@ module Crono saved_log = model.reload.log || '' log_to_save = saved_log + job_log.string model.update(last_performed_at: last_performed_at, log: log_to_save, - healthy: healthy, data: data) + healthy: healthy, args: job_args) end def perform_job - args = [] - args << JSON.parse(data) if data - performer.new(*args).perform + performer.new.perform *JSON.parse(job_args) rescue StandardError => e handle_job_fail(e) else diff --git a/lib/crono/performer_proxy.rb b/lib/crono/performer_proxy.rb index c03b272..19c9896 100644 --- a/lib/crono/performer_proxy.rb +++ b/lib/crono/performer_proxy.rb @@ -1,14 +1,14 @@ module Crono # Crono::PerformerProxy is a proxy used in cronotab.rb semantic class PerformerProxy - def initialize(performer, scheduler, data) + def initialize(performer, scheduler, job_args) @performer = performer @scheduler = scheduler - @data = data + @job_args = job_args end def every(period, *args) - @job = Job.new(@performer, Period.new(period, *args), @data) + @job = Job.new(@performer, Period.new(period, *args), @job_args) @scheduler.add_job(@job) self end @@ -19,7 +19,7 @@ module Crono end end - def self.perform(performer, data=nil) - PerformerProxy.new(performer, Crono.scheduler, data) + def self.perform(performer, *job_args) + PerformerProxy.new(performer, Crono.scheduler, job_args) end end diff --git a/lib/generators/crono/install/templates/migrations/create_crono_jobs.rb b/lib/generators/crono/install/templates/migrations/create_crono_jobs.rb index 2641cd5..3c8c26e 100644 --- a/lib/generators/crono/install/templates/migrations/create_crono_jobs.rb +++ b/lib/generators/crono/install/templates/migrations/create_crono_jobs.rb @@ -5,7 +5,7 @@ class CreateCronoJobs < ActiveRecord::Migration t.text :log t.datetime :last_performed_at t.boolean :healthy - t.text :data + t.text :args t.timestamps null: false end add_index :crono_jobs, [:job_id], unique: true diff --git a/spec/job_spec.rb b/spec/job_spec.rb index e1efc8a..5e4c63b 100644 --- a/spec/job_spec.rb +++ b/spec/job_spec.rb @@ -2,10 +2,10 @@ require 'spec_helper' describe Crono::Job do let(:period) { Crono::Period.new(2.day, at: '15:00') } - let(:data) {{some: 'data'}} - let(:job) { Crono::Job.new(TestJob, period, nil) } - let(:job_with_data) { Crono::Job.new(TestJob, period, data) } - let(:failing_job) { Crono::Job.new(TestFailingJob, period, nil) } + 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, []) } it 'should contain performer and period' do expect(job.performer).to be TestJob @@ -13,7 +13,7 @@ describe Crono::Job do end it 'should contain data as JSON String' do - expect(job_with_data.data).to eq '{"some":"data"}' + expect(job_with_args.job_args).to eq '[{"some":"data"}]' end describe '#next' do @@ -69,8 +69,10 @@ describe Crono::Job do end it 'should call perform of performer with data' do - expect(TestJob).to receive(:new).with({"some" => "data"}) - thread = job_with_data.perform.join + test_job = double() + expect(TestJob).to receive(:new).and_return(test_job) + expect(test_job).to receive(:perform).with({'some' => 'data'}) + thread = job_with_args.perform.join expect(thread).to be_stop end @@ -98,12 +100,12 @@ describe Crono::Job do it 'should update saved job' do job.last_performed_at = Time.now job.healthy = true - job.data = JSON.generate({some: 'data'}) + job.job_args = JSON.generate([{some: 'data'}]) job.save @crono_job = Crono::CronoJob.find_by(job_id: job.job_id) expect(@crono_job.last_performed_at.utc.to_s).to be_eql job.last_performed_at.utc.to_s expect(@crono_job.healthy).to be true - expect(@crono_job.data).to eq '{"some":"data"}' + expect(@crono_job.args).to eq '[{"some":"data"}]' end it 'should save and truncate job log' do @@ -122,7 +124,7 @@ describe Crono::Job do end it 'should load last_performed_at from DB' do - @job = Crono::Job.new(TestJob, period, data) + @job = Crono::Job.new(TestJob, period, job_args) @job.load expect(@job.last_performed_at.utc.to_s).to be_eql @saved_last_performed_at.utc.to_s end diff --git a/spec/performer_proxy_spec.rb b/spec/performer_proxy_spec.rb index c2f0cfc..e0e1aa5 100644 --- a/spec/performer_proxy_spec.rb +++ b/spec/performer_proxy_spec.rb @@ -19,9 +19,9 @@ describe Crono::PerformerProxy do Crono.perform(TestJob).once_per 10.minutes end - it 'should add job with data to schedule' do - expect(Crono::Job).to receive(:new).with(TestJob, kind_of(Crono::Period), {some: 'data'}) + it 'should add job with args to schedule' do + 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: 'data'}).every(2.days, at: '15:30') + Crono.perform(TestJob, :some, {some: 'data'}).every(2.days, at: '15:30') end end diff --git a/spec/scheduler_spec.rb b/spec/scheduler_spec.rb index 8597aa0..2b8f1b9 100644 --- a/spec/scheduler_spec.rb +++ b/spec/scheduler_spec.rb @@ -5,7 +5,7 @@ describe Crono::Scheduler do describe '#add_job' do it 'should call Job#load on Job' do - @job = Crono::Job.new(TestJob, Crono::Period.new(10.day, at: '04:05'), nil) + @job = Crono::Job.new(TestJob, Crono::Period.new(10.day, at: '04:05'), []) expect(@job).to receive(:load) scheduler.add_job(@job) end @@ -17,7 +17,7 @@ describe Crono::Scheduler do Crono::Period.new(3.days, at: 10.minutes.from_now.strftime('%H:%M')), Crono::Period.new(1.day, at: 20.minutes.from_now.strftime('%H:%M')), Crono::Period.new(7.days, at: 40.minutes.from_now.strftime('%H:%M')) - ].map { |period| Crono::Job.new(TestJob, period, nil) } + ].map { |period| Crono::Job.new(TestJob, period, []) } time, jobs = scheduler.next_jobs expect(jobs).to be_eql [jobs[0]] @@ -29,7 +29,7 @@ describe Crono::Scheduler do Crono::Period.new(1.day, at: time.strftime('%H:%M')), Crono::Period.new(1.day, at: time.strftime('%H:%M')), Crono::Period.new(1.day, at: 10.minutes.from_now.strftime('%H:%M')) - ].map { |period| Crono::Job.new(TestJob, period, nil) } + ].map { |period| Crono::Job.new(TestJob, period, []) } time, jobs = scheduler.next_jobs expect(jobs).to be_eql [jobs[0], jobs[1]] @@ -40,7 +40,7 @@ describe Crono::Scheduler do Crono::Period.new(10.seconds), Crono::Period.new(10.seconds), Crono::Period.new(1.day, at: 10.minutes.from_now.strftime('%H:%M')) - ].map { |period| Crono::Job.new(TestJob, period, nil) } + ].map { |period| Crono::Job.new(TestJob, period, []) } _, next_jobs = scheduler.next_jobs expect(next_jobs).to be_eql [jobs[0]] From 042228900fcb617008126aaf17ef472e2375d4ee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jannis=20H=C3=BCbl?= Date: Thu, 14 Jan 2016 10:23:46 +0100 Subject: [PATCH 3/3] document how to schedule jobs with arguments --- README.md | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 180a8e5..53e261b 100644 --- a/README.md +++ b/README.md @@ -61,11 +61,11 @@ class TestJob < ActiveJob::Base end ``` -The ActiveJob jobs is convenient because you can use one job in both periodic and enqueued ways. But Active Job is not required. Any class can be used as a crono job if it implements a method `perform` without arguments: +The ActiveJob jobs is convenient because you can use one job in both periodic and enqueued ways. But Active Job is not required. Any class can be used as a crono job if it implements a method `perform`: ```ruby class TestJob # This is not an Active Job job, but pretty legal Crono job. - def perform + def perform(*args) # put you scheduled code here # Comments.deleted.clean_up... end @@ -124,6 +124,13 @@ The `at` can be a Hash: Crono.perform(TestJob).every 1.day, at: {hour: 12, min: 15} ``` +You can schedule a job with arguments, which can contain objects that can be +serialized using JSON.generate + +```ruby +Crono.perform(TestJob, 'some', 'args').every 1.day, at: {hour: 12, min: 15} +``` + #### Run daemon To run Crono daemon, in your Rails project root directory: