This repository has been archived by the owner on Mar 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.go
89 lines (76 loc) · 1.9 KB
/
main.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
package main
import (
"io/ioutil"
"os"
"path/filepath"
"time"
"gopkg.in/yaml.v2"
"github.com/levenlabs/go-llog"
"github.com/levenlabs/thumper/config"
)
func main() {
if config.AlertFileDir == "" {
llog.Fatal("--alerts must be set")
}
fstat, err := os.Stat(config.AlertFileDir)
if err != nil {
llog.Fatal("failed getting alert definitions", llog.KV{"err": err})
}
files := make([]string, 0, 10)
if !fstat.IsDir() {
files = append(files, config.AlertFileDir)
} else {
fileInfos, err := ioutil.ReadDir(config.AlertFileDir)
if err != nil {
llog.Fatal("failed getting alert dir info", llog.KV{"err": err})
}
for _, fi := range fileInfos {
if !fi.IsDir() {
files = append(files, filepath.Join(config.AlertFileDir, fi.Name()))
}
}
}
for _, file := range files {
kv := llog.KV{"file": file}
var alerts []Alert
b, err := ioutil.ReadFile(file)
if err != nil {
kv["err"] = err
llog.Fatal("failed to read alert config", kv)
}
if err := yaml.Unmarshal(b, &alerts); err != nil {
kv["err"] = err
llog.Fatal("failed to parse yaml", kv)
}
for i := range alerts {
kv["name"] = alerts[i].Name
llog.Info("initializing alert", kv)
if err := alerts[i].Init(); err != nil {
kv["err"] = err
llog.Fatal("failed to initialize alert", kv)
}
if config.ForceRun != "" && config.ForceRun == alerts[i].Name {
alerts[i].Run()
time.Sleep(250 * time.Millisecond) // allow time for logs to print
return
} else if config.ForceRun == "" {
go alertSpin(alerts[i])
}
}
}
// If we made it this far with --force-run set to something it means an
// alert by that name was never found, so we should error
if config.ForceRun != "" {
llog.Error("could not find alert with name given by --force-run")
os.Exit(1)
}
select {}
}
func alertSpin(a Alert) {
for {
now := time.Now()
next := a.cron.Next(now)
time.Sleep(next.Sub(now))
go a.Run()
}
}