Class: Zoidberg::Timer

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Methods included from SoftShell

#async, #defer, included, #sleep

Constructor Details

#initializeself

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

#alerterIO (readonly)

Returns:

  • (IO)


88
89
90
# File 'lib/zoidberg/timer.rb', line 88

def alerter
  @alerter
end

#pausedTrueClass, FalseClass (readonly)

Returns timer is paused.

Returns:

  • (TrueClass, FalseClass)

    timer is paused



84
85
86
# File 'lib/zoidberg/timer.rb', line 84

def paused
  @paused
end

#to_runArray<Action> (readonly)

Returns items to run.

Returns:

  • (Array<Action>)

    items to run



82
83
84
# File 'lib/zoidberg/timer.rb', line 82

def to_run
  @to_run
end

#wakerIO (readonly)

Returns:

  • (IO)


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

Parameters:

  • interval (Numeric)

Yields:

  • action to run

Returns:



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

#cancelself

Remove all actions from the timer

Returns:

  • (self)


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

Parameters:

  • interval (Numeric)

Yields:

  • action to run

Returns:



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_intervalNumeric, NilClass

Returns interval to next action.

Returns:

  • (Numeric, NilClass)

    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

#pauseself

Pause the timer to prevent any actions from being run

Returns:

  • (self)


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

Parameters:

  • wakeup (TrueClass, FalseClass) (defaults to: true)

    wakeup the timer thread

Returns:

  • (self)


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

#resumeself

Resume a paused timer

Returns:

  • (self)


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_readyself

Run any actions that are ready

Returns:

  • (self)


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

#terminateObject

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