forked from remp2020/remp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
201 lines (168 loc) · 4.47 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
//go:generate goagen bootstrap -d gitlab.com/remp/remp/Beam/go/cmd/tracker/design
package main
import (
"context"
"log"
"net/http"
"os"
"os/signal"
"strings"
"sync"
"syscall"
"time"
"github.com/Shopify/sarama"
"github.com/go-sql-driver/mysql"
"github.com/goadesign/goa"
"github.com/goadesign/goa/middleware"
"github.com/jmoiron/sqlx"
"github.com/joho/godotenv"
"github.com/kelseyhightower/envconfig"
"github.com/pkg/errors"
"gitlab.com/remp/remp/Beam/go/cmd/tracker/app"
"gitlab.com/remp/remp/Beam/go/cmd/tracker/controller"
"gitlab.com/remp/remp/Beam/go/model"
)
func main() {
err := godotenv.Load()
if err != nil {
log.Fatalln(errors.Wrap(err, "unable to load .env file"))
}
var c Config
if err := envconfig.Process("tracker", &c); err != nil {
log.Fatalln(errors.Wrap(err, "unable to process envconfig"))
}
stop := make(chan os.Signal, 3)
signal.Notify(stop, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT)
service := goa.New("tracker")
service.Use(middleware.RequestID())
if c.Debug {
service.Use(middleware.LogRequest(true))
service.Use(middleware.LogResponse())
}
service.Use(middleware.ErrorHandler(service, true))
service.Use(middleware.Recover())
// kafka init
brokerAddrs := strings.Split(c.BrokerAddrs, ",")
for _, addr := range brokerAddrs {
service.LogInfo("connecting to broker", "bind", addr)
}
eventProducer, err := newProducer(brokerAddrs)
if err != nil {
log.Fatalln(err)
}
defer eventProducer.Close()
// DB init
mysqlDBConfig := mysql.Config{
Net: c.MysqlNet,
Addr: c.MysqlAddr,
User: c.MysqlUser,
Passwd: c.MysqlPasswd,
DBName: c.MysqlDBName,
ParseTime: true,
AllowNativePasswords: true,
}
mysqlDB, err := sqlx.Connect("mysql", mysqlDBConfig.FormatDSN())
if err != nil {
log.Fatalln(errors.Wrap(err, "unable to connect to MySQL"))
}
propertyDB := &model.PropertyDB{
MySQL: mysqlDB,
}
entitySchemaDB := &model.EntitySchemaDB{
MySQL: mysqlDB,
}
// server cancellation
var wg sync.WaitGroup
ctx, cancelCtx := context.WithCancel(context.Background())
ticker := time.NewTicker(10 * time.Second)
defer ticker.Stop()
cacheProperties := func() {
if err := propertyDB.Cache(); err != nil {
service.LogError("unable to cache properties", "err", err)
}
}
wg.Add(1)
cacheEntities := func() {
if err := entitySchemaDB.Cache(); err != nil {
service.LogError("unable to cache entity schemas", "err", err)
}
}
wg.Add(1)
cacheProperties()
go func() {
defer wg.Done()
service.LogInfo("starting property caching")
for {
select {
case <-ticker.C:
cacheProperties()
case <-ctx.Done():
service.LogInfo("property caching stopped")
return
}
}
}()
cacheEntities()
go func() {
defer wg.Done()
service.LogInfo("starting entity schemas caching")
for {
select {
case <-ticker.C:
cacheEntities()
case <-ctx.Done():
service.LogInfo("entity schemas caching stopped")
return
}
}
}()
// controllers init
app.MountSwaggerController(service, service.NewController("swagger"))
app.MountTrackController(service, controller.NewTrackController(
service,
eventProducer,
propertyDB,
entitySchemaDB,
))
// server init
service.LogInfo("starting server", "bind", c.TrackerAddr)
srv := &http.Server{
Addr: c.TrackerAddr,
Handler: service.Mux,
}
wg.Add(1)
go func() {
if err := srv.ListenAndServe(); err != nil {
if err != http.ErrServerClosed {
service.LogError("startup", "err", err)
stop <- syscall.SIGQUIT
}
wg.Done()
}
}()
s := <-stop
service.LogInfo("shutting down", "signal", s)
srv.Shutdown(ctx)
cancelCtx()
wg.Wait()
service.LogInfo("bye bye")
}
func newProducer(brokerList []string) (sarama.AsyncProducer, error) {
config := sarama.NewConfig()
config.ClientID = "beam-tracker"
config.Producer.RequiredAcks = sarama.WaitForLocal // Only wait for the leader to ack
config.Producer.Compression = sarama.CompressionSnappy // Compress messages
config.Producer.Flush.Frequency = 500 * time.Millisecond // Flush batches every 500ms
producer, err := sarama.NewAsyncProducer(brokerList, config)
if err != nil {
return nil, err
}
// We will just log to STDOUT if we're not able to produce messages.
// Note: messages will only be returned here after all retry attempts are exhausted.
go func() {
for err := range producer.Errors() {
log.Println("Failed to write kafka producer entry:", err)
}
}()
return producer, nil
}