-
Notifications
You must be signed in to change notification settings - Fork 2
/
oogway.go
97 lines (84 loc) · 2.06 KB
/
oogway.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
96
97
package main
import (
"errors"
"sync"
"time"
log "github.com/sirupsen/logrus"
"gopkg.in/yaml.v2"
)
// oogway masters your checks
type oogway struct {
CheckInterval time.Duration
ChecksDir string
ChecksExtension string
checks map[string]*check
checksLock *sync.Mutex
}
// loadChecks reads in all config files and reloads the configuration
func (o *oogway) loadChecks() error {
checks := make(map[string]*instructions)
checksInFiles := combineConfigFiles(o.ChecksDir, o.ChecksExtension)
o.checksLock.Lock()
defer o.checksLock.Unlock()
yamlError := yaml.Unmarshal(checksInFiles, &checks)
if yamlError != nil {
log.Error("Unable to parse yaml")
log.Error(yamlError.Error())
return errors.New("Unable to parse yaml")
}
// delete checks that are no longer needed
for preExistingCheck := range o.checks {
del := true
// go through the new checks, make sure it exists
for newCheck := range checks {
if newCheck == preExistingCheck {
del = false
break
}
}
// do we need to delete it?
if del {
delete(o.checks, preExistingCheck)
log.WithFields(log.Fields{
"name": preExistingCheck,
}).Error("Removing check")
}
}
for name, li := range checks {
// does this check exist?
if _, exists := o.checks[name]; !exists {
// ok. lets add it
o.checks[name] = &check{
Name: name,
Instructions: li,
ExecLock: &sync.Mutex{},
}
log.WithFields(log.Fields{
"name": name,
}).Info("A new check was found")
} else {
// kk, something was updated ...
if o.checks[name].id() != li.id() {
o.checks[name].Instructions = li
// reset the logic ...
o.checks[name].Attempts = 0
log.WithFields(log.Fields{
"name": name,
}).Info("Check was modified")
}
}
}
return nil
}
// instruct is the central hub for dispatching checks
func (o *oogway) instruct() {
for {
o.loadChecks()
for _, check := range o.checks {
if check.LastChecked.Add(check.every()).Before(time.Now()) && !check.isMuted() {
check.check()
}
}
<-time.After(o.CheckInterval)
}
}