2014-01-29 23:21:38 -06:00
|
|
|
# encoding: utf-8
|
|
|
|
require 'spec_helper'
|
|
|
|
require 'scheduler/scheduler'
|
|
|
|
|
|
|
|
describe Scheduler::Manager do
|
|
|
|
|
|
|
|
module Testing
|
|
|
|
class RandomJob
|
|
|
|
extend ::Scheduler::Schedule
|
|
|
|
|
|
|
|
def self.runs=(val)
|
|
|
|
@runs = val
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.runs
|
|
|
|
@runs ||= 0
|
|
|
|
end
|
|
|
|
|
|
|
|
every 5.minutes
|
|
|
|
|
|
|
|
def perform
|
|
|
|
self.class.runs+=1
|
|
|
|
sleep 0.001
|
|
|
|
end
|
|
|
|
end
|
2014-02-11 20:32:34 -06:00
|
|
|
|
|
|
|
class SuperLongJob
|
|
|
|
extend ::Scheduler::Schedule
|
|
|
|
|
|
|
|
every 10.minutes
|
|
|
|
|
|
|
|
def perform
|
|
|
|
sleep 1000
|
|
|
|
end
|
|
|
|
end
|
2014-01-29 23:21:38 -06:00
|
|
|
end
|
|
|
|
|
2014-02-10 23:11:51 -06:00
|
|
|
let(:manager) { Scheduler::Manager.new(DiscourseRedis.new) }
|
2014-01-29 23:21:38 -06:00
|
|
|
|
|
|
|
before do
|
2014-02-11 20:32:34 -06:00
|
|
|
$redis.del manager.class.lock_key
|
2014-01-29 23:21:38 -06:00
|
|
|
$redis.del manager.class.queue_key
|
2014-02-11 20:32:34 -06:00
|
|
|
manager.remove(Testing::RandomJob)
|
|
|
|
manager.remove(Testing::SuperLongJob)
|
2014-01-29 23:21:38 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
after do
|
|
|
|
manager.stop!
|
2014-02-11 20:32:34 -06:00
|
|
|
manager.remove(Testing::RandomJob)
|
|
|
|
manager.remove(Testing::SuperLongJob)
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#sync' do
|
|
|
|
|
|
|
|
it 'increases' do
|
|
|
|
Scheduler::Manager.seq.should == Scheduler::Manager.seq - 1
|
|
|
|
end
|
2014-01-29 23:21:38 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
describe '#tick' do
|
2014-02-11 20:32:34 -06:00
|
|
|
|
|
|
|
it 'should recover from crashed manager' do
|
|
|
|
|
|
|
|
info = manager.schedule_info(Testing::SuperLongJob)
|
|
|
|
info.next_run = Time.now.to_i - 1
|
|
|
|
info.write!
|
|
|
|
|
|
|
|
manager.tick
|
|
|
|
manager.stop!
|
|
|
|
|
|
|
|
$redis.del manager.identity_key
|
|
|
|
|
|
|
|
manager = Scheduler::Manager.new(DiscourseRedis.new)
|
|
|
|
manager.reschedule_orphans!
|
|
|
|
|
|
|
|
info = manager.schedule_info(Testing::SuperLongJob)
|
|
|
|
info.next_run.should <= Time.now.to_i
|
|
|
|
end
|
|
|
|
|
2014-01-29 23:21:38 -06:00
|
|
|
it 'should only run pending job once' do
|
|
|
|
|
|
|
|
Testing::RandomJob.runs = 0
|
|
|
|
|
|
|
|
info = manager.schedule_info(Testing::RandomJob)
|
|
|
|
info.next_run = Time.now.to_i - 1
|
|
|
|
info.write!
|
|
|
|
|
|
|
|
(0..5).map do
|
|
|
|
Thread.new do
|
2014-02-08 14:55:11 -06:00
|
|
|
manager = Scheduler::Manager.new(DiscourseRedis.new)
|
2014-01-29 23:21:38 -06:00
|
|
|
manager.blocking_tick
|
2014-02-11 20:32:34 -06:00
|
|
|
manager.stop!
|
2014-01-29 23:21:38 -06:00
|
|
|
end
|
|
|
|
end.map(&:join)
|
|
|
|
|
|
|
|
Testing::RandomJob.runs.should == 1
|
|
|
|
|
|
|
|
info = manager.schedule_info(Testing::RandomJob)
|
|
|
|
info.prev_run.should be <= Time.now.to_i
|
|
|
|
info.prev_duration.should be > 0
|
|
|
|
info.prev_result.should == "OK"
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2014-02-05 17:14:41 -06:00
|
|
|
describe '#discover_schedules' do
|
|
|
|
it 'Discovers Testing::RandomJob' do
|
|
|
|
Scheduler::Manager.discover_schedules.should include(Testing::RandomJob)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-01-29 23:21:38 -06:00
|
|
|
describe '#next_run' do
|
|
|
|
it 'should be within the next 5 mins if it never ran' do
|
|
|
|
|
|
|
|
manager.remove(Testing::RandomJob)
|
|
|
|
manager.ensure_schedule!(Testing::RandomJob)
|
|
|
|
|
|
|
|
manager.next_run(Testing::RandomJob)
|
|
|
|
.should be_within(5.minutes.to_i).of(Time.now.to_i + 5.minutes)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|