Class: Timer::Action
- Inherits:
-
Object
- Object
- Timer::Action
- Defined in:
- /home/apoc/projects/ruby/rbot/lib/rbot/timer.rb
Overview
class representing individual timed action
Instance Attribute Summary (collapse)
-
- (Object) next
Time when the Action should be called next.
Instance Method Summary (collapse)
-
- (Object) block
blocks an Action, so it won't be run.
- - (Boolean) blocked?
-
- (Object) configure(opts = {}, &block)
Provides for on-the-fly reconfiguration of the Actions Accept the same arguments as the constructor.
-
- (Action) initialize(options = {}, &block)
constructor
- Options are: start
-
Time when the Action should be run for the first time.
-
- (Object) reschedule(period, &block)
modify the Action period.
-
- (Object) run(now = Time.now)
calls the Action callback, resets .next to the Time of the next call, if the Action is repeatable.
-
- (Object) unblock
unblocks a blocked Action.
Constructor Details
- (Action) initialize(options = {}, &block)
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.
43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File '/home/apoc/projects/ruby/rbot/lib/rbot/timer.rb', line 43 def initialize( = {}, &block) opts = { :period => 1, :blocked => false, :args => [], :repeat => false }.merge() @block = nil debug("adding timer #{self} :period => #{opts[:period]}, :repeat => #{opts[:repeat].inspect}") self.configure(opts, &block) debug("added #{self}") end |
Instance Attribute Details
- (Object) next
Time when the Action should be called next
27 28 29 |
# File '/home/apoc/projects/ruby/rbot/lib/rbot/timer.rb', line 27 def next @next end |
Instance Method Details
- (Object) block
blocks an Action, so it won't be run
88 89 90 |
# File '/home/apoc/projects/ruby/rbot/lib/rbot/timer.rb', line 88 def block @blocked = true end |
- (Boolean) blocked?
97 98 99 |
# File '/home/apoc/projects/ruby/rbot/lib/rbot/timer.rb', line 97 def blocked? @blocked end |
- (Object) configure(opts = {}, &block)
Provides for on-the-fly reconfiguration of the Actions Accept the same arguments as the constructor
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File '/home/apoc/projects/ruby/rbot/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 |
- (Object) reschedule(period, &block)
modify the Action period
83 84 85 |
# File '/home/apoc/projects/ruby/rbot/lib/rbot/timer.rb', line 83 def reschedule(period, &block) self.configure(:period => period, &block) end |
- (Object) run(now = Time.now)
calls the Action callback, resets .next to the Time of the next call, if the Action is repeatable.
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File '/home/apoc/projects/ruby/rbot/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 |
- (Object) unblock
unblocks a blocked Action
93 94 95 |
# File '/home/apoc/projects/ruby/rbot/lib/rbot/timer.rb', line 93 def unblock @blocked = false end |