Module: ServiceState

Included in:
FileSystemWatcher
Defined in:
lib/filesystemwatcher/servicestate.rb

Overview

The Runnable module is a generic mixin for including state and status information in a class

Constant Summary collapse

NOT_STARTED =

state constants

0
STARTED =
1
STOPPED =
2
CONFIGURED =
3

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#endTimeObject (readonly)

Returns the value of attribute endTime.



12
13
14
# File 'lib/filesystemwatcher/servicestate.rb', line 12

def endTime
  @endTime
end

#startTimeObject (readonly)

Returns the value of attribute startTime.



12
13
14
# File 'lib/filesystemwatcher/servicestate.rb', line 12

def startTime
  @startTime
end

Instance Method Details

#initializeStateObject

Initialize the state information



15
16
17
18
19
20
21
22
23
# File 'lib/filesystemwatcher/servicestate.rb', line 15

def initializeState()
  @configured = false
  @startTime = 0
  @stopTime = 0
  
  @stateMutex = Mutex.new()
  @stopWhen = nil
  setState(NOT_STARTED)
end

#isConfigured?Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/filesystemwatcher/servicestate.rb', line 53

def isConfigured?
  return @configured
end

#isStarted?Boolean

Returns:

  • (Boolean)


57
58
59
# File 'lib/filesystemwatcher/servicestate.rb', line 57

def isStarted?
  return @state == STARTED
end

#isStopped?Boolean

Returns:

  • (Boolean)


61
62
63
64
65
66
67
68
69
70
# File 'lib/filesystemwatcher/servicestate.rb', line 61

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

#onStateChange(&callbackBlock) ⇒ Object

Set the callback for when someone calls setState. You will be passed the state CONSTANT being set



27
28
29
# File 'lib/filesystemwatcher/servicestate.rb', line 27

def onStateChange(&callbackBlock)
  @stateCallback = callbackBlock
end

#setState(newState) ⇒ Object

All methods, inside this class or not, should use this method to change the state of the JobRunner

Parameters:

  • newState

    The new state value



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/filesystemwatcher/servicestate.rb', line 34

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

#stopWhen(&block) ⇒ Object



72
73
74
# File 'lib/filesystemwatcher/servicestate.rb', line 72

def stopWhen(&block)
  @stopWhen = block
end