class Lita::Timing::Scheduled

Constants

ONE_WEEK_IN_SECONDS

Public Class Methods

new(name, redis) click to toggle source
# File lib/lita/timing/scheduled.rb, line 9
def initialize(name, redis)
  @name, @redis = name, redis
  @mutex = Timing::Mutex.new("#{name}-lock", redis)
end

Public Instance Methods

daily_at(time, days = nil) { || ... } click to toggle source
# File lib/lita/timing/scheduled.rb, line 14
def daily_at(time, days = nil, &block)
  @mutex.syncronise do
    days ||= [:monday, :tuesday, :wednesday, :thursday, :friday, :saturday, :sunday]
    last_run_at = get_last_run_at
    next_run = calc_next_daily_run(time, days, last_run_at)
    if next_run < Time.now
      yield
      set_last_run_at
    end
  end
end
weekly_at(time, day) { || ... } click to toggle source
# File lib/lita/timing/scheduled.rb, line 26
def weekly_at(time, day, &block)
  @mutex.syncronise do
    last_run_at = get_last_run_at
    next_run = calc_next_weekly_run(time, day, last_run_at)
    if next_run < Time.now
      yield
      set_last_run_at
    end
  end
end

Private Instance Methods

calc_next_daily_run(time, days, last_run_at) click to toggle source
# File lib/lita/timing/scheduled.rb, line 39
def calc_next_daily_run(time, days, last_run_at)
  hours, minutes = TimeParser.extract_hours_and_minutes(time)
  wdays = TimeParser.day_strings_to_ints(days)

  next_run_at = last_run_at + 1
  loop do
    if next_run_at.hour == hours && next_run_at.min == minutes && next_run_at.sec == 0 && wdays.include?(next_run_at.wday)
      return next_run_at
    end
    next_run_at += 1
  end
end
calc_next_weekly_run(time, day, last_run_at) click to toggle source
# File lib/lita/timing/scheduled.rb, line 52
def calc_next_weekly_run(time, day, last_run_at)
  hours, minutes = TimeParser.extract_hours_and_minutes(time)
  wday = TimeParser.day_string_to_int(day)

  next_run_at = last_run_at + 1
  loop do
    if next_run_at.hour == hours && next_run_at.min == minutes && next_run_at.sec == 0 && next_run_at.wday == wday
      return next_run_at
    end
    next_run_at += 1
  end
end
get_last_run_at() click to toggle source
# File lib/lita/timing/scheduled.rb, line 65
def get_last_run_at
  value = @redis.get(@name)
  value ? Time.at(value.to_i).utc : set_last_run_at
end
set_last_run_at(time = Time.now.utc) click to toggle source
# File lib/lita/timing/scheduled.rb, line 70
def set_last_run_at(time = Time.now.utc)
  @redis.set(@name, time.to_i, ex: ONE_WEEK_IN_SECONDS * 2)
  time
end