-
Notifications
You must be signed in to change notification settings - Fork 8
/
main.go
367 lines (316 loc) · 9.75 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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
package main
import (
"context"
"errors"
"fmt"
"net"
"net/http"
"net/http/pprof"
"os"
"os/signal"
"runtime"
"syscall"
"time"
"github.com/artefactual-labs/enduro/internal/api"
"github.com/artefactual-labs/enduro/internal/cadence"
"github.com/artefactual-labs/enduro/internal/collection"
"github.com/artefactual-labs/enduro/internal/db"
nha_activities "github.com/artefactual-labs/enduro/internal/nha/activities"
"github.com/artefactual-labs/enduro/internal/pipeline"
"github.com/artefactual-labs/enduro/internal/watcher"
"github.com/artefactual-labs/enduro/internal/workflow"
"github.com/artefactual-labs/enduro/internal/workflow/activities"
"github.com/artefactual-labs/enduro/internal/workflow/manager"
"github.com/go-logr/logr"
"github.com/go-logr/zapr"
"github.com/oklog/run"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/spf13/pflag"
"github.com/spf13/viper"
"go.uber.org/cadence/client"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
const appName = "enduro"
var (
version = "(dev-version)"
gitCommit = "(dev-commit)"
buildTime = "(dev-buildtime)"
goVersion = runtime.Version()
)
func main() {
var (
v = viper.New()
p = pflag.NewFlagSet(appName, pflag.ExitOnError)
)
configureViper(v)
p.String("config", "", "Configuration file")
p.Bool("version", false, "Show version information")
_ = p.Parse(os.Args[1:])
if v, _ := p.GetBool("version"); v {
fmt.Printf(
"%s version %s (commit=%s) built on %s using %s\n",
appName, version, gitCommit, buildTime, goVersion)
os.Exit(0)
}
var config configuration
configFile, _ := p.GetString("config")
configFileFound, err := readConfig(v, &config, configFile)
if err != nil {
fmt.Printf("Failed to read configuration: %v\n", err)
os.Exit(1)
}
// Logging configuration.
var logger logr.Logger
var zlogger *zap.Logger
{
var zconfig zap.Config
if config.Debug {
encoderConfig := zap.NewDevelopmentEncoderConfig()
encoderConfig.EncodeLevel = zapcore.CapitalColorLevelEncoder
zconfig = zap.NewDevelopmentConfig()
zconfig.EncoderConfig = encoderConfig
} else {
zconfig = zap.NewProductionConfig()
}
zlogger, err = zconfig.Build(zap.AddCallerSkip(1))
zlogger = zlogger.Named(appName)
defer func() { _ = zlogger.Sync() }()
if err != nil {
fmt.Printf("Failed to set up logger %v", err)
os.Exit(1)
}
logger = zapr.NewLogger(zlogger)
logger.Info("Starting...", "version", version, "pid", os.Getpid())
}
if configFileFound {
logger.Info("Configuration file loaded.", "path", v.ConfigFileUsed())
} else {
logger.Info("Configuration file not found.")
}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
database, err := db.Connect(config.Database.DSN)
if err != nil {
logger.Error(err, "Database configuration failed.")
os.Exit(1)
}
_ = database.Ping()
var workflowClient client.Client
{
workflowClient, err = cadence.NewWorkflowClient(zlogger.Named("cadence-client"), appName, config.Cadence)
if err != nil {
logger.Error(err, "Cadence workflow client creation failed.")
os.Exit(1)
}
}
// Set up the pipeline registry.
pipelineRegistry, err := pipeline.NewPipelineRegistry(logger.WithName("registry"), config.Pipeline)
if err != nil {
logger.Error(err, "Pipeline registry cannot be initialized.")
os.Exit(1)
}
// Set up the collection service.
var colsvc collection.Service
{
colsvc = collection.NewService(logger.WithName("collection"), database, workflowClient, pipelineRegistry)
}
// Set up the watcher service.
var wsvc watcher.Service
{
wsvc, err = watcher.New(ctx, &config.Watcher)
if err != nil {
logger.Error(err, "Error setting up watchers.")
os.Exit(1)
}
}
var g run.Group
// API server.
{
var srv *http.Server
g.Add(
func() error {
srv = api.HTTPServer(logger, &config.API, colsvc)
return srv.ListenAndServe()
},
func(err error) {
ctx, cancel := context.WithTimeout(ctx, time.Second*5)
defer cancel()
_ = srv.Shutdown(ctx)
},
)
}
// Watchers, where each watcher is a group actor.
{
for _, w := range wsvc.Watchers() {
done := make(chan struct{})
cur := w
g.Add(
func() error {
for {
select {
case <-done:
return nil
default:
event, err := cur.Watch(ctx)
if err != nil {
if errors.Is(err, watcher.ErrWatchTimeout) {
continue
}
logger.Error(err, "Error monitoring watcher interface.")
}
go func() {
if err := collection.InitProcessingWorkflow(ctx, workflowClient, event); err != nil {
logger.Error(err, "Error initializing processing workflow.")
}
}()
}
}
},
func(err error) {
close(done)
},
)
}
}
// Create workflow workers which manage workflow and activity executions.
// This section could be executed as a different process and have replicas.
{
// TODO: this is a temporary workaround for dependency injection until we
// figure out what's the depdencency tree is going to look like after POC.
// The share-everything pattern should be avoided.
m := manager.NewManager(logger, colsvc, wsvc, pipelineRegistry, config.Hooks)
cadence.RegisterWorkflow(workflow.NewProcessingWorkflow(m).Execute, collection.ProcessingWorkflowName)
cadence.RegisterActivity(activities.NewAcquirePipelineActivity(m).Execute, activities.AcquirePipelineActivityName)
cadence.RegisterActivity(activities.NewDownloadActivity(m).Execute, activities.DownloadActivityName)
cadence.RegisterActivity(activities.NewBundleActivity().Execute, activities.BundleActivityName)
cadence.RegisterActivity(activities.NewTransferActivity(m).Execute, activities.TransferActivityName)
cadence.RegisterActivity(activities.NewPollTransferActivity(m).Execute, activities.PollTransferActivityName)
cadence.RegisterActivity(activities.NewPollIngestActivity(m).Execute, activities.PollIngestActivityName)
cadence.RegisterActivity(activities.NewCleanUpActivity(m).Execute, activities.CleanUpActivityName)
cadence.RegisterActivity(activities.NewHidePackageActivity(m).Execute, activities.HidePackageActivityName)
cadence.RegisterActivity(activities.NewDeleteOriginalActivity(m).Execute, activities.DeleteOriginalActivityName)
cadence.RegisterActivity(workflow.NewAsyncCompletionActivity(m).Execute, workflow.AsyncCompletionActivityName)
cadence.RegisterActivity(nha_activities.NewUpdateHARIActivity(m).Execute, nha_activities.UpdateHARIActivityName)
cadence.RegisterActivity(nha_activities.NewUpdateProductionSystemActivity(m).Execute, nha_activities.UpdateProductionSystemActivityName)
done := make(chan struct{})
w, err := cadence.NewWorker(zlogger.Named("cadence-worker"), appName, config.Cadence)
if err != nil {
logger.Error(err, "Error creating Cadence worker.")
os.Exit(1)
}
g.Add(
func() error {
if err := w.Start(); err != nil {
return err
}
<-done
return nil
},
func(err error) {
w.Stop()
close(done)
},
)
}
{
ln, err := net.Listen("tcp", config.DebugListen)
if err != nil {
logger.Error(err, "Error setting up the debug interface.")
os.Exit(1)
}
g.Add(func() error {
mux := http.NewServeMux()
// Health check.
mux.HandleFunc("/health", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
fmt.Fprintln(w, "OK")
})
// Prometheus metrics.
mux.Handle("/metrics", promhttp.Handler())
// Profiling data.
mux.HandleFunc("/debug/pprof/", pprof.Index)
mux.HandleFunc("/debug/pprof/cmdline", pprof.Cmdline)
mux.HandleFunc("/debug/pprof/profile", pprof.Profile)
mux.HandleFunc("/debug/pprof/symbol", pprof.Symbol)
mux.HandleFunc("/debug/pprof/trace", pprof.Trace)
mux.Handle("/debug/pprof/block", pprof.Handler("block"))
mux.Handle("/debug/pprof/goroutine", pprof.Handler("goroutine"))
mux.Handle("/debug/pprof/heap", pprof.Handler("heap"))
mux.Handle("/debug/pprof/threadcreate", pprof.Handler("threadcreate"))
return http.Serve(ln, mux)
}, func(error) {
ln.Close()
})
}
// Signal handler.
{
var (
cancelInterrupt = make(chan struct{})
ch = make(chan os.Signal, 2)
)
defer close(ch)
g.Add(
func() error {
signal.Notify(ch, syscall.SIGINT, syscall.SIGTERM)
select {
case <-ch:
case <-cancelInterrupt:
}
return nil
}, func(err error) {
logger.Info("Quitting...")
close(cancelInterrupt)
cancel()
signal.Stop(ch)
},
)
}
logger.Error(g.Run(), "Bye!")
}
type configuration struct {
Debug bool
DebugListen string
API api.Config
Database db.Config
Cadence cadence.Config
Watcher watcher.Config
Pipeline []pipeline.Config
// This is a workaround for client-specific functionality.
// Simple mechanism to support an arbitrary number of hooks and parameters.
Hooks map[string]map[string]interface{}
}
func (c configuration) Validate() error {
return nil
}
func configureViper(v *viper.Viper) {
v.AddConfigPath(".")
v.AddConfigPath("$HOME/.config/")
v.AddConfigPath("/etc")
v.SetConfigName(appName)
v.SetDefault("debugListen", "127.0.0.1:9001")
v.SetDefault("api.listen", "127.0.0.1:9000")
v.SetDefault("cadence.address", ":7933")
v.Set("api.appVersion", version)
}
func readConfig(v *viper.Viper, config *configuration, configFile string) (found bool, err error) {
if configFile != "" {
v.SetConfigFile(configFile)
}
err = v.ReadInConfig()
_, ok := err.(viper.ConfigFileNotFoundError)
if !ok {
found = true
}
if found && err != nil {
return found, fmt.Errorf("Failed to read configuration file: %w", err)
}
err = v.Unmarshal(config)
if err != nil {
return found, fmt.Errorf("Failed to unmarshal configuration: %w", err)
}
if err := config.Validate(); err != nil {
return found, fmt.Errorf("Failed to validate the provided config: %w", err)
}
return found, nil
}