-
Notifications
You must be signed in to change notification settings - Fork 607
/
Copy pathmain.go
262 lines (221 loc) · 7.12 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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
/*
Copyright 2021 Cortex Labs, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
import (
"flag"
"fmt"
"net/http"
"os"
"os/signal"
"strconv"
"github.com/DataDog/datadog-go/statsd"
"github.com/cortexlabs/cortex/pkg/consts"
"github.com/cortexlabs/cortex/pkg/dequeuer"
awslib "github.com/cortexlabs/cortex/pkg/lib/aws"
"github.com/cortexlabs/cortex/pkg/lib/errors"
"github.com/cortexlabs/cortex/pkg/lib/files"
"github.com/cortexlabs/cortex/pkg/lib/logging"
"github.com/cortexlabs/cortex/pkg/lib/telemetry"
"github.com/cortexlabs/cortex/pkg/probe"
"github.com/cortexlabs/cortex/pkg/types/clusterconfig"
"github.com/cortexlabs/cortex/pkg/types/userconfig"
"go.uber.org/zap"
)
func main() {
var (
clusterConfigPath string
clusterUID string
probesPath string
queueURL string
userContainerPort int
apiName string
jobID string
statsdAddress string
apiKind string
adminPort int
)
flag.StringVar(&clusterConfigPath, "cluster-config", "", "cluster config path")
flag.StringVar(&clusterUID, "cluster-uid", "", "cluster unique identifier")
flag.StringVar(&probesPath, "probes-path", "", "path to the probes spec")
flag.StringVar(&queueURL, "queue", "", "target queue URL from which the api messages will be dequeued")
flag.StringVar(&apiKind, "api-kind", "", fmt.Sprintf("api kind (%s|%s)", userconfig.BatchAPIKind.String(), userconfig.AsyncAPIKind.String()))
flag.StringVar(&apiName, "api-name", "", "api name")
flag.StringVar(&jobID, "job-id", "", "job ID")
flag.StringVar(&statsdAddress, "statsd-address", "", "address to push statsd metrics")
flag.IntVar(&userContainerPort, "user-port", 8080, "target port to which the dequeued messages will be sent to")
flag.IntVar(&adminPort, "admin-port", 0, "port where the admin server (for the probes) will be exposed")
flag.Parse()
version := os.Getenv("CORTEX_VERSION")
if version == "" {
version = consts.CortexVersion
}
log := logging.GetLogger()
defer func() {
_ = log.Sync()
}()
switch {
case clusterConfigPath == "":
log.Fatal("--cluster-config is a required option")
case probesPath == "":
log.Fatal("--probes-path is a required option")
case queueURL == "":
log.Fatal("--queue is a required option")
case apiName == "":
log.Fatal("--api-name is a required option")
case apiKind == "":
log.Fatal("--api-kind is a required option")
case adminPort == 0:
log.Fatal("--admin-port is a required option")
}
targetURL := "http://127.0.0.1:" + strconv.Itoa(userContainerPort)
clusterConfig, err := clusterconfig.NewForFile(clusterConfigPath)
if err != nil {
exit(log, err)
}
awsClient, err := awslib.NewForRegion(clusterConfig.Region)
if err != nil {
exit(log, err, "failed to create aws client")
}
_, userID, err := awsClient.CheckCredentials()
if err != nil {
exit(log, err)
}
err = telemetry.Init(telemetry.Config{
Enabled: clusterConfig.Telemetry,
UserID: userID,
Properties: map[string]string{
"kind": apiKind,
"image_type": "dequeuer",
},
Environment: "api",
LogErrors: true,
BackoffMode: telemetry.BackoffDuplicateMessages,
})
if err != nil {
log.Fatalw("failed to initialize telemetry", "error", err)
}
defer telemetry.Close()
var probes []*probe.Probe
if files.IsFile(probesPath) {
probes, err = dequeuer.ProbesFromFile(probesPath, log)
if err != nil {
exit(log, err, fmt.Sprintf("unable to read probes from %s", probesPath))
}
}
if !dequeuer.HasTCPProbeTargetingUserPod(probes, userContainerPort) {
probes = append(probes, probe.NewDefaultProbe(fmt.Sprintf("http://localhost:%d", userContainerPort), log))
}
adminHandler := http.NewServeMux()
adminHandler.Handle("/healthz", dequeuer.HealthcheckHandler(func() bool {
return probe.AreProbesHealthy(probes)
}))
var dequeuerConfig dequeuer.SQSDequeuerConfig
var messageHandler dequeuer.MessageHandler
switch apiKind {
case userconfig.BatchAPIKind.String():
if jobID == "" {
log.Fatal("--job-id is a required option")
}
config := dequeuer.BatchMessageHandlerConfig{
Region: clusterConfig.Region,
APIName: apiName,
JobID: jobID,
QueueURL: queueURL,
TargetURL: targetURL,
}
metricsClient, err := statsd.New(statsdAddress)
if err != nil {
exit(log, err, "unable to initialize metrics client")
}
messageHandler = dequeuer.NewBatchMessageHandler(config, awsClient, metricsClient, log)
dequeuerConfig = dequeuer.SQSDequeuerConfig{
Region: clusterConfig.Region,
QueueURL: queueURL,
StopIfNoMessages: true,
}
case userconfig.AsyncAPIKind.String():
if clusterUID == "" {
log.Fatal("--cluster-uid is a required option")
}
config := dequeuer.AsyncMessageHandlerConfig{
ClusterUID: clusterUID,
Bucket: clusterConfig.Bucket,
APIName: apiName,
TargetURL: targetURL,
}
asyncStatsReporter := dequeuer.NewAsyncPrometheusStatsReporter()
messageHandler = dequeuer.NewAsyncMessageHandler(config, awsClient, asyncStatsReporter, log)
dequeuerConfig = dequeuer.SQSDequeuerConfig{
Region: clusterConfig.Region,
QueueURL: queueURL,
StopIfNoMessages: false,
}
// report prometheus metrics for async api kinds
adminHandler.Handle("/metrics", asyncStatsReporter)
default:
exit(log, err, fmt.Sprintf("kind %s is not supported", apiKind))
}
errCh := make(chan error)
go func() {
server := &http.Server{
Addr: ":" + strconv.Itoa(adminPort),
Handler: adminHandler,
}
log.Infof("Starting %s server on %s", consts.AdminPortName, server.Addr)
errCh <- server.ListenAndServe()
}()
sigint := make(chan os.Signal, 1)
signal.Notify(sigint, os.Interrupt)
sqsDequeuer, err := dequeuer.NewSQSDequeuer(dequeuerConfig, awsClient, log)
if err != nil {
exit(log, err, "failed to create sqs dequeuer")
}
go func() {
log.Info("Starting dequeuer...")
errCh <- sqsDequeuer.Start(messageHandler, func() bool {
return probe.AreProbesHealthy(probes)
})
}()
var stopChs []chan struct{}
for _, p := range probes {
stopChs = append(stopChs, p.StartProbing())
}
defer func() {
for _, stopCh := range stopChs {
stopCh <- struct{}{}
}
}()
select {
case err = <-errCh:
exit(log, err, "error during message dequeueing or error from admin server")
case <-sigint:
log.Info("Received TERM signal, handling a graceful shutdown...")
sqsDequeuer.Shutdown()
log.Info("Shutdown complete, exiting...")
}
}
func exit(log *zap.SugaredLogger, err error, wrapStrs ...string) {
if err == nil {
os.Exit(0)
}
for _, str := range wrapStrs {
err = errors.Wrap(err, str)
}
if !errors.IsNoTelemetry(err) {
telemetry.Error(err)
}
if !errors.IsNoPrint(err) {
log.Error(err)
}
os.Exit(1)
}