mirror of
https://github.com/plashchynski/crono.git
synced 2026-05-13 03:10:42 +02:00
Compare commits
5 Commits
v0.8.1
...
next_jobs_
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b4ad8fb953 | ||
|
|
d075a55f03 | ||
|
|
2d72020ac4 | ||
|
|
cde8a2d214 | ||
|
|
2ec9cfa829 |
18
Gemfile.lock
18
Gemfile.lock
@@ -1,7 +1,7 @@
|
|||||||
PATH
|
PATH
|
||||||
remote: .
|
remote: .
|
||||||
specs:
|
specs:
|
||||||
crono (0.8.0)
|
crono (0.8.1)
|
||||||
activejob (~> 4.0)
|
activejob (~> 4.0)
|
||||||
activerecord (~> 4.0)
|
activerecord (~> 4.0)
|
||||||
activesupport (~> 4.0)
|
activesupport (~> 4.0)
|
||||||
@@ -9,17 +9,17 @@ PATH
|
|||||||
GEM
|
GEM
|
||||||
remote: https://rubygems.org/
|
remote: https://rubygems.org/
|
||||||
specs:
|
specs:
|
||||||
activejob (4.2.0)
|
activejob (4.2.1)
|
||||||
activesupport (= 4.2.0)
|
activesupport (= 4.2.1)
|
||||||
globalid (>= 0.3.0)
|
globalid (>= 0.3.0)
|
||||||
activemodel (4.2.0)
|
activemodel (4.2.1)
|
||||||
activesupport (= 4.2.0)
|
activesupport (= 4.2.1)
|
||||||
builder (~> 3.1)
|
builder (~> 3.1)
|
||||||
activerecord (4.2.0)
|
activerecord (4.2.1)
|
||||||
activemodel (= 4.2.0)
|
activemodel (= 4.2.1)
|
||||||
activesupport (= 4.2.0)
|
activesupport (= 4.2.1)
|
||||||
arel (~> 6.0)
|
arel (~> 6.0)
|
||||||
activesupport (4.2.0)
|
activesupport (4.2.1)
|
||||||
i18n (~> 0.7)
|
i18n (~> 0.7)
|
||||||
json (~> 1.7, >= 1.7.7)
|
json (~> 1.7, >= 1.7.7)
|
||||||
minitest (~> 5.1)
|
minitest (~> 5.1)
|
||||||
|
|||||||
@@ -1,8 +1,6 @@
|
|||||||
Crono — Job scheduler for Rails
|
Crono — Job scheduler for Rails
|
||||||
------------------------
|
------------------------
|
||||||
[](http://badge.fury.io/rb/crono)Here's an example of a test job:
|
[](http://badge.fury.io/rb/crono)
|
||||||
|
|
||||||
|
|
||||||
[](https://travis-ci.org/plashchynski/crono)
|
[](https://travis-ci.org/plashchynski/crono)
|
||||||
[](https://codeclimate.com/github/plashchynski/crono)
|
[](https://codeclimate.com/github/plashchynski/crono)
|
||||||
[](https://hakiri.io/github/plashchynski/crono/master)
|
[](https://hakiri.io/github/plashchynski/crono/master)
|
||||||
|
|||||||
@@ -79,9 +79,10 @@ module Crono
|
|||||||
end
|
end
|
||||||
|
|
||||||
def start_working_loop
|
def start_working_loop
|
||||||
while (job = Crono.scheduler.next)
|
while true
|
||||||
sleep(job.next - Time.now)
|
next_time, jobs = Crono.scheduler.next_jobs
|
||||||
job.perform
|
sleep(next_time - Time.now)
|
||||||
|
jobs.each(&:perform)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -17,8 +17,7 @@ module Crono
|
|||||||
end
|
end
|
||||||
|
|
||||||
def next
|
def next
|
||||||
next_time = period.next(since: last_performed_at)
|
period.next(since: last_performed_at)
|
||||||
next_time.past? ? period.next : next_time
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def description
|
def description
|
||||||
|
|||||||
@@ -12,14 +12,8 @@ module Crono
|
|||||||
jobs << job
|
jobs << job
|
||||||
end
|
end
|
||||||
|
|
||||||
def next
|
def next_jobs
|
||||||
queue.first
|
jobs.group_by(&:next).sort_by {|time,_| time }.first
|
||||||
end
|
|
||||||
|
|
||||||
private
|
|
||||||
|
|
||||||
def queue
|
|
||||||
jobs.sort_by(&:next)
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
require 'spec_helper'
|
require 'spec_helper'
|
||||||
|
|
||||||
describe Crono::Job do
|
describe Crono::Job do
|
||||||
let(:period) { Crono::Period.new(2.day) }
|
let(:period) { Crono::Period.new(2.day, at: '15:00') }
|
||||||
let(:job) { Crono::Job.new(TestJob, period) }
|
let(:job) { Crono::Job.new(TestJob, period) }
|
||||||
let(:failing_job) { Crono::Job.new(TestFailingJob, period) }
|
let(:failing_job) { Crono::Job.new(TestFailingJob, period) }
|
||||||
|
|
||||||
@@ -10,6 +10,12 @@ describe Crono::Job do
|
|||||||
expect(job.period).to be period
|
expect(job.period).to be period
|
||||||
end
|
end
|
||||||
|
|
||||||
|
describe '#next' do
|
||||||
|
it 'should return next performing time according to period' do
|
||||||
|
expect(job.next).to be_eql period.next
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
describe '#perform' do
|
describe '#perform' do
|
||||||
after { job.send(:model).destroy }
|
after { job.send(:model).destroy }
|
||||||
|
|
||||||
@@ -40,14 +46,14 @@ describe Crono::Job do
|
|||||||
expect(failing_job.healthy).to be false
|
expect(failing_job.healthy).to be false
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'should set @_crono_job variable to instance' do
|
xit 'should set @_crono_job variable to instance' do
|
||||||
job.perform
|
job.perform
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
describe '#description' do
|
describe '#description' do
|
||||||
it 'should return job identificator' do
|
it 'should return job identificator' do
|
||||||
expect(job.description).to be_eql('Perform TestJob every 2 days')
|
expect(job.description).to be_eql('Perform TestJob every 2 days at 15:00')
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -1,27 +1,38 @@
|
|||||||
require 'spec_helper'
|
require 'spec_helper'
|
||||||
|
|
||||||
describe Crono::Scheduler do
|
describe Crono::Scheduler do
|
||||||
before(:each) do
|
let(:scheduler) { Crono::Scheduler.new }
|
||||||
@scheduler = Crono::Scheduler.new
|
|
||||||
@jobs = [
|
|
||||||
Crono::Period.new(3.day, 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.day, at: 40.minutes.from_now.strftime('%H:%M'))
|
|
||||||
].map { |period| Crono::Job.new(TestJob, period) }
|
|
||||||
@scheduler.jobs = @jobs
|
|
||||||
end
|
|
||||||
|
|
||||||
describe '#add_job' do
|
describe '#add_job' do
|
||||||
it 'should call Job#load on 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)
|
expect(@job).to receive(:load)
|
||||||
@scheduler.add_job(@job)
|
scheduler.add_job(@job)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
describe '#next' do
|
describe '#next_jobs' do
|
||||||
it 'should return next job in schedule' do
|
it 'should return next job in schedule' do
|
||||||
expect(@scheduler.next).to be @jobs[0]
|
scheduler.jobs = jobs = [
|
||||||
|
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) }
|
||||||
|
|
||||||
|
time, jobs = scheduler.next_jobs
|
||||||
|
expect(jobs).to be_eql [jobs[0]]
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'should return an array of jobs scheduled at same time' do
|
||||||
|
time = 5.minutes.from_now
|
||||||
|
scheduler.jobs = jobs = [
|
||||||
|
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) }
|
||||||
|
|
||||||
|
time, jobs = scheduler.next_jobs
|
||||||
|
expect(jobs).to be_eql [jobs[0], jobs[1]]
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user