Skip to content

Commit

Permalink
Add Crono::Cronotab to process cronotab
Browse files Browse the repository at this point in the history
  • Loading branch information
plashchynski committed Apr 13, 2015
1 parent 8174f86 commit 1900a06
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 13 deletions.
1 change: 1 addition & 0 deletions lib/crono.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ module Crono
require 'crono/scheduler'
require 'crono/config'
require 'crono/performer_proxy'
require 'crono/cronotab'
require 'crono/orm/active_record/crono_job'
require 'crono/railtie' if defined?(Rails)

Expand Down
2 changes: 1 addition & 1 deletion lib/crono/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ def run

write_pid
load_rails
Cronotab.process(File.expand_path(config.cronotab))
print_banner

check_jobs
Expand Down Expand Up @@ -71,7 +72,6 @@ def load_rails
require 'rails'
require File.expand_path('config/environment.rb')
::Rails.application.eager_load!
require File.expand_path(config.cronotab)
end

def check_jobs
Expand Down
10 changes: 10 additions & 0 deletions lib/crono/cronotab.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module Crono
class Cronotab
def self.process(cronotab_path = nil)
cronotab_path ||= ENV['CRONOTAB'] || (defined?(Rails) &&
File.join(Rails.root, Config::CRONOTAB))
fail 'No cronotab defined' unless cronotab_path
require cronotab_path
end
end
end
14 changes: 2 additions & 12 deletions lib/tasks/crono_tasks.rake
Original file line number Diff line number Diff line change
@@ -1,26 +1,16 @@
module Crono
def self.load_cronotab
cronotab_path = ENV['CRONOTAB'] || (defined?(Rails) &&
File.join(Rails.root, Config::CRONOTAB))
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
Crono::Cronotab.process
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
Crono::Cronotab.process
puts 'Syntax ok'
end
end
20 changes: 20 additions & 0 deletions spec/cronotab_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
require 'spec_helper'

describe Crono::Cronotab do
describe '#process' do
it 'should load cronotab file' do
cronotab_path = File.expand_path('../assets/good_cronotab.rb', __FILE__)
expect(Crono.scheduler).to receive(:add_job).with(kind_of(Crono::Job))
expect {
Crono::Cronotab.process(cronotab_path)
}.to_not raise_error
end

it 'should raise error when cronotab is invalid' do
cronotab_path = File.expand_path('../assets/bad_cronotab.rb', __FILE__)
expect {
Crono::Cronotab.process(cronotab_path)
}.to raise_error
end
end
end

0 comments on commit 1900a06

Please sign in to comment.