/
main.go
66 lines (57 loc) · 1.28 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
package main
import (
"bytes"
"context"
"flag"
"io/ioutil"
"os"
"os/signal"
"sync"
"github.com/hashicorp/hcl/hcl/ast"
"github.com/sirupsen/logrus"
)
const rabbitmqConfKey = "rabbitmq"
var (
configPath = flag.String("config", "./spray-rabbit.hcl", "Path to the configuration")
l = logrus.New()
globalConfig *ast.ObjectList
)
func main() {
flag.Parse()
l.Level = logrus.InfoLevel
confBytes, err := ioutil.ReadFile(*configPath)
if err != nil {
l.Fatalf("Coudn't read configuration from %s", *configPath)
}
globalConfig, err = loadConfiguration(bytes.NewBuffer(confBytes))
if err != nil {
l.Fatalln("Coudn't load configuration")
}
run()
}
func run() {
wg := new(sync.WaitGroup)
inputs, err := getRmqs(globalConfig)
if err != nil {
l.Errorf("Coudn't start inputs. Err: '%s'", err)
}
ctx := context.Background()
ctx, cancel := context.WithCancel(ctx)
err = sprays.initSprays(ctx, globalConfig)
if err != nil {
l.Fatalf("Fail to init sprays. Err: '%s'", err)
}
for _, in := range inputs {
wg.Add(1)
go in.run(ctx, wg)
}
go cancelIfSIGINTR(cancel)
wg.Wait()
}
func cancelIfSIGINTR(cancel context.CancelFunc) {
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
<-c
l.Infoln("SIGINT Retrieved... Gracefully shutdown... ")
cancel()
}