Class: Zoidberg::Timer
- Inherits:
-
Object
- Object
- Zoidberg::Timer
- Includes:
- SoftShell
- Defined in:
- lib/zoidberg/timer.rb
Overview
Simple timer class
Defined Under Namespace
Classes: Action
Constant Summary collapse
- WAKEUP =
Wakeup string
"WAKEUP\n"
Instance Attribute Summary collapse
- #alerter ⇒ IO readonly
-
#paused ⇒ TrueClass, FalseClass
readonly
Timer is paused.
-
#to_run ⇒ Array<Action>
readonly
Items to run.
- #waker ⇒ IO readonly
Instance Method Summary collapse
-
#after(interval) { ... } ⇒ Action
Run action after given interval.
-
#cancel ⇒ self
Remove all actions from the timer.
-
#every(interval) { ... } ⇒ Action
Run recurring action at given interval.
-
#initialize ⇒ self
constructor
Create a new timer.
-
#next_interval ⇒ Numeric, NilClass
Interval to next action.
-
#pause ⇒ self
Pause the timer to prevent any actions from being run.
-
#reset(wakeup = true) ⇒ self
Reset the timer.
-
#resume ⇒ self
Resume a paused timer.
-
#run_ready ⇒ self
Run any actions that are ready.
-
#terminate ⇒ Object
Clean up timer thread.
Methods included from SoftShell
#async, #defer, included, #sleep
Constructor Details
#initialize ⇒ self
Create a new timer
93 94 95 96 97 98 |
# File 'lib/zoidberg/timer.rb', line 93 def initialize @to_run = [] @paused = false @alerter, @waker = IO.pipe @thread = Thread.new{ run! } end |
Instance Attribute Details
#alerter ⇒ IO (readonly)
88 89 90 |
# File 'lib/zoidberg/timer.rb', line 88 def alerter @alerter end |
#paused ⇒ TrueClass, FalseClass (readonly)
Returns timer is paused.
84 85 86 |
# File 'lib/zoidberg/timer.rb', line 84 def paused @paused end |
#to_run ⇒ Array<Action> (readonly)
Returns items to run.
82 83 84 |
# File 'lib/zoidberg/timer.rb', line 82 def to_run @to_run end |
#waker ⇒ IO (readonly)
86 87 88 |
# File 'lib/zoidberg/timer.rb', line 86 def waker @waker end |
Instance Method Details
#after(interval) { ... } ⇒ Action
Run action after given interval
122 123 124 125 126 127 128 129 130 |
# File 'lib/zoidberg/timer.rb', line 122 def after(interval, &block) action = Action.new( {:interval => interval}, &block ) to_run.push(action) reset action end |
#cancel ⇒ self
Remove all actions from the timer
157 158 159 160 161 |
# File 'lib/zoidberg/timer.rb', line 157 def cancel to_run.clear reset current_self end |
#every(interval) { ... } ⇒ Action
Run recurring action at given interval
105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/zoidberg/timer.rb', line 105 def every(interval, &block) action = Action.new({ :interval => interval, :recur => true }, &block ) to_run.push(action) reset action end |
#next_interval ⇒ Numeric, NilClass
Returns interval to next action.
178 179 180 181 182 183 184 |
# File 'lib/zoidberg/timer.rb', line 178 def next_interval unless(to_run.empty? || paused) item = to_run.first result = (item.last_run + item.interval) - Time.now.to_f result < 0 ? 0 : result end end |
#pause ⇒ self
Pause the timer to prevent any actions from being run
135 136 137 138 139 140 141 |
# File 'lib/zoidberg/timer.rb', line 135 def pause unless(@paused) @paused = true reset end current_self end |
#reset(wakeup = true) ⇒ self
Reset the timer
167 168 169 170 171 172 173 174 175 |
# File 'lib/zoidberg/timer.rb', line 167 def reset(wakeup=true) to_run.sort_by! do |item| (item.interval + item.last_run) - Time.now.to_f end if(wakeup) waker.write WAKEUP end current_self end |
#resume ⇒ self
Resume a paused timer
146 147 148 149 150 151 152 |
# File 'lib/zoidberg/timer.rb', line 146 def resume if(@paused) @paused = false reset end current_self end |
#run_ready ⇒ self
Run any actions that are ready
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 |
# File 'lib/zoidberg/timer.rb', line 189 def run_ready items = to_run.find_all(&:ready?) to_run.delete_if do |item| items.include?(item) end items.map do |item| begin item.run! unless item.cancelled? rescue DeadException item.cancel rescue => e Zoidberg.logger.error "<#{self}> Timed action generated an error: #{e.class.name} - #{e}" end item if item.recur end.compact.each do |item| to_run << item end current_self end |
#terminate ⇒ Object
Clean up timer thread
210 211 212 |
# File 'lib/zoidberg/timer.rb', line 210 def terminate @thread.raise Zoidberg::DeadException.new('Instance in terminated state', object_id) end |