Class: Lita::Timing::Scheduled

Inherits:
Object
  • Object
show all
Defined in:
lib/lita/timing/scheduled.rb

Constant Summary collapse

ONE_WEEK_IN_SECONDS =
60 * 60 * 24 * 7

Instance Method Summary collapse

Constructor Details

#initialize(name, redis) ⇒ Scheduled

Returns a new instance of Scheduled.



9
10
11
12
# File 'lib/lita/timing/scheduled.rb', line 9

def initialize(name, redis)
  @name, @redis = name, redis
  @mutex = Timing::Mutex.new("#{name}-lock", redis)
end

Instance Method Details

#daily_at(time, days = nil, &block) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
# 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, &block) ⇒ Object



26
27
28
29
30
31
32
33
34
35
# 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