Skip to content

Commit

Permalink
Add rake task to check cronotab.rb syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
plashchynski committed Mar 18, 2015
1 parent dc70212 commit fa97f57
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 7 deletions.
13 changes: 12 additions & 1 deletion lib/tasks/crono_tasks.rake
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
module Crono
def self.load_cronotab
require File.join(Rails.root, Config::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

Expand All @@ -12,4 +16,11 @@ namespace :crono do
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 changes: 12 additions & 0 deletions spec/assets/bad_cronotab.rb
Original file line number Diff line number Diff line change
@@ -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 changes: 9 additions & 0 deletions spec/assets/good_cronotab.rb
Original file line number Diff line number Diff line change
@@ -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
21 changes: 15 additions & 6 deletions spec/tasks/crono_tasks_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,20 @@
load 'tasks/crono_tasks.rake'
Rake::Task.define_task(:environment)

describe 'crono:clean' do
it 'should clean unused tasks from DB' do
Crono::CronoJob.create!(job_id: 'used_job')
expect(Crono).to receive(:load_cronotab)
Rake::Task['crono:clean'].invoke
expect(Crono::CronoJob.where(job_id: 'used_job')).not_to exist
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
1 change: 1 addition & 0 deletions spec/web_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
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!(
Expand Down

0 comments on commit fa97f57

Please sign in to comment.