module Servolux::Threaded

Synopsis

The Threaded module is used to perform some activity at a specified interval.

Details

Sometimes it is useful for an object to have its own thread of execution to perform a task at a recurring interval. The Threaded module encapsulates this functionality so you don’t have to write it yourself. It can be used with any object that responds to the run method.

The threaded object is run by calling the start method. This will create a new thread that will invoke the run method at the desired interval. Just before the thread is created the before_starting method will be called (if it is defined by the threaded object). Likewise, after the thread is created the after_starting method will be called (if it is defined by the threaded object).

The threaded object is stopped by calling the stop method. This sets an internal flag and then wakes up the thread. The thread gracefully exits after checking the flag. Like the start method, before and after methods are defined for stopping as well. Just before the thread is stopped the before_stopping method will be called (if it is defined by the threaded object). Likewise, after the thread has died the after_stopping method will be called (if it is defined by the threaded object).

Calling the join method on a threaded object will cause the calling thread to wait until the threaded object has stopped. An optional timeout parameter can be given.

Examples

Take a look at the Servolux::Server class for an example of a threaded object.

Public Instance Methods

continue_on_error=( value ) click to toggle source

Set to true to continue running the threaded object even if an error is raised by the run method. The default behavior is to stop the activity thread when an error is raised by the run method.

A SystemExit will never be caught; it will always cause the Ruby interpreter to exit.

# File lib/servolux/threaded.rb, line 209
def continue_on_error=( value )
  _activity_thread.continue_on_error = (value ? true : false)
end
continue_on_error?() click to toggle source

Returns true if the threaded object should continue running even if an error is raised by the run method. The default is to return false. The threaded object will stop running when an error is raised.

# File lib/servolux/threaded.rb, line 217
def continue_on_error?
  _activity_thread.continue_on_error
end
finished_iterations?() click to toggle source

Returns true if the activity thread has finished its maximum number of iterations or the thread is no longer running. Returns false otherwise.

# File lib/servolux/threaded.rb, line 118
def finished_iterations?
  return true unless _activity_thread.running?
  @_activity_thread.finished_iterations?
end
interval() click to toggle source

Returns the number of seconds to sleep between invocations of the threaded object’s ‘run’ method.

# File lib/servolux/threaded.rb, line 151
def interval
  _activity_thread.interval
end
interval=( value ) click to toggle source

Sets the number of seconds to sleep between invocations of the threaded object’s ‘run’ method.

# File lib/servolux/threaded.rb, line 142
def interval=( value )
  value = Float(value)
  raise ArgumentError, "Sleep interval must be >= 0" unless value >= 0
  _activity_thread.interval = value
end
iterations() click to toggle source

Returns the number of iterations of the threaded object’s ‘run’ method completed thus far.

# File lib/servolux/threaded.rb, line 198
def iterations
  _activity_thread.iterations
end
join( limit = nil ) click to toggle source

If the activity thread is running, the calling thread will suspend execution and run the activity thread. This method does not return until the activity thread is stopped or until limit seconds have passed.

If the activity thread is not running, this method returns immediately with nil.

# File lib/servolux/threaded.rb, line 103
def join( limit = nil )
  _activity_thread.join(limit) ? self : nil
end
maximum_iterations() click to toggle source

Returns the maximum number of invocations of the threaded object’s ‘run’ method

# File lib/servolux/threaded.rb, line 191
def maximum_iterations
  _activity_thread.maximum_iterations
end
maximum_iterations=( value ) click to toggle source

Sets the maximum number of invocations of the threaded object’s ‘run’ method

# File lib/servolux/threaded.rb, line 179
def maximum_iterations=( value )
  unless value.nil?
    value = Integer(value)
    raise ArgumentError, "maximum iterations must be >= 1" unless value >= 1
  end

  _activity_thread.maximum_iterations = value
end
run() click to toggle source

This method will be called by the activity thread at the desired interval. Implementing classes are expect to provide this functionality.

# File lib/servolux/threaded.rb, line 41
def run
  raise NotImplementedError,
       'The run method must be defined by the threaded object.'
end
running?() click to toggle source

Returns true if the activity thread is running. Returns false otherwise.

# File lib/servolux/threaded.rb, line 110
def running?
  _activity_thread.running?
end
status() click to toggle source

Returns the status of threaded object.

'sleep'    : sleeping or waiting on I/O
'run'      : executing
'aborting' : aborting
false      : not running or terminated normally
nil        : terminated with an exception

If this method returns nil, then calling join on the threaded object will cause the exception to be raised in the calling thread.

# File lib/servolux/threaded.rb, line 134
def status
  return false if _activity_thread.thread.nil?
  @_activity_thread.thread.status
end
use_strict_interval() click to toggle source

When true, the activity thread will treat the sleep interval with strict semantics. See the setter method for an in depth explanation.

# File lib/servolux/threaded.rb, line 171
def use_strict_interval
  _activity_thread.use_strict_interval
end
Also aliased as: use_strict_interval?
use_strict_interval=( value ) click to toggle source

Signals the activity thread to treat the sleep interval with strict semantics. The time it takes for the ‘run’ method to execute will be subtracted from the sleep interval.

If the sleep interval is 60 seconds and the ‘run’ method takes 2.2 seconds to execute, then the activity thread will sleep for 57.2 seconds. The subsequent invocation of the ‘run’ will occur as close as possible to 60 seconds after the previous invocation.

# File lib/servolux/threaded.rb, line 164
def use_strict_interval=( value )
  _activity_thread.use_strict_interval = (value ? true : false)
end
use_strict_interval?() click to toggle source
Alias for: use_strict_interval
wait( limit = nil ) click to toggle source

Wait on the activity thread. If the thread is already stopped, this method will return without taking any action. Otherwise, this method does not return until the activity thread has stopped, or a specific number of iterations has passed since this method was called.

# File lib/servolux/threaded.rb, line 86
def wait( limit = nil )
  return self unless _activity_thread.running?
  initial_iterations = @_activity_thread.iterations
  loop {
    break unless @_activity_thread.running?
    break if limit and @_activity_thread.iterations > ( initial_iterations + limit )
    Thread.pass
  }
end