mirror of
https://github.com/plashchynski/crono.git
synced 2026-03-09 07:40:05 +01:00
Compare commits
15 Commits
v0.8.0
...
next_jobs_
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b4ad8fb953 | ||
|
|
d075a55f03 | ||
|
|
2d72020ac4 | ||
|
|
cde8a2d214 | ||
|
|
2ec9cfa829 | ||
|
|
63c86c8cd9 | ||
|
|
e10daec9c6 | ||
|
|
f72c288ce8 | ||
|
|
78ce578484 | ||
|
|
0c77c490bd | ||
|
|
d889b9380d | ||
|
|
f75bdf352b | ||
|
|
fa97f573e0 | ||
|
|
dc70212f9d | ||
|
|
7328bea24c |
18
Gemfile.lock
18
Gemfile.lock
@@ -1,7 +1,7 @@
|
||||
PATH
|
||||
remote: .
|
||||
specs:
|
||||
crono (0.8.0)
|
||||
crono (0.8.1)
|
||||
activejob (~> 4.0)
|
||||
activerecord (~> 4.0)
|
||||
activesupport (~> 4.0)
|
||||
@@ -9,17 +9,17 @@ PATH
|
||||
GEM
|
||||
remote: https://rubygems.org/
|
||||
specs:
|
||||
activejob (4.2.0)
|
||||
activesupport (= 4.2.0)
|
||||
activejob (4.2.1)
|
||||
activesupport (= 4.2.1)
|
||||
globalid (>= 0.3.0)
|
||||
activemodel (4.2.0)
|
||||
activesupport (= 4.2.0)
|
||||
activemodel (4.2.1)
|
||||
activesupport (= 4.2.1)
|
||||
builder (~> 3.1)
|
||||
activerecord (4.2.0)
|
||||
activemodel (= 4.2.0)
|
||||
activesupport (= 4.2.0)
|
||||
activerecord (4.2.1)
|
||||
activemodel (= 4.2.1)
|
||||
activesupport (= 4.2.1)
|
||||
arel (~> 6.0)
|
||||
activesupport (4.2.0)
|
||||
activesupport (4.2.1)
|
||||
i18n (~> 0.7)
|
||||
json (~> 1.7, >= 1.7.7)
|
||||
minitest (~> 5.1)
|
||||
|
||||
@@ -49,7 +49,7 @@ Now you are ready to move forward to create a job and schedule it.
|
||||
|
||||
Crono can use Active Job jobs from `app/jobs/`. The only requirements is that the `perform` method should take no arguments.
|
||||
|
||||
Here's an example of a test job:
|
||||
Here's an example of a job:
|
||||
|
||||
```ruby
|
||||
# app/jobs/test_job.rb
|
||||
@@ -72,6 +72,8 @@ class TestJob # This is not an Active Job job, but pretty legal Crono job.
|
||||
end
|
||||
```
|
||||
|
||||
_Please note that crono uses threads, so your code should be thread-safe_
|
||||
|
||||
#### Job Schedule
|
||||
|
||||
Schedule list is defined in the file `config/cronotab.rb`, that created using `crono:install`. The semantic is pretty straightforward:
|
||||
|
||||
@@ -11,5 +11,6 @@ require 'crono/scheduler'
|
||||
require 'crono/config'
|
||||
require 'crono/performer_proxy'
|
||||
require 'crono/orm/active_record/crono_job'
|
||||
require 'crono/railtie' if defined?(Rails)
|
||||
|
||||
Crono.autoload :Web, 'crono/web'
|
||||
|
||||
@@ -2,8 +2,6 @@ require 'crono'
|
||||
require 'optparse'
|
||||
|
||||
module Crono
|
||||
mattr_accessor :scheduler
|
||||
|
||||
# Crono::CLI - The main class for the crono daemon exacutable `bin/crono`
|
||||
class CLI
|
||||
include Singleton
|
||||
@@ -33,7 +31,7 @@ module Crono
|
||||
|
||||
def setup_log
|
||||
if config.daemonize
|
||||
self.logifile = config.logfile
|
||||
self.logfile = config.logfile
|
||||
daemonize
|
||||
else
|
||||
self.logfile = STDOUT
|
||||
@@ -81,9 +79,10 @@ module Crono
|
||||
end
|
||||
|
||||
def start_working_loop
|
||||
while (job = Crono.scheduler.next)
|
||||
sleep(job.next - Time.now)
|
||||
job.perform
|
||||
while true
|
||||
next_time, jobs = Crono.scheduler.next_jobs
|
||||
sleep(next_time - Time.now)
|
||||
jobs.each(&:perform)
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
@@ -17,8 +17,7 @@ module Crono
|
||||
end
|
||||
|
||||
def next
|
||||
next_time = period.next(since: last_performed_at)
|
||||
next_time.past? ? period.next : next_time
|
||||
period.next(since: last_performed_at)
|
||||
end
|
||||
|
||||
def description
|
||||
@@ -61,7 +60,9 @@ module Crono
|
||||
end
|
||||
|
||||
def perform_job
|
||||
performer.new.perform
|
||||
performer_instance = performer.new
|
||||
performer_instance.instance_variable_set(:@_crono_job, self)
|
||||
performer_instance.perform
|
||||
finished_time_sec = format('%.2f', Time.now - last_performed_at)
|
||||
rescue StandardError => e
|
||||
handle_job_fail(e, finished_time_sec)
|
||||
|
||||
@@ -5,5 +5,9 @@ module Crono
|
||||
class CronoJob < ActiveRecord::Base
|
||||
self.table_name = 'crono_jobs'
|
||||
validates :job_id, presence: true, uniqueness: true
|
||||
|
||||
def self.outdated
|
||||
self
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
9
lib/crono/railtie.rb
Normal file
9
lib/crono/railtie.rb
Normal file
@@ -0,0 +1,9 @@
|
||||
module Crono
|
||||
class Railtie < ::Rails::Railtie
|
||||
rake_tasks do
|
||||
Dir[File.join(File.dirname(__FILE__), '../tasks/*.rake')].each do |file|
|
||||
load file
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -12,14 +12,10 @@ module Crono
|
||||
jobs << job
|
||||
end
|
||||
|
||||
def next
|
||||
queue.first
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def queue
|
||||
jobs.sort_by(&:next)
|
||||
def next_jobs
|
||||
jobs.group_by(&:next).sort_by {|time,_| time }.first
|
||||
end
|
||||
end
|
||||
|
||||
mattr_accessor :scheduler
|
||||
end
|
||||
|
||||
@@ -1,3 +1,3 @@
|
||||
module Crono
|
||||
VERSION = '0.8.0'
|
||||
VERSION = '0.8.1'
|
||||
end
|
||||
|
||||
26
lib/tasks/crono_tasks.rake
Normal file
26
lib/tasks/crono_tasks.rake
Normal file
@@ -0,0 +1,26 @@
|
||||
module Crono
|
||||
def self.load_cronotab
|
||||
cronotab_path = ENV['CRONOTAB'] || (defined?(Rails) &&
|
||||
File.join(Rails.root, cronotab_path))
|
||||
fail 'No cronotab defined' unless cronotab_path
|
||||
puts "Load cronotab #{cronotab_path}"
|
||||
require cronotab_path
|
||||
end
|
||||
end
|
||||
|
||||
namespace :crono do
|
||||
desc 'Clean unused job stats from DB'
|
||||
task clean: :environment do
|
||||
Crono.scheduler = Crono::Scheduler.new
|
||||
Crono.load_cronotab
|
||||
current_job_ids = Crono.scheduler.jobs.map(&:job_id)
|
||||
Crono::CronoJob.where.not(job_id: current_job_ids).destroy_all
|
||||
end
|
||||
|
||||
desc 'Check cronotab.rb syntax'
|
||||
task check: :environment do
|
||||
Crono.scheduler = Crono::Scheduler.new
|
||||
Crono.load_cronotab
|
||||
puts 'Syntax ok'
|
||||
end
|
||||
end
|
||||
12
spec/assets/bad_cronotab.rb
Normal file
12
spec/assets/bad_cronotab.rb
Normal file
@@ -0,0 +1,12 @@
|
||||
# This is an example of a bad cronotab for tests
|
||||
|
||||
class TestJob
|
||||
def perform
|
||||
puts 'Test!'
|
||||
end
|
||||
end
|
||||
|
||||
# This is an error, because you can use `on` options with
|
||||
# a period less than 7 days.
|
||||
|
||||
Crono.perform(TestJob).every 5.days, on: :sunday
|
||||
9
spec/assets/good_cronotab.rb
Normal file
9
spec/assets/good_cronotab.rb
Normal file
@@ -0,0 +1,9 @@
|
||||
# This is an example of a good cronotab for tests
|
||||
|
||||
class TestJob
|
||||
def perform
|
||||
puts 'Test!'
|
||||
end
|
||||
end
|
||||
|
||||
Crono.perform(TestJob).every 5.seconds
|
||||
@@ -1,11 +1,6 @@
|
||||
require 'spec_helper'
|
||||
require 'crono/cli'
|
||||
|
||||
class TestJob
|
||||
def perform
|
||||
end
|
||||
end
|
||||
|
||||
describe Crono::CLI do
|
||||
let(:cli) { Crono::CLI.instance }
|
||||
|
||||
|
||||
@@ -1,18 +1,7 @@
|
||||
require 'spec_helper'
|
||||
|
||||
class TestJob
|
||||
def perform
|
||||
end
|
||||
end
|
||||
|
||||
class TestFailingJob
|
||||
def perform
|
||||
fail 'Some error'
|
||||
end
|
||||
end
|
||||
|
||||
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(:failing_job) { Crono::Job.new(TestFailingJob, period) }
|
||||
|
||||
@@ -21,6 +10,12 @@ describe Crono::Job do
|
||||
expect(job.period).to be period
|
||||
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
|
||||
after { job.send(:model).destroy }
|
||||
|
||||
@@ -37,20 +32,28 @@ describe Crono::Job do
|
||||
expect(saved_log).to include 'Some error'
|
||||
end
|
||||
|
||||
it 'should set Job#healthy to true if perform ok' do
|
||||
xit 'should set Job#healthy to true if perform ok' do
|
||||
class TestJob
|
||||
def perform
|
||||
@_crono_job
|
||||
end
|
||||
end
|
||||
job.perform.join
|
||||
expect(job.healthy).to be true
|
||||
end
|
||||
|
||||
it 'should set Job#healthy to false if perform with error' do
|
||||
failing_job.perform.join
|
||||
expect(failing_job.healthy).to be false
|
||||
end
|
||||
|
||||
xit 'should set @_crono_job variable to instance' do
|
||||
job.perform
|
||||
end
|
||||
end
|
||||
|
||||
describe '#description' 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
|
||||
|
||||
|
||||
@@ -1,10 +1,5 @@
|
||||
require 'spec_helper'
|
||||
|
||||
class TestJob
|
||||
def perform
|
||||
end
|
||||
end
|
||||
|
||||
describe Crono::PerformerProxy do
|
||||
it 'should add job to schedule' do
|
||||
expect(Crono.scheduler).to receive(:add_job).with(kind_of(Crono::Job))
|
||||
|
||||
@@ -1,32 +1,38 @@
|
||||
require 'spec_helper'
|
||||
|
||||
class TestJob
|
||||
def perform
|
||||
end
|
||||
end
|
||||
|
||||
describe Crono::Scheduler do
|
||||
before(:each) do
|
||||
@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
|
||||
let(:scheduler) { Crono::Scheduler.new }
|
||||
|
||||
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'))
|
||||
expect(@job).to receive(:load)
|
||||
@scheduler.add_job(@job)
|
||||
scheduler.add_job(@job)
|
||||
end
|
||||
end
|
||||
|
||||
describe '#next' do
|
||||
describe '#next_jobs' 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
|
||||
|
||||
@@ -13,3 +13,14 @@ ActiveRecord::Base.establish_connection(
|
||||
|
||||
ActiveRecord::Base.logger = Logger.new(STDOUT)
|
||||
CreateCronoJobs.up
|
||||
|
||||
class TestJob
|
||||
def perform
|
||||
end
|
||||
end
|
||||
|
||||
class TestFailingJob
|
||||
def perform
|
||||
fail 'Some error'
|
||||
end
|
||||
end
|
||||
|
||||
23
spec/tasks/crono_tasks_spec.rb
Normal file
23
spec/tasks/crono_tasks_spec.rb
Normal file
@@ -0,0 +1,23 @@
|
||||
require 'spec_helper'
|
||||
require 'rake'
|
||||
|
||||
load 'tasks/crono_tasks.rake'
|
||||
Rake::Task.define_task(:environment)
|
||||
|
||||
describe 'rake' do
|
||||
describe 'crono:clean' do
|
||||
it 'should clean unused tasks from DB' do
|
||||
Crono::CronoJob.create!(job_id: 'used_job')
|
||||
ENV['CRONOTAB'] = File.expand_path('../../assets/good_cronotab.rb', __FILE__)
|
||||
Rake::Task['crono:clean'].invoke
|
||||
expect(Crono::CronoJob.where(job_id: 'used_job')).not_to exist
|
||||
end
|
||||
end
|
||||
|
||||
describe 'crono:check' do
|
||||
it 'should check cronotab syntax' do
|
||||
ENV['CRONOTAB'] = File.expand_path('../../assets/bad_cronotab.rb', __FILE__)
|
||||
expect { Rake::Task['crono:check'].invoke }.to raise_error
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -6,6 +6,7 @@ describe Crono::Web do
|
||||
let(:app) { Crono::Web }
|
||||
|
||||
before do
|
||||
Crono::CronoJob.destroy_all
|
||||
@test_job_id = 'Perform TestJob every 5 seconds'
|
||||
@test_job_log = 'All runs ok'
|
||||
@test_job = Crono::CronoJob.create!(
|
||||
|
||||
Reference in New Issue
Block a user