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:
Robin Ward
2014-03-14 13:02:21 -04:00
parent 2b5e4c3919
commit e22f1ae186
4 changed files with 125 additions and 39 deletions

View File

@ -1,4 +1,12 @@
module Scheduler::Schedule
def daily(options=nil)
if options
@daily = options
end
@daily
end
def every(duration=nil)
if duration
@every = duration
@ -15,6 +23,6 @@ module Scheduler::Schedule
end
def scheduled?
!!@every
!!@every || !!@daily
end
end

View File

@ -31,14 +31,22 @@ module Scheduler
def valid?
return false unless @next_run
(!@prev_run && @next_run < Time.now.to_i + 5.minutes) ||
( @prev_run &&
@prev_run <= Time.now.to_i &&
@next_run < @prev_run + @klass.every * (1 + @manager.random_ratio)
)
(!@prev_run && @next_run < Time.now.to_i + 5.minutes) || valid_every? || valid_daily?
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
mixup = @klass.every * @manager.random_ratio
mixup = (mixup * Random.rand - mixup / 2).to_i
@ -48,6 +56,30 @@ module Scheduler
if !valid?
@next_run = Time.now.to_i + 5.minutes * Random.rand
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!
end