mirror of
https://github.com/discourse/discourse.git
synced 2025-02-25 18:55:32 -06:00
Support for a daily job at a certain hour. Convert backup job to run at
3am instead of randomly during the day.
This commit is contained in:
parent
2b5e4c3919
commit
e22f1ae186
@ -2,7 +2,7 @@ require_dependency "backup_restore"
|
|||||||
|
|
||||||
module Jobs
|
module Jobs
|
||||||
class CreateBackup < Jobs::Scheduled
|
class CreateBackup < Jobs::Scheduled
|
||||||
every 1.day
|
daily at: 3.hours
|
||||||
sidekiq_options retry: false
|
sidekiq_options retry: false
|
||||||
|
|
||||||
def execute(args)
|
def execute(args)
|
||||||
|
@ -1,4 +1,12 @@
|
|||||||
module Scheduler::Schedule
|
module Scheduler::Schedule
|
||||||
|
|
||||||
|
def daily(options=nil)
|
||||||
|
if options
|
||||||
|
@daily = options
|
||||||
|
end
|
||||||
|
@daily
|
||||||
|
end
|
||||||
|
|
||||||
def every(duration=nil)
|
def every(duration=nil)
|
||||||
if duration
|
if duration
|
||||||
@every = duration
|
@every = duration
|
||||||
@ -15,6 +23,6 @@ module Scheduler::Schedule
|
|||||||
end
|
end
|
||||||
|
|
||||||
def scheduled?
|
def scheduled?
|
||||||
!!@every
|
!!@every || !!@daily
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -31,14 +31,22 @@ module Scheduler
|
|||||||
|
|
||||||
def valid?
|
def valid?
|
||||||
return false unless @next_run
|
return false unless @next_run
|
||||||
(!@prev_run && @next_run < Time.now.to_i + 5.minutes) ||
|
(!@prev_run && @next_run < Time.now.to_i + 5.minutes) || valid_every? || valid_daily?
|
||||||
( @prev_run &&
|
|
||||||
@prev_run <= Time.now.to_i &&
|
|
||||||
@next_run < @prev_run + @klass.every * (1 + @manager.random_ratio)
|
|
||||||
)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def schedule!
|
def valid_every?
|
||||||
|
return false unless @klass.every
|
||||||
|
@prev_run &&
|
||||||
|
@prev_run <= Time.now.to_i &&
|
||||||
|
@next_run < @prev_run + @klass.every * (1 + @manager.random_ratio)
|
||||||
|
end
|
||||||
|
|
||||||
|
def valid_daily?
|
||||||
|
return false unless @klass.daily
|
||||||
|
@prev_run && @prev_run <= Time.now.to_i && @next_run < @prev_run + 1.day
|
||||||
|
end
|
||||||
|
|
||||||
|
def schedule_every!
|
||||||
if !valid? && @prev_run
|
if !valid? && @prev_run
|
||||||
mixup = @klass.every * @manager.random_ratio
|
mixup = @klass.every * @manager.random_ratio
|
||||||
mixup = (mixup * Random.rand - mixup / 2).to_i
|
mixup = (mixup * Random.rand - mixup / 2).to_i
|
||||||
@ -48,6 +56,30 @@ module Scheduler
|
|||||||
if !valid?
|
if !valid?
|
||||||
@next_run = Time.now.to_i + 5.minutes * Random.rand
|
@next_run = Time.now.to_i + 5.minutes * Random.rand
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def schedule_daily!
|
||||||
|
return if valid?
|
||||||
|
|
||||||
|
at = @klass.daily[:at] || 0
|
||||||
|
today_begin = Time.now.midnight.to_i
|
||||||
|
today_offset = DateTime.now.seconds_since_midnight
|
||||||
|
|
||||||
|
# If it's later today
|
||||||
|
if at > today_offset
|
||||||
|
@next_run = today_begin + at
|
||||||
|
else
|
||||||
|
# Otherwise do it tomorrow
|
||||||
|
@next_run = today_begin + 1.day + at
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def schedule!
|
||||||
|
if @klass.every
|
||||||
|
schedule_every!
|
||||||
|
elsif @klass.daily
|
||||||
|
schedule_daily!
|
||||||
|
end
|
||||||
|
|
||||||
write!
|
write!
|
||||||
end
|
end
|
||||||
|
@ -4,48 +4,94 @@ require 'scheduler/scheduler'
|
|||||||
|
|
||||||
describe Scheduler::ScheduleInfo do
|
describe Scheduler::ScheduleInfo do
|
||||||
|
|
||||||
class RandomJob
|
let(:manager){ Scheduler::Manager.new }
|
||||||
extend ::Scheduler::Schedule
|
|
||||||
|
|
||||||
every 1.hour
|
context "every" do
|
||||||
|
class RandomJob
|
||||||
|
extend ::Scheduler::Schedule
|
||||||
|
|
||||||
def perform
|
every 1.hour
|
||||||
# work_it
|
|
||||||
|
def perform
|
||||||
|
# work_it
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
before do
|
||||||
|
@info = manager.schedule_info(RandomJob)
|
||||||
|
@info.del!
|
||||||
|
$redis.del manager.class.queue_key
|
||||||
|
end
|
||||||
|
|
||||||
|
after do
|
||||||
|
manager.stop!
|
||||||
|
end
|
||||||
|
|
||||||
|
it "is a scheduled job" do
|
||||||
|
RandomJob.should be_scheduled
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'starts off invalid' do
|
||||||
|
@info.valid?.should be_false
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'will have a due date in the next 5 minutes if it was blank' do
|
||||||
|
@info.schedule!
|
||||||
|
@info.valid?.should be_true
|
||||||
|
@info.next_run.should be_within(5.minutes).of(Time.now.to_i)
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'will have a due date within the next hour if it just ran' do
|
||||||
|
@info.prev_run = Time.now.to_i
|
||||||
|
@info.schedule!
|
||||||
|
@info.valid?.should be_true
|
||||||
|
@info.next_run.should be_within(1.hour * manager.random_ratio).of(Time.now.to_i + 1.hour)
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'is invalid if way in the future' do
|
||||||
|
@info.next_run = Time.now.to_i + 1.year
|
||||||
|
@info.valid?.should be_false
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
let(:manager){ Scheduler::Manager.new }
|
context "daily" do
|
||||||
|
|
||||||
before do
|
class DailyJob
|
||||||
@info = manager.schedule_info(RandomJob)
|
extend ::Scheduler::Schedule
|
||||||
@info.del!
|
daily at: 2.hours
|
||||||
$redis.del manager.class.queue_key
|
|
||||||
end
|
|
||||||
|
|
||||||
after do
|
def perform
|
||||||
manager.stop!
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'starts off invalid' do
|
before do
|
||||||
@info.valid?.should be_false
|
@info = manager.schedule_info(DailyJob)
|
||||||
end
|
@info.del!
|
||||||
|
$redis.del manager.class.queue_key
|
||||||
|
end
|
||||||
|
|
||||||
it 'will have a due date in the next 5 minutes if it was blank' do
|
after do
|
||||||
@info.schedule!
|
manager.stop!
|
||||||
@info.valid?.should be_true
|
end
|
||||||
@info.next_run.should be_within(5.minutes).of(Time.now.to_i)
|
|
||||||
end
|
|
||||||
|
|
||||||
it 'will have a due date within the next hour if it just ran' do
|
it "is a scheduled job" do
|
||||||
@info.prev_run = Time.now.to_i
|
DailyJob.should be_scheduled
|
||||||
@info.schedule!
|
end
|
||||||
@info.valid?.should be_true
|
|
||||||
@info.next_run.should be_within(1.hour * manager.random_ratio).of(Time.now.to_i + 1.hour)
|
|
||||||
end
|
|
||||||
|
|
||||||
it 'is invalid if way in the future' do
|
it "starts off invalid" do
|
||||||
@info.next_run = Time.now.to_i + 1.year
|
@info.valid?.should be_false
|
||||||
@info.valid?.should be_false
|
end
|
||||||
|
|
||||||
|
it "will have a due date at the appropriate time if blank" do
|
||||||
|
@info.next_run.should be_nil
|
||||||
|
@info.schedule!
|
||||||
|
@info.valid?.should be_true
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'is invalid if way in the future' do
|
||||||
|
@info.next_run = Time.now.to_i + 1.year
|
||||||
|
@info.valid?.should be_false
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
Loading…
Reference in New Issue
Block a user