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



227
228
229
# File 'lib/servolux/threaded.rb', line 227

def continue_on_error
  @continue_on_error
end

#intervalObject

Returns the value of attribute interval

Returns:

  • (Object)

    the current value of interval



227
228
229
# File 'lib/servolux/threaded.rb', line 227

def interval
  @interval
end

#iterationsObject

Returns the value of attribute iterations

Returns:

  • (Object)

    the current value of iterations



227
228
229
# File 'lib/servolux/threaded.rb', line 227

def iterations
  @iterations
end

#maximum_iterationsObject

Returns the value of attribute maximum_iterations

Returns:

  • (Object)

    the current value of maximum_iterations



227
228
229
# File 'lib/servolux/threaded.rb', line 227

def maximum_iterations
  @maximum_iterations
end

#runningObject Also known as: running?

Returns the value of attribute running

Returns:

  • (Object)

    the current value of running



227
228
229
# File 'lib/servolux/threaded.rb', line 227

def running
  @running
end

#threadObject

Returns the value of attribute thread

Returns:

  • (Object)

    the current value of thread



227
228
229
# File 'lib/servolux/threaded.rb', line 227

def thread
  @thread
end

#use_strict_intervalObject

Returns the value of attribute use_strict_interval

Returns:

  • (Object)

    the current value of use_strict_interval



227
228
229
# File 'lib/servolux/threaded.rb', line 227

def use_strict_interval
  @use_strict_interval
end

Instance Method Details

#finished_iterations?Boolean

Returns:

  • (Boolean)


278
279
280
281
# File 'lib/servolux/threaded.rb', line 278

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

#join(limit = nil) ⇒ Object



273
274
275
276
# File 'lib/servolux/threaded.rb', line 273

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

#mark_timeObject

Mark the start time of the run loop.



287
288
289
# File 'lib/servolux/threaded.rb', line 287

def mark_time
  @mark = Time.now if use_strict_interval
end

#run(threaded) ⇒ Object



240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
# File 'lib/servolux/threaded.rb', line 240

def run( threaded )
  loop do
    begin
      mark_time
      break unless running?
      threaded.run

      if maximum_iterations
        self.iterations += 1
        if finished_iterations?
          self.running = false
          break
        end
      end

      sleep if running?
    rescue SystemExit; raise
    rescue Exception => err
      if continue_on_error
        threaded.logger.error err
      else
        threaded.logger.fatal err
        raise err
      end
    end
  end
ensure
  if threaded.respond_to?(:after_stopping) and !self.running
    threaded.after_stopping
  end
  self.running = false
end

#sleepObject

Sleep for "interval" seconds adjusting for the run time of the "run" method if the "use_strict_interval" flag is set. If the run time of the "run" method exceeds our sleep "interval", then log a warning and just use the interval as normal for this sleep period.



296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
# File 'lib/servolux/threaded.rb', line 296

def sleep
  time_to_sleep = interval

  if use_strict_interval and interval > 0
    diff = Time.now - @mark
    time_to_sleep = interval - diff

    if time_to_sleep < 0
      time_to_sleep = interval
      logger.warn "Run time [#{diff} s] exceeded strict interval [#{interval} s]"
    end
  end

  ::Kernel.sleep time_to_sleep
end

#start(threaded) ⇒ Object



228
229
230
231
232
233
# File 'lib/servolux/threaded.rb', line 228

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

#stopObject



235
236
237
238
# File 'lib/servolux/threaded.rb', line 235

def stop
  self.running = false
  thread.wakeup if thread.alive?
end