Class: Servolux::Threaded::ThreadContainer

Inherits:
Struct
  • Object
show all
Defined in:
lib/servolux/threaded.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#continue_on_errorObject

Returns the value of attribute continue_on_error

Returns:

  • (Object)

    the current value of continue_on_error



206
207
208
# File 'lib/servolux/threaded.rb', line 206

def continue_on_error
  @continue_on_error
end

#intervalObject

Returns the value of attribute interval

Returns:

  • (Object)

    the current value of interval



206
207
208
# File 'lib/servolux/threaded.rb', line 206

def interval
  @interval
end

#iterationsObject

Returns the value of attribute iterations

Returns:

  • (Object)

    the current value of iterations



206
207
208
# File 'lib/servolux/threaded.rb', line 206

def iterations
  @iterations
end

#maximum_iterationsObject

Returns the value of attribute maximum_iterations

Returns:

  • (Object)

    the current value of maximum_iterations



206
207
208
# File 'lib/servolux/threaded.rb', line 206

def maximum_iterations
  @maximum_iterations
end

#runningObject Also known as: running?

Returns the value of attribute running

Returns:

  • (Object)

    the current value of running



206
207
208
# File 'lib/servolux/threaded.rb', line 206

def running
  @running
end

#threadObject

Returns the value of attribute thread

Returns:

  • (Object)

    the current value of thread



206
207
208
# File 'lib/servolux/threaded.rb', line 206

def thread
  @thread
end

Instance Method Details

#finished_iterations?Boolean

Returns:

  • (Boolean)


249
250
251
252
# File 'lib/servolux/threaded.rb', line 249

def finished_iterations?
  return true if maximum_iterations and (iterations >= maximum_iterations)
  return false
end

#join(limit = nil) ⇒ Object



244
245
246
247
# File 'lib/servolux/threaded.rb', line 244

def join( limit = nil )
  return if thread.nil?
  limit ? thread.join(limit) : thread.join
end

#run(threaded) ⇒ Object



219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
# File 'lib/servolux/threaded.rb', line 219

def run( threaded )
  loop {
    begin
      sleep interval if running?
      break unless running?
      threaded.run
      self.iterations += 1
      break if finished_iterations?
    rescue SystemExit; raise
    rescue Exception => err
      if continue_on_error
        threaded.logger.error err
      else
        threaded.logger.fatal err
        raise err
      end
    end
  }
ensure
  if threaded.respond_to?(:after_stopping) and !self.running
    threaded.after_stopping
  end
  self.running = false
end

#start(threaded) ⇒ Object



207
208
209
210
211
212
# File 'lib/servolux/threaded.rb', line 207

def start( threaded )
  self.running = true
  self.iterations = 0
  self.thread = Thread.new { run threaded }
  Thread.pass
end

#stopObject



214
215
216
217
# File 'lib/servolux/threaded.rb', line 214

def stop
  self.running = false
  thread.wakeup
end