This repository has been archived by the owner on Jul 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcluster_state.go
62 lines (54 loc) · 1.45 KB
/
cluster_state.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
57
58
59
60
61
62
package main
import (
log "github.com/Sirupsen/logrus"
"github.com/looplab/fsm"
)
const (
// CURRENT is name of current state event
CURRENT = "current"
// WARNING is name of warning state event
//WARNING = "warning"
// FATAL is name of fatal state event
FATAL = "fatal"
// RESOLV is name of resolv state event
RESOLV = "resolv"
)
// Health is cluster health fsm
type Health struct {
To string
FSM *fsm.FSM
}
// State is current cluster state information
type State struct {
Success bool `json:"success"`
Request string `json:"request"`
Current string `json:"current"`
Last string `json:"last"`
Message string `json:"message"`
}
// NewHealth creates a instance of cluster fsm
func NewHealth(to string) *Health {
h := &Health{
To: to,
}
h.FSM = fsm.NewFSM("red",
fsm.Events{
{Name: "warning", Src: []string{"green"}, Dst: "orange"},
{Name: "warning", Src: []string{"orange"}, Dst: "orange"},
{Name: "resolv", Src: []string{"red"}, Dst: "orange"},
{Name: "resolv", Src: []string{"orange"}, Dst: "green"},
{Name: "fatal", Src: []string{"green"}, Dst: "red"},
{Name: "fatal", Src: []string{"orange"}, Dst: "red"},
},
fsm.Callbacks{
"enter_state": func(e *fsm.Event) { h.enterState(e) },
},
)
return h
}
func (h *Health) enterState(e *fsm.Event) {
log.Infof("The health of %s is %s\n", h.To, e.Dst)
}
func (h *Health) doDegrade(e *fsm.Event) {
log.Infof("Degrade from %s to %s", e.FSM.Current(), e.FSM.Can("fatal"))
}