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
#_zoidberg_thread, #async, #defer, included, #sleep
Constructor Details
#initialize ⇒ self
Create a new timer
88 89 90 91 92 93 |
# File 'lib/zoidberg/timer.rb', line 88 def initialize @to_run = [] @paused = false @alerter, @waker = IO.pipe @thread = Thread.new{ run! } end |
Instance Attribute Details
#alerter ⇒ IO (readonly)
83 84 85 |
# File 'lib/zoidberg/timer.rb', line 83 def alerter @alerter end |
#paused ⇒ TrueClass, FalseClass (readonly)
Returns timer is paused.
79 80 81 |
# File 'lib/zoidberg/timer.rb', line 79 def paused @paused end |
#to_run ⇒ Array<Action> (readonly)
Returns items to run.
77 78 79 |
# File 'lib/zoidberg/timer.rb', line 77 def to_run @to_run end |
#waker ⇒ IO (readonly)
81 82 83 |
# File 'lib/zoidberg/timer.rb', line 81 def waker @waker end |
Instance Method Details
#after(interval) { ... } ⇒ Action
Run action after given interval
117 118 119 120 121 122 123 124 125 |
# File 'lib/zoidberg/timer.rb', line 117 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
152 153 154 155 156 |
# File 'lib/zoidberg/timer.rb', line 152 def cancel to_run.clear reset current_self end |
#every(interval) { ... } ⇒ Action
Run recurring action at given interval
100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/zoidberg/timer.rb', line 100 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.
173 174 175 176 177 178 179 |
# File 'lib/zoidberg/timer.rb', line 173 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
130 131 132 133 134 135 136 |
# File 'lib/zoidberg/timer.rb', line 130 def pause unless(@paused) @paused = true reset end current_self end |
#reset(wakeup = true) ⇒ self
Reset the timer
162 163 164 165 166 167 168 169 170 |
# File 'lib/zoidberg/timer.rb', line 162 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
141 142 143 144 145 146 147 |
# File 'lib/zoidberg/timer.rb', line 141 def resume if(@paused) @paused = false reset end current_self end |
#run_ready ⇒ self
Run any actions that are ready
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 |
# File 'lib/zoidberg/timer.rb', line 184 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
205 206 207 |
# File 'lib/zoidberg/timer.rb', line 205 def terminate @thread.raise Zoidberg::DeadException.new('Instance in terminated state', object_id) end |