-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.go
248 lines (198 loc) · 7.44 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
// jex-adapter
//
// jex-adapter allows the apps service to submit job requests through an AMQP
// broker by implementing the portion of the old JEX API that apps interacted
// with. Instead of writing the files out to disk and calling condor_submit like
// the JEX service did, it serializes the request as JSON and pushes it out
// as a message on the "jobs" exchange with a routing key of "jobs.launches".
//
package main
import (
"context"
_ "expvar"
"flag"
"fmt"
"net/http"
"net/url"
"os"
"time"
"github.com/cyverse-de/configurate"
"github.com/cyverse-de/go-mod/cfg"
"github.com/cyverse-de/go-mod/gotelnats"
"github.com/cyverse-de/go-mod/otelutils"
"github.com/cyverse-de/go-mod/protobufjson"
"github.com/cyverse-de/messaging/v9"
"github.com/cyverse-de/version"
"github.com/jmoiron/sqlx"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
"github.com/nats-io/nats.go"
"github.com/sirupsen/logrus"
"github.com/spf13/viper"
"github.com/cyverse-de/jex-adapter/adapter"
"github.com/cyverse-de/jex-adapter/db"
"github.com/cyverse-de/jex-adapter/logging"
"github.com/cyverse-de/jex-adapter/millicores"
"github.com/cyverse-de/jex-adapter/previewer"
"github.com/uptrace/opentelemetry-go-extra/otelsql"
"github.com/uptrace/opentelemetry-go-extra/otelsqlx"
"go.opentelemetry.io/contrib/instrumentation/github.com/labstack/echo/otelecho"
semconv "go.opentelemetry.io/otel/semconv/v1.7.0"
_ "github.com/lib/pq"
)
const serviceName = "jex-adapter"
var log = logging.Log.WithFields(logrus.Fields{"package": "main"})
func dbConnection(cfg *viper.Viper) *sqlx.DB {
log := log.WithFields(logrus.Fields{"context": "database configuration"})
dbURI := cfg.GetString("db.uri")
if dbURI == "" {
log.Fatal("db.uri must be set in the configuration file")
}
dbURL, err := url.Parse(dbURI)
if err != nil {
log.Fatal(err)
}
log.Infof("db host is %s:%s%s?%s", dbURL.Hostname(), dbURL.Port(), dbURL.Path, dbURL.RawQuery)
dbconn := otelsqlx.MustConnect("postgres", dbURI, otelsql.WithAttributes(semconv.DBSystemPostgreSQL))
log.Info("connected to the database")
return dbconn
}
func amqpConnection(cfg *viper.Viper) (*messaging.Client, string) {
log := log.WithFields(logrus.Fields{"context": "amqp configuration"})
amqpURI := cfg.GetString("amqp.uri")
if amqpURI == "" {
log.Fatal("amqp.uri must be set in the configuration file")
}
aURL, err := url.Parse(amqpURI)
if err != nil {
log.Fatal(err)
}
log.Infof("amqp broker host is %s:%s%s", aURL.Hostname(), aURL.Port(), aURL.Path)
exchangeName := cfg.GetString("amqp.exchange.name")
if exchangeName == "" {
log.Fatal("amqp.exchange.name must be set in the configuration file")
}
log.Infof("amqp exchange name is %s", exchangeName)
amqpclient, err := messaging.NewClient(amqpURI, false)
if err != nil {
log.Fatal(err)
}
if err = amqpclient.SetupPublishing(exchangeName); err != nil {
log.Fatal(err)
}
log.Info("set up AMQP connection")
return amqpclient, exchangeName
}
func natsConnection(natsCluster, creds, tlsca, tlscrt, tlskey string, maxReconnects, reconnectWait int, envPrefix string) (*nats.EncodedConn, error) {
nc, err := nats.Connect(
natsCluster,
nats.UserCredentials(creds),
nats.RootCAs(tlsca),
nats.ClientCert(tlscrt, tlskey),
nats.RetryOnFailedConnect(true),
nats.MaxReconnects(maxReconnects),
nats.ReconnectWait(time.Duration(reconnectWait)*time.Second),
nats.DisconnectErrHandler(func(nc *nats.Conn, err error) {
log.Errorf("disconnected from nats: %s", err.Error())
}),
nats.ReconnectHandler(func(nc *nats.Conn) {
log.Infof("reconnected to %s", nc.ConnectedUrl())
}),
nats.ClosedHandler(func(nc *nats.Conn) {
log.Errorf("connection closed: %s", nc.LastError().Error())
}),
)
if err != nil {
return nil, err
}
return nats.NewEncodedConn(nc, "protojson")
}
func main() {
var (
err error
showVersion = flag.Bool("version", false, "Print version information")
cfgPath = flag.String("config", "", "Path to the configuration file")
dotEnvPath = flag.String("dotenv-path", cfg.DefaultDotEnvPath, "Path to the dotenv file")
tlsCert = flag.String("tlscert", gotelnats.DefaultTLSCertPath, "Path to the NATS TLS cert file")
tlsKey = flag.String("tlskey", gotelnats.DefaultTLSKeyPath, "Path to the NATS TLS key file")
caCert = flag.String("tlsca", gotelnats.DefaultTLSCAPath, "Path to the NATS TLS CA file")
credsPath = flag.String("creds", gotelnats.DefaultCredsPath, "Path to the NATS creds file")
maxReconnects = flag.Int("max-reconnects", gotelnats.DefaultMaxReconnects, "Maximum number of reconnection attempts to NATS")
reconnectWait = flag.Int("reconnect-wait", gotelnats.DefaultReconnectWait, "Seconds to wait between reconnection attempts to NATS")
envPrefix = flag.String("env-prefix", cfg.DefaultEnvPrefix, "The prefix for environment variables")
addr = flag.String("addr", ":60000", "The port to listen on for HTTP requests")
defaultMillicores = flag.Float64("default-millicores", 4000.0, "The default number of millicores reserved for an analysis.")
logLevel = flag.String("log-level", "info", "One of trace, debug, info, warn, error, fatal, or panic.")
)
flag.Parse()
logging.SetupLogging(*logLevel)
log := log.WithFields(logrus.Fields{"context": "main function"})
var tracerCtx, cancel = context.WithCancel(context.Background())
defer cancel()
shutdown := otelutils.TracerProviderFromEnv(tracerCtx, serviceName, func(e error) { log.Fatal(e) })
defer shutdown()
nats.RegisterEncoder("protojson", protobufjson.NewCodec(protobufjson.WithEmitUnpopulated()))
log.Infof("log level is %s", *logLevel)
log.Infof("default millicores is %f", *defaultMillicores)
if *showVersion {
version.AppVersion()
os.Exit(0)
}
if *cfgPath == "" {
fmt.Println("--config is required")
flag.PrintDefaults()
os.Exit(-1)
}
log.Infof("config path is %s", *cfgPath)
c, err := configurate.InitDefaults(*cfgPath, configurate.JobServicesDefaults)
if err != nil {
log.Fatal(err)
}
envCfg, err := cfg.Init(&cfg.Settings{
EnvPrefix: *envPrefix,
ConfigPath: *cfgPath,
DotEnvPath: *dotEnvPath,
StrictMerge: false,
FileType: cfg.YAML,
})
if err != nil {
log.Fatal(err)
}
natsCluster := envCfg.String("nats.cluster")
if natsCluster == "" {
log.Fatalf("The %s_NATS_CLUSTER environment variable or nats.cluster configuration value must be set", *envPrefix)
}
log.Infof("nats.cluster is set to '%s'", natsCluster)
dbconn := dbConnection(c)
amqpclient, exchangeName := amqpConnection(c)
nc, err := natsConnection(natsCluster, *credsPath, *caCert, *tlsCert, *tlsKey, *maxReconnects, *reconnectWait, *envPrefix)
if err != nil {
log.Fatal(err)
}
dbase := db.New(dbconn)
detector, err := millicores.New(dbase, *defaultMillicores)
if err != nil {
log.Fatal(err)
}
messenger := adapter.NewAMQPMessenger(exchangeName, amqpclient, nc)
p := previewer.New()
a := adapter.New(c, detector, messenger)
go a.Run()
defer a.Finish()
router := echo.New()
router.Use(otelecho.Middleware(serviceName))
router.HTTPErrorHandler = logging.HTTPErrorHandler
routerLogger := log.Writer()
defer routerLogger.Close()
router.Use(middleware.LoggerWithConfig(
middleware.LoggerConfig{
Format: "${method} ${uri} ${status}",
Output: routerLogger,
},
))
a.Routes(router)
previewrouter := router.Group("/arg-preview")
p.Routes(previewrouter)
log.Infof("starting server on %s", *addr)
log.Fatal(http.ListenAndServe(*addr, router))
}