-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdaemon.go
95 lines (82 loc) · 1.74 KB
/
daemon.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package main
import (
"fmt"
"log"
"strings"
"time"
)
const (
timeoutSeconds = 30
)
type State string
const (
Error = "Error"
Live = "Live"
Unknown = "Unknown"
Fatal = "Fatal"
Unitialized = "Unitialized"
)
type Daemon struct {
config *DaemonConfig
}
func NewDaemon(config *DaemonConfig) *Daemon {
daemon := &Daemon{
config: config,
}
disable_checkers := config.getOrDefault("checkers", "disable", []string{}).([]string)
if len(disable_checkers) > 0 {
log.Println(fmt.Sprintf("Disable checkers: %s", strings.Join(disable_checkers, ",")))
for _, disable_checker := range disable_checkers {
delete(checkers, disable_checker)
}
}
for name, checker := range checkers {
err := checker.initialize(daemon.config)
if err != nil {
log.Fatalln(err.Error())
}
log.Println(fmt.Sprintf("Checker: %s\t begins to initialize", name))
go checker.start()
}
return daemon
}
func (*Daemon) run() error {
return nil
}
func (daemon *Daemon) states() map[string]interface{} {
if len(checkers) == 0 {
return map[string]interface{}{}
}
states := make(map[string]interface{})
ch := make(chan Info)
ok := make(chan struct{})
for name, checker := range checkers {
info := UnknownInfo(name)
states[name] = info.toMap()
go func(name string, checker Checker) {
ch <- checker.info()
}(name, checker)
}
// 检查所有checker是否已返回info
go func() {
count := 0
for {
select {
case info := <-ch:
count += 1
states[info.name] = info.toMap()
if count == len(checkers) {
close(ok)
break
}
}
}
}()
// 超时机制
select {
case <-time.After(time.Second * timeoutSeconds):
log.Println(fmt.Sprintf("Timeout while trying to get infos"))
case <-ok:
}
return states
}