2014-01-30 00:21:38 -05:00
|
|
|
# encoding: utf-8
|
2015-10-11 05:41:23 -04:00
|
|
|
require 'rails_helper'
|
2014-01-30 00:21:38 -05:00
|
|
|
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 21:32:34 -05:00
|
|
|
|
|
|
|
class SuperLongJob
|
|
|
|
extend ::Scheduler::Schedule
|
|
|
|
|
|
|
|
every 10.minutes
|
|
|
|
|
|
|
|
def perform
|
|
|
|
sleep 1000
|
|
|
|
end
|
|
|
|
end
|
2015-06-25 23:32:08 -04:00
|
|
|
|
|
|
|
class PerHostJob
|
|
|
|
extend ::Scheduler::Schedule
|
|
|
|
|
|
|
|
per_host
|
|
|
|
every 10.minutes
|
|
|
|
|
|
|
|
def self.runs=(val)
|
|
|
|
@runs = val
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.runs
|
|
|
|
@runs ||= 0
|
|
|
|
end
|
|
|
|
|
|
|
|
def perform
|
|
|
|
self.class.runs += 1
|
|
|
|
end
|
|
|
|
end
|
2014-01-30 00:21:38 -05:00
|
|
|
end
|
|
|
|
|
2014-02-11 00:11:51 -05:00
|
|
|
let(:manager) { Scheduler::Manager.new(DiscourseRedis.new) }
|
2014-01-30 00:21:38 -05:00
|
|
|
|
|
|
|
before do
|
2014-02-11 21:32:34 -05:00
|
|
|
$redis.del manager.class.lock_key
|
2014-01-30 00:21:38 -05:00
|
|
|
$redis.del manager.class.queue_key
|
2014-02-11 21:32:34 -05:00
|
|
|
manager.remove(Testing::RandomJob)
|
|
|
|
manager.remove(Testing::SuperLongJob)
|
2015-06-25 23:32:08 -04:00
|
|
|
manager.remove(Testing::PerHostJob)
|
2014-01-30 00:21:38 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
after do
|
|
|
|
manager.stop!
|
2014-02-11 21:32:34 -05:00
|
|
|
manager.remove(Testing::RandomJob)
|
|
|
|
manager.remove(Testing::SuperLongJob)
|
2015-06-25 23:32:08 -04:00
|
|
|
manager.remove(Testing::PerHostJob)
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'per host jobs' do
|
|
|
|
it "correctly schedules on multiple hosts" do
|
|
|
|
Testing::PerHostJob.runs = 0
|
|
|
|
|
|
|
|
hosts = ['a','b','c']
|
|
|
|
|
|
|
|
hosts.map do |host|
|
|
|
|
|
|
|
|
manager = Scheduler::Manager.new(DiscourseRedis.new, hostname: host)
|
|
|
|
manager.ensure_schedule!(Testing::PerHostJob)
|
|
|
|
|
|
|
|
info = manager.schedule_info(Testing::PerHostJob)
|
|
|
|
info.next_run = Time.now.to_i - 1
|
|
|
|
info.write!
|
|
|
|
|
|
|
|
manager
|
|
|
|
|
|
|
|
end.each do |manager|
|
|
|
|
|
|
|
|
manager.blocking_tick
|
|
|
|
manager.stop!
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
expect(Testing::PerHostJob.runs).to eq(3)
|
|
|
|
|
|
|
|
end
|
2014-02-11 21:32:34 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
describe '#sync' do
|
|
|
|
|
|
|
|
it 'increases' do
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(Scheduler::Manager.seq).to eq(Scheduler::Manager.seq - 1)
|
2014-02-11 21:32:34 -05:00
|
|
|
end
|
2014-01-30 00:21:38 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
describe '#tick' do
|
2014-02-11 21:32:34 -05:00
|
|
|
|
2014-03-03 15:36:50 -05:00
|
|
|
it 'should nuke missing jobs' do
|
|
|
|
$redis.zadd Scheduler::Manager.queue_key, Time.now.to_i - 1000, "BLABLA"
|
|
|
|
manager.tick
|
2015-01-09 11:34:37 -05:00
|
|
|
expect($redis.zcard(Scheduler::Manager.queue_key)).to eq(0)
|
2014-03-03 15:36:50 -05:00
|
|
|
end
|
|
|
|
|
2014-02-11 21:32:34 -05: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)
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(info.next_run).to be <= Time.now.to_i
|
2014-02-11 21:32:34 -05:00
|
|
|
end
|
|
|
|
|
2014-01-30 00:21:38 -05: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 15:55:11 -05:00
|
|
|
manager = Scheduler::Manager.new(DiscourseRedis.new)
|
2014-01-30 00:21:38 -05:00
|
|
|
manager.blocking_tick
|
2014-02-11 21:32:34 -05:00
|
|
|
manager.stop!
|
2014-01-30 00:21:38 -05:00
|
|
|
end
|
|
|
|
end.map(&:join)
|
|
|
|
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(Testing::RandomJob.runs).to eq(1)
|
2014-01-30 00:21:38 -05:00
|
|
|
|
|
|
|
info = manager.schedule_info(Testing::RandomJob)
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(info.prev_run).to be <= Time.now.to_i
|
|
|
|
expect(info.prev_duration).to be > 0
|
|
|
|
expect(info.prev_result).to eq("OK")
|
2014-01-30 00:21:38 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2014-02-05 18:14:41 -05:00
|
|
|
describe '#discover_schedules' do
|
|
|
|
it 'Discovers Testing::RandomJob' do
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(Scheduler::Manager.discover_schedules).to include(Testing::RandomJob)
|
2014-02-05 18:14:41 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-01-30 00:21:38 -05: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)
|
|
|
|
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(manager.next_run(Testing::RandomJob))
|
|
|
|
.to be_within(5.minutes.to_i).of(Time.now.to_i + 5.minutes)
|
2014-01-30 00:21:38 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|