-
Notifications
You must be signed in to change notification settings - Fork 6
/
stateful_state_machine.go
56 lines (43 loc) · 1.29 KB
/
stateful_state_machine.go
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
package canary
import (
"fmt"
"time"
"code.cloudfoundry.org/lager"
)
//go:generate stringer -type=State
type State int
const (
NotUnhealthy State = iota
Unhealthy
)
type StatefulStateMachine struct {
State State
OnBecomesUnhealthy func(time.Time)
OnBecomesNotUnhealthy func(time.Time)
Logger lager.Logger
}
func (m *StatefulStateMachine) BecomesUnhealthy(timestamp time.Time) {
if m.State == NotUnhealthy {
m.Logger.Debug("StateMachine Transitioning to unhealthy", lager.Data{"timestamp": timestamp})
m.OnBecomesUnhealthy(timestamp)
}
m.Logger.Debug("StateMachine unhealthy", lager.Data{"timestamp": timestamp})
m.State = Unhealthy
}
func (m *StatefulStateMachine) BecomesNotUnhealthy(timestamp time.Time) {
if m.State == Unhealthy {
m.Logger.Debug("StateMachine Transitioning to not unhealthy", lager.Data{"timestamp": timestamp})
m.OnBecomesNotUnhealthy(timestamp)
}
m.Logger.Debug("StateMachine not unhealthy", lager.Data{"timestamp": timestamp})
m.State = NotUnhealthy
}
func (m *StatefulStateMachine) RemainsInSameState(timestamp time.Time) {
m.Logger.Debug(
"StateMachine remaining in the same state",
lager.Data{"timestamp": timestamp, "state": fmt.Sprint(m.State)},
)
}
func (m *StatefulStateMachine) GetState() State {
return m.State
}