class Timer::Action
class representing individual timed action
Attributes
Time when the Action should be called next
Public Class Methods
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
blocks an Action, so it won't be run
# File lib/rbot/timer.rb, line 88 def block @blocked = true end
# File lib/rbot/timer.rb, line 97 def blocked? @blocked end
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
modify the Action period
# File lib/rbot/timer.rb, line 83 def reschedule(period, &block) self.configure(:period => period, &block) end
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
unblocks a blocked Action
# File lib/rbot/timer.rb, line 93 def unblock @blocked = false end