forked from appcanary/agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
93 lines (70 loc) · 1.87 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
90
91
92
93
package main
import (
"flag"
"fmt"
"os"
"time"
"github.com/stateio/canary-agent/agent"
"github.com/stateio/canary-agent/agent/umwelten"
)
var CanaryVersion string
var env = umwelten.Fetch()
var log = umwelten.Log
func usage() {
fmt.Fprintf(os.Stderr, "Usage: canary-agent [OPTION]\n")
flag.PrintDefaults()
}
func main() {
umwelten.Init(os.Getenv("CANARY_ENV"))
flag.Usage = usage
flag.StringVar(&env.ConfFile, "conf", env.ConfFile, "Set the config file")
flag.StringVar(&env.VarFile, "server", env.VarFile, "Set the server file")
if !env.Prod {
flag.StringVar(&env.BaseUrl, "url", env.BaseUrl, "Set the endpoint")
}
version := flag.Bool("version", false, "Display version information")
flag.Parse()
if *version {
fmt.Println(CanaryVersion)
os.Exit(0)
}
done := make(chan os.Signal, 1)
fmt.Println(env.Logo)
// slurp env, instantiate agent
conf := agent.NewConfFromEnv()
a := agent.NewAgent(conf)
// we prob can't reliably fingerprint servers.
// so instead, we assign a uuid by registering
if a.FirstRun() {
log.Debug("Found no server config. Let's register!")
for err := a.RegisterServer(); err != nil; {
// we don't need to wait here because of the backoff
// exponential decay library; by the time we hit this
// point we've been trying for about, what, an hour?
log.Info("Register server error: %s", err)
err = a.RegisterServer()
}
}
// Add hooks to files, and push them over
// whenever they change
a.StartWatching()
// send a heartbeat every ~60min, forever
go func() {
tick := time.Tick(env.HeartbeatDuration)
for {
err := a.Heartbeat()
if err != nil {
log.Info("<3 error: %s", err)
}
<-tick
}
}()
defer a.CloseWatches()
// Close the logfile when we exit
if env.LogFile != nil {
defer env.LogFile.Close()
}
// wait for the right signal
// signal.Notify(done, os.Interrupt, os.Kill)
<-done
}