class Timer::Action

class representing individual timed action

Attributes

next[RW]

Time when the Action should be called next

Public Class Methods

new(options = {}, &block) click to toggle source

Options are:

start

Time when the Action should be run for the first time. Repeatable Actions will be repeated after that, see :period. One-time Actions will not (obviously) Default: Time.now + :period

period

How often repeatable Action should be run, in seconds. Default: 1

blocked

if true, Action starts as blocked (i.e. will stay dormant until unblocked)

args

Arguments to pass to the Action callback. Default: []

repeat

Should the Action be called repeatedly? Default: false

code

You can specify the Action body using &block, or using this option.

# File lib/rbot/timer.rb, line 43
def initialize(options = {}, &block)
  opts = {
    :period => 1,
    :blocked => false,
    :args => [],
    :repeat => false
  }.merge(options)

  @block = nil
  debug("adding timer #{self} :period => #{opts[:period]}, :repeat => #{opts[:repeat].inspect}")
  self.configure(opts, &block)
  debug("added #{self}")
end

Public Instance Methods

block() click to toggle source

blocks an Action, so it won’t be run

# File lib/rbot/timer.rb, line 88
def block
  @blocked = true
end
blocked?() click to toggle source
# File lib/rbot/timer.rb, line 97
def blocked?
  @blocked
end
configure(opts = {}, &block) click to toggle source

Provides for on-the-fly reconfiguration of the Actions Accept the same arguments as the constructor

# File lib/rbot/timer.rb, line 59
def configure(opts = {}, &block)
  @period = opts[:period] if opts.include? :period
  @blocked = opts[:blocked] if opts.include? :blocked
  @repeat = opts[:repeat] if opts.include? :repeat

  if block_given?
    @block = block
  elsif opts[:code]
    @block = opts[:code]
  end

  raise 'huh?? blockless action?' unless @block
  if opts.include? :args
    @args = Array === opts[:args] ? opts[:args] : [opts[:args]]
  end

  if opts[:start] and (Time === opts[:start])
    self.next = opts[:start]
  else
    self.next = Time.now + (opts[:start] || @period)
  end
end
reschedule(period, &block) click to toggle source

modify the Action period

# File lib/rbot/timer.rb, line 83
def reschedule(period, &block)
  self.configure(:period => period, &block)
end
run(now = Time.now) click to toggle source

calls the Action callback, resets .next to the Time of the next call, if the Action is repeatable.

# File lib/rbot/timer.rb, line 103
def run(now = Time.now)
  raise 'inappropriate time to run()' unless self.next && self.next <= now
  self.next = nil
  begin
    @block.call(*@args)
  rescue Exception => e
    error "Timer action #{self.inspect}: block #{@block.inspect} failed!"
    error e.pretty_inspect
    debug e.backtrace.join("\n")
  end

  if @repeat && @period > 0
    self.next = now + @period
  end

  return self.next
end
unblock() click to toggle source

unblocks a blocked Action

# File lib/rbot/timer.rb, line 93
def unblock
  @blocked = false
end