Sha256: 2044e772febdfeafa6a916a0631a03a156b32d558de1ad93087b9136fe7e2584

Contents?: true

Size: 1.55 KB

Versions: 5

Compression:

Stored size: 1.55 KB

Contents

require 'thread'

# The Runnable module is a generic mixin for including state and
# status information in a class
module ServiceState
  # state constants
  NOT_STARTED = 0
  STARTED = 1
  STOPPED = 2
  CONFIGURED = 3
  
  attr_reader :startTime, :endTime
  
  # Initialize the state information
  def initializeState()
    @configured = false
    @startTime = 0
    @stopTime = 0
    
    @stateMutex = Mutex.new()
    @stopWhen = nil
    setState(NOT_STARTED)
  end
  
  # Set the callback for when someone calls setState. You
  # will be passed the state CONSTANT being set
  def onStateChange(&callbackBlock)
    @stateCallback = callbackBlock
  end
  
  # All methods, inside this class or not, should use this
  # method to change the state of the JobRunner
  # @param newState The new state value
  def setState(newState)
    @stateMutex.synchronize {
      if newState == CONFIGURED then
	@configured = true
      else
	@state = newState
	if isStarted? then
	  @startTime = Time.now()
	elsif isStopped?
	  @stopTime = Time.now()
	end
      end
    }
    
    if defined?(@stateCallback) then
      @stateCallback.call(newState)
    end
  end
  
  def isConfigured?
    return @configured
  end

  def isStarted?
    return @state == STARTED
  end

  def isStopped?
    if @state == STOPPED then
      return true
    elsif @stopWhen && @stopWhen.call() then
      setState(STOPPED)
      return true
    else
      return false
    end
  end   

  def stopWhen(&block)
    @stopWhen = block
  end
end

Version data entries

5 entries across 5 versions & 2 rubygems

Version Path
chisel-0.0.4 lib/filesystemwatcher/servicestate.rb
chisel-0.0.3 lib/filesystemwatcher/servicestate.rb
sweet-lang-0.3.7 lib/servicestate.rb
sweet-lang-0.2.2 lib/servicestate.rb
chisel-0.0.2 lib/filesystemwatcher/servicestate.rb