-
Notifications
You must be signed in to change notification settings - Fork 1
/
polygate.go
170 lines (142 loc) · 3.49 KB
/
polygate.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
package main
import (
"os"
"os/signal"
"strings"
"sync"
"syscall"
stdlog "log"
_ "net/http/pprof"
"github.com/fsnotify/fsnotify"
"github.com/go-redis/redis"
logrus "github.com/sirupsen/logrus"
)
var parameters Parameters
var configuration Configuration
var instanceProducerID string
var log = logrus.New()
func setup() {
prettyLog, exists := os.LookupEnv("PRETTY_LOG")
if exists && prettyLog == "true" {
log.SetFormatter(&logrus.TextFormatter{})
} else {
log.SetFormatter(&logrus.JSONFormatter{})
}
log.SetOutput(os.Stdout)
parameters = loadParameters()
log.SetLevel(parameters.logLevel)
redisLogger := stdlog.New(logrus.StandardLogger().Writer(), "", 0)
redis.SetLogger(redisLogger)
configuration = loadConfiguration()
if configuration.Server.Enable {
loadProducerMetrics()
startRedisClient("job", configuration.Redis.JobPoolSize)
startRedisClient("producer", 1)
var producerID strings.Builder
hostname, err := os.Hostname()
if err != nil {
log.Fatalf("Unable to get hostname: %v", err)
}
producerID.WriteString(configuration.Redis.Prefix)
producerID.WriteRune(':')
producerID.WriteString("prod:")
producerID.WriteString(hostname)
instanceProducerID = producerID.String()
}
if configuration.Client.Enable {
loadConsumerMetrics()
if !configuration.Server.Enable {
startRedisClient("job", configuration.Redis.JobPoolSize)
}
loadClientJobHandlers()
loadConsumers()
}
}
func main() {
setup()
sigs := make(chan os.Signal, 1)
var wg sync.WaitGroup
loadMetricsServer()
startMetricsServer()
if parameters.enableHotReload {
watcher, err := fsnotify.NewWatcher()
if err != nil {
log.Fatalf("Error enabling the hot reload configuration watcher: %v", err)
}
defer watcher.Close()
go func() {
for {
select {
case event, ok := <-watcher.Events:
if !ok {
return
}
log.Warnf("Configuration change detected: %v", event)
if event.Op&fsnotify.Write == fsnotify.Write {
syscall.Kill(syscall.Getpid(), syscall.SIGTERM)
}
case err, ok := <-watcher.Errors:
if !ok {
return
}
log.Println("error:", err)
}
}
}()
err = watcher.Add(parameters.configurationFile)
if err != nil {
log.Fatalf("Error while adding the configuration file to watcher: %v", err)
}
log.Infof("Hot reload enabled, watching file: %v", parameters.configurationFile)
}
stopConsumer := func(sig os.Signal) {
log.Warnf("Stopping client, signal received: %v", sig)
consumerReady.Set(0)
consumersStopped = true
consumersStopWait.Wait()
log.Warn("Stopped consumers")
clientJobsWait.Wait()
log.Warn("No more jobs to wait")
if !configuration.Server.Enable {
closeRedisClients("job")
}
stopConsumersRedisConnections()
stopMetricsServer()
wg.Done()
}
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
if configuration.Server.Enable {
startProducerListener()
server := createServer()
wg.Add(1)
go func() {
sig := <-sigs
log.Warnf("Stopping server, signal received: %v", sig)
producerReady.Set(0)
server.GracefulStop()
log.Warn("gRPC server stopped")
closeProducerListener()
closeRedisClients("producer")
if configuration.Client.Enable {
stopConsumer(sig)
wg.Done()
return
}
closeRedisClients("job")
stopMetricsServer()
wg.Done()
}()
}
if configuration.Client.Enable {
startConsumers()
wg.Add(1)
if !configuration.Server.Enable {
go func() {
sig := <-sigs
stopConsumer(sig)
}()
}
}
polygateUp.Set(1)
wg.Wait()
}