-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathmain.go
186 lines (156 loc) · 4.24 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
package main
import (
"beam/cmd/tracker/controller"
"beam/cmd/tracker/gen/track"
"beam/model"
"context"
"fmt"
"log"
"os"
"os/signal"
"strings"
"sync"
"syscall"
"time"
"github.com/go-sql-driver/mysql"
"github.com/jmoiron/sqlx"
"github.com/joho/godotenv"
"github.com/kelseyhightower/envconfig"
"github.com/pkg/errors"
)
func main() {
logger := log.New(os.Stderr, "[tracker] ", log.Ltime)
err := godotenv.Load()
if err != nil {
logger.Fatalln(errors.Wrap(err, "unable to load .env file"))
}
var c Config
if err := envconfig.Process("tracker", &c); err != nil {
logger.Fatalln(errors.Wrap(err, "unable to process envconfig"))
}
// Create channel used by both the signal handler and server goroutines
// to notify the main goroutine when to stop the server.
errc := make(chan error)
go func() {
c := make(chan os.Signal, 3)
signal.Notify(c, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT)
errc <- fmt.Errorf("%s", <-c)
}()
var wg sync.WaitGroup
ctx, cancel := context.WithCancel(context.Background())
// Broker init
logger.Println("initializing message broker producer", "implementation", c.BrokerImpl)
eventProducer, err := newProducer(ctx, &c, logger)
if err != nil {
logger.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 {
logger.Fatalln(errors.Wrap(err, "unable to connect to MySQL"))
}
propertyDB := &model.PropertyDB{
MySQL: mysqlDB,
}
entitySchemaDB := &model.EntitySchemaDB{
MySQL: mysqlDB,
}
// server cancellation
ticker := time.NewTicker(10 * time.Second)
defer ticker.Stop()
cacheProperties := func() {
if err = propertyDB.Cache(logger); err != nil {
logger.Fatalln("unable to cache properties", "err", err)
}
}
wg.Add(1)
cacheEntities := func() {
if err = entitySchemaDB.Cache(logger); err != nil {
logger.Fatalln("unable to cache entity schemas", "err", err)
}
}
wg.Add(1)
cacheProperties()
go func() {
defer wg.Done()
logger.Println("starting property caching")
for {
select {
case <-ticker.C:
cacheProperties()
case <-ctx.Done():
logger.Println("property caching stopped")
return
}
}
}()
cacheEntities()
go func() {
defer wg.Done()
logger.Println("starting entity schemas caching")
for {
select {
case <-ticker.C:
cacheEntities()
case <-ctx.Done():
logger.Println("entity schemas caching stopped")
return
}
}
}()
// Initialize the services.
trackSvc := controller.NewTrackController(
eventProducer,
propertyDB,
entitySchemaDB,
strings.Split(c.InternalHosts, ","),
c.TimespentLimit,
)
// Wrap the services in endpoints that can be invoked from other services
// potentially running in different processes.
trackEndpoints := track.NewEndpoints(trackSvc)
handleHTTPServer(ctx, c.TrackerAddr, trackEndpoints, &wg, errc, logger, c.Debug)
// Wait for signal.
logger.Printf("exiting (%v)", <-errc)
// Send cancellation signal to the goroutines.
cancel()
wg.Wait()
logger.Println("bye bye")
}
func newProducer(ctx context.Context, config *Config, logger *log.Logger) (controller.EventProducer, error) {
switch config.BrokerImpl {
case "kafka":
brokerAddrs := strings.Split(config.BrokerAddrs, ",")
for _, addr := range brokerAddrs {
logger.Println("connecting to kafka broker", "bind", addr)
}
saslConfig := &controller.SaslConfig{
Username: config.KafkaSaslUser,
Password: config.KafkaSaslPasswd,
}
producer, err := controller.NewKafkaEventProducer(brokerAddrs, saslConfig)
if err != nil {
return nil, err
}
return producer, nil
case "pubsub":
logger.Println("connecting to pubsub", "projectID", config.PubSubProjectID, "topicID", config.PubSubTopicID)
producer, err := controller.NewPubSubEventProducer(ctx, config.PubSubProjectID, config.PubSubTopicID)
if err != nil {
return nil, err
}
return producer, nil
default:
return nil, errors.Errorf("unknown broker implementation %q", config.BrokerImpl)
}
}