/
servicestate.rb
75 lines (64 loc) · 1.55 KB
/
servicestate.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
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