-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmain_container.go
96 lines (87 loc) · 2.11 KB
/
main_container.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
//go:build container
// +build container
package main
import (
"context"
"flag"
"fmt"
"os"
"os/signal"
"syscall"
"github.com/kubemq-io/kubemq-targets/api"
"github.com/kubemq-io/kubemq-targets/binding"
"github.com/kubemq-io/kubemq-targets/config"
"github.com/kubemq-io/kubemq-targets/pkg/logger"
)
var version = ""
var (
log *logger.Logger
configFile = flag.String("config", "config.yaml", "set config file name")
)
func run() error {
gracefulShutdown := make(chan os.Signal, 1)
signal.Notify(gracefulShutdown, syscall.SIGTERM)
signal.Notify(gracefulShutdown, syscall.SIGINT)
signal.Notify(gracefulShutdown, syscall.SIGQUIT)
configCh := make(chan *config.Config)
cfg, err := config.Load(configCh)
if err != nil {
return err
}
err = cfg.Validate()
if err != nil {
return err
}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
bindingsService, err := binding.New()
if err != nil {
return err
}
err = bindingsService.Start(ctx, cfg)
if err != nil {
return err
}
apiServer, err := api.Start(ctx, cfg.ApiPort, bindingsService)
if err != nil {
return err
}
for {
select {
case newConfig := <-configCh:
err = newConfig.Validate()
if err != nil {
return fmt.Errorf("error on validation new config file: %s", err.Error())
}
bindingsService.Stop()
err = bindingsService.Start(ctx, newConfig)
if err != nil {
return fmt.Errorf("error on restarting service with new config file: %s", err.Error())
}
if apiServer != nil {
err = apiServer.Stop()
if err != nil {
return fmt.Errorf("error on shutdown api server: %s", err.Error())
}
}
apiServer, err = api.Start(ctx, newConfig.ApiPort, bindingsService)
if err != nil {
return fmt.Errorf("error on start api server: %s", err.Error())
}
case <-gracefulShutdown:
_ = apiServer.Stop()
bindingsService.Stop()
return nil
}
}
}
func main() {
log = logger.NewLogger("kubemq-targets")
flag.Parse()
config.SetConfigFile(*configFile)
log.Infof("starting kubemq targets connectors version: %s", version)
if err := run(); err != nil {
log.Error(err)
os.Exit(1)
}
}