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

#_zoidberg_thread, #async, #defer, included, #sleep

Constructor Details

#initializeself

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

#alerterIO (readonly)

Returns:

  • (IO)


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

def alerter
  @alerter
end

#pausedTrueClass, FalseClass (readonly)

Returns timer is paused.

Returns:

  • (TrueClass, FalseClass)

    timer is paused



79
80
81
# File 'lib/zoidberg/timer.rb', line 79

def paused
  @paused
end

#to_runArray<Action> (readonly)

Returns items to run.

Returns:

  • (Array<Action>)

    items to run



77
78
79
# File 'lib/zoidberg/timer.rb', line 77

def to_run
  @to_run
end

#wakerIO (readonly)

Returns:

  • (IO)


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

Parameters:

  • interval (Numeric)

Yields:

  • action to run

Returns:



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

#cancelself

Remove all actions from the timer

Returns:

  • (self)


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

Parameters:

  • interval (Numeric)

Yields:

  • action to run

Returns:



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

Returns interval to next action.

Returns:

  • (Numeric, NilClass)

    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

#pauseself

Pause the timer to prevent any actions from being run

Returns:

  • (self)


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

Parameters:

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

    wakeup the timer thread

Returns:

  • (self)


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

#resumeself

Resume a paused timer

Returns:

  • (self)


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_readyself

Run any actions that are ready

Returns:

  • (self)


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

#terminateObject

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