-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
102 lines (93 loc) · 2.35 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
94
95
96
97
98
99
100
101
102
// Copyright 2016 bs authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
"flag"
"fmt"
"os"
"os/signal"
"syscall"
"github.com/google/gops/agent"
"github.com/tsuru/bs/bslog"
"github.com/tsuru/bs/config"
"github.com/tsuru/bs/log"
"github.com/tsuru/bs/metric"
_ "github.com/tsuru/bs/metric/logstash"
"github.com/tsuru/bs/status"
)
const (
version = "v1.10-rc5"
)
var printVersion bool
type StopWaiter interface {
Stop()
Wait()
}
func init() {
flag.BoolVar(&printVersion, "version", false, "Print version and exit")
}
func startSignalHandler(callback func(os.Signal), signals ...os.Signal) {
sigChan := make(chan os.Signal, 4)
go func() {
if signal, ok := <-sigChan; ok {
callback(signal)
}
}()
signal.Notify(sigChan, signals...)
}
func main() {
err := agent.Listen(&agent.Options{
NoShutdownCleanup: true,
})
if err != nil {
bslog.Fatalf("Unable to initialize gops agent: %s\n", err)
}
defer agent.Close()
flag.Parse()
if printVersion {
fmt.Printf("bs version %s\n", version)
return
}
lf := log.LogForwarder{
BindAddress: config.Config.SyslogListenAddress,
DockerEndpoint: config.Config.DockerEndpoint,
EnabledBackends: config.Config.LogBackends,
}
err = lf.Start()
if err != nil {
bslog.Fatalf("Unable to initialize log forwarder: %s\n", err)
}
mRunner := metric.NewRunner(config.Config.DockerEndpoint, config.Config.MetricsInterval,
config.Config.MetricsBackend)
err = mRunner.Start()
if err != nil {
bslog.Warnf("Unable to initialize metrics runner: %s\n", err)
}
reporter, err := status.NewReporter(&status.ReporterConfig{
TsuruEndpoint: config.Config.TsuruEndpoint,
TsuruToken: config.Config.TsuruToken,
DockerEndpoint: config.Config.DockerEndpoint,
Interval: config.Config.StatusInterval,
})
if err != nil {
bslog.Warnf("Unable to initialize status reporter: %s\n", err)
}
monitorEl := []StopWaiter{&lf, mRunner}
if reporter != nil {
monitorEl = append(monitorEl, reporter)
}
var signaled bool
startSignalHandler(func(signal os.Signal) {
signaled = true
for _, m := range monitorEl {
go m.Stop()
}
}, os.Interrupt, syscall.SIGTERM, syscall.SIGQUIT)
for _, m := range monitorEl {
m.Wait()
}
if !signaled {
bslog.Fatalf("Exiting bs because no service could be initialized.")
}
}