mirror of
https://github.com/plashchynski/crono.git
synced 2026-03-12 05:22:07 +01:00
Merge pull request #36 from preisanalytics/jhuebl_add_data_to_scheduled_job_20160113
Add the possibility to schedule jobs with arguments
This commit is contained in:
11
README.md
11
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:
|
||||
|
||||
@@ -6,12 +6,13 @@ module Crono
|
||||
class Job
|
||||
include Logging
|
||||
|
||||
attr_accessor :performer, :period, :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)
|
||||
def initialize(performer, period, job_args)
|
||||
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.next_performed_at = period.next
|
||||
@@ -64,11 +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)
|
||||
healthy: healthy, args: job_args)
|
||||
end
|
||||
|
||||
def perform_job
|
||||
performer.new.perform
|
||||
performer.new.perform *JSON.parse(job_args)
|
||||
rescue StandardError => e
|
||||
handle_job_fail(e)
|
||||
else
|
||||
|
||||
@@ -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, job_args)
|
||||
@performer = performer
|
||||
@scheduler = scheduler
|
||||
@job_args = job_args
|
||||
end
|
||||
|
||||
def every(period, *args)
|
||||
@job = Job.new(@performer, Period.new(period, *args))
|
||||
@job = Job.new(@performer, Period.new(period, *args), @job_args)
|
||||
@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, *job_args)
|
||||
PerformerProxy.new(performer, Crono.scheduler, job_args)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -5,6 +5,7 @@ class CreateCronoJobs < ActiveRecord::Migration
|
||||
t.text :log
|
||||
t.datetime :last_performed_at
|
||||
t.boolean :healthy
|
||||
t.text :args
|
||||
t.timestamps null: false
|
||||
end
|
||||
add_index :crono_jobs, [:job_id], unique: true
|
||||
|
||||
@@ -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(: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
|
||||
expect(job.period).to be period
|
||||
end
|
||||
|
||||
it 'should contain data as JSON String' do
|
||||
expect(job_with_args.job_args).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,20 @@ 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
|
||||
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
|
||||
|
||||
def test_preform_job_twice
|
||||
expect(job).to receive(:perform_job).twice
|
||||
job.perform.join
|
||||
@@ -80,10 +100,12 @@ describe Crono::Job do
|
||||
it 'should update saved job' do
|
||||
job.last_performed_at = Time.now
|
||||
job.healthy = true
|
||||
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.args).to eq '[{"some":"data"}]'
|
||||
end
|
||||
|
||||
it 'should save and truncate job log' do
|
||||
@@ -102,7 +124,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, job_args)
|
||||
@job.load
|
||||
expect(@job.last_performed_at.utc.to_s).to be_eql @saved_last_performed_at.utc.to_s
|
||||
end
|
||||
|
||||
@@ -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 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, {some: 'data'}).every(2.days, at: '15:30')
|
||||
end
|
||||
end
|
||||
|
||||
@@ -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'), [])
|
||||
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, []) }
|
||||
|
||||
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, []) }
|
||||
|
||||
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, []) }
|
||||
|
||||
_, next_jobs = scheduler.next_jobs
|
||||
expect(next_jobs).to be_eql [jobs[0]]
|
||||
|
||||
Reference in New Issue
Block a user