2014-01-30 00:21:38 -05:00
|
|
|
module Scheduler
|
|
|
|
class ScheduleInfo
|
|
|
|
attr_accessor :next_run,
|
|
|
|
:prev_run,
|
|
|
|
:prev_duration,
|
2014-02-11 21:32:34 -05:00
|
|
|
:prev_result,
|
|
|
|
:current_owner
|
2014-01-30 00:21:38 -05:00
|
|
|
|
|
|
|
def initialize(klass, manager)
|
|
|
|
@klass = klass
|
|
|
|
@manager = manager
|
|
|
|
|
|
|
|
data = nil
|
|
|
|
|
2017-10-11 03:46:53 -04:00
|
|
|
if data = @manager.redis.get(key)
|
2014-01-30 00:21:38 -05:00
|
|
|
data = JSON.parse(data)
|
|
|
|
end
|
|
|
|
|
|
|
|
if data
|
|
|
|
@next_run = data["next_run"]
|
|
|
|
@prev_run = data["prev_run"]
|
|
|
|
@prev_result = data["prev_result"]
|
|
|
|
@prev_duration = data["prev_duration"]
|
2014-02-11 21:32:34 -05:00
|
|
|
@current_owner = data["current_owner"]
|
2014-01-30 00:21:38 -05:00
|
|
|
end
|
|
|
|
rescue
|
|
|
|
# corrupt redis
|
2014-02-11 21:32:34 -05:00
|
|
|
@next_run = @prev_run = @prev_result = @prev_duration = @current_owner = nil
|
2014-01-30 00:21:38 -05:00
|
|
|
end
|
|
|
|
|
2017-07-24 14:30:35 -04:00
|
|
|
# this means the schedule is going to fire, it is setup correctly
|
|
|
|
# invalid schedules are fixed by running "schedule!"
|
|
|
|
# this happens automatically after if fire by the manager
|
2014-01-30 00:21:38 -05:00
|
|
|
def valid?
|
|
|
|
return false unless @next_run
|
2014-03-14 13:02:21 -04:00
|
|
|
(!@prev_run && @next_run < Time.now.to_i + 5.minutes) || valid_every? || valid_daily?
|
|
|
|
end
|
|
|
|
|
|
|
|
def valid_every?
|
|
|
|
return false unless @klass.every
|
2014-09-25 11:44:48 -04:00
|
|
|
!!@prev_run &&
|
2014-01-30 00:21:38 -05:00
|
|
|
@prev_run <= Time.now.to_i &&
|
|
|
|
@next_run < @prev_run + @klass.every * (1 + @manager.random_ratio)
|
|
|
|
end
|
|
|
|
|
2014-03-14 13:02:21 -04:00
|
|
|
def valid_daily?
|
|
|
|
return false unless @klass.daily
|
2017-07-24 14:30:35 -04:00
|
|
|
return true if !@prev_run && @next_run && @next_run <= (Time.zone.now + 1.day).to_i
|
2014-09-25 11:44:48 -04:00
|
|
|
!!@prev_run &&
|
2017-07-24 14:30:35 -04:00
|
|
|
@prev_run <= Time.zone.now.to_i &&
|
2014-09-25 11:44:48 -04:00
|
|
|
@next_run < @prev_run + 1.day
|
2014-03-14 13:02:21 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def schedule_every!
|
2014-01-30 00:21:38 -05:00
|
|
|
if !valid? && @prev_run
|
|
|
|
mixup = @klass.every * @manager.random_ratio
|
|
|
|
mixup = (mixup * Random.rand - mixup / 2).to_i
|
|
|
|
@next_run = @prev_run + mixup + @klass.every
|
|
|
|
end
|
|
|
|
|
|
|
|
if !valid?
|
|
|
|
@next_run = Time.now.to_i + 5.minutes * Random.rand
|
|
|
|
end
|
2014-03-14 13:02:21 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def schedule_daily!
|
|
|
|
return if valid?
|
|
|
|
|
|
|
|
at = @klass.daily[:at] || 0
|
2017-07-24 11:03:03 -04:00
|
|
|
today_begin = Time.zone.now.midnight.to_i
|
2014-03-14 13:02:21 -04:00
|
|
|
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
|
2014-01-30 00:21:38 -05:00
|
|
|
|
|
|
|
write!
|
|
|
|
end
|
|
|
|
|
|
|
|
def write!
|
2015-06-25 23:32:08 -04:00
|
|
|
|
2014-01-30 00:21:38 -05:00
|
|
|
clear!
|
|
|
|
redis.set key, {
|
|
|
|
next_run: @next_run,
|
|
|
|
prev_run: @prev_run,
|
|
|
|
prev_duration: @prev_duration,
|
2014-02-11 21:32:34 -05:00
|
|
|
prev_result: @prev_result,
|
|
|
|
current_owner: @current_owner
|
2014-01-30 00:21:38 -05:00
|
|
|
}.to_json
|
2014-04-17 01:57:17 -04:00
|
|
|
|
2017-10-11 03:46:53 -04:00
|
|
|
redis.zadd queue_key, @next_run, @klass if @next_run
|
2014-01-30 00:21:38 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def del!
|
|
|
|
clear!
|
2014-02-11 21:32:34 -05:00
|
|
|
@next_run = @prev_run = @prev_result = @prev_duration = @current_owner = nil
|
2014-01-30 00:21:38 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def key
|
2015-06-25 23:32:08 -04:00
|
|
|
if @klass.is_per_host
|
|
|
|
Manager.schedule_key(@klass, @manager.hostname)
|
|
|
|
else
|
|
|
|
Manager.schedule_key(@klass)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def queue_key
|
|
|
|
if @klass.is_per_host
|
|
|
|
Manager.queue_key(@manager.hostname)
|
|
|
|
else
|
|
|
|
Manager.queue_key
|
|
|
|
end
|
2014-01-30 00:21:38 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def redis
|
|
|
|
@manager.redis
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
def clear!
|
|
|
|
redis.del key
|
2015-06-25 23:32:08 -04:00
|
|
|
redis.zrem queue_key, @klass
|
2014-01-30 00:21:38 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|