-
Notifications
You must be signed in to change notification settings - Fork 15
/
main.go
98 lines (82 loc) · 2.51 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
package main // import "github.com/turnerlabs/udeploy"
import (
"context"
"log"
"strconv"
"github.com/turnerlabs/udeploy/component/broker"
"github.com/turnerlabs/udeploy/component/cache"
"github.com/turnerlabs/udeploy/component/cfg"
"github.com/turnerlabs/udeploy/component/db"
"github.com/turnerlabs/udeploy/component/deploy"
"github.com/turnerlabs/udeploy/component/notify"
"go.mongodb.org/mongo-driver/mongo"
)
var version = "0.0.0-rc"
func main() {
//--------------------------------------------------
//- Initialize connections
//--------------------------------------------------
ctx := context.Background()
if err := db.Connect(ctx, cfg.Get["DB_URI"]); err != nil {
log.Fatal(err)
}
defer db.Disconnect(ctx)
sess, err := db.Client().StartSession()
if err != nil {
log.Fatal(err)
}
defer sess.EndSession(ctx)
changeNotifier := broker.NewBroker()
go changeNotifier.Start()
go func() {
for msg := range cache.Apps.Notifications {
changeNotifier.Publish(msg)
}
}()
//--------------------------------------------------
//- Cache applications
//--------------------------------------------------
if preCache, err := strconv.ParseBool(cfg.Get["PRE_CACHE"]); err == nil && preCache {
go func() {
if err = mongo.WithSession(ctx, sess, func(sctx mongo.SessionContext) error {
return cache.Ensure(sctx)
}); err != nil {
log.Fatal(err)
}
}()
}
//--------------------------------------------------
//- Monitor for changes
//--------------------------------------------------
monitorChanges(ctx, sess)
//--------------------------------------------------
//- Propagate deployments
//--------------------------------------------------
go func() {
changes := changeNotifier.Subscribe()
defer changeNotifier.Unsubscribe(changes)
if err = mongo.WithSession(ctx, sess, func(sctx mongo.SessionContext) error {
log.Fatal(deploy.Propagate(sctx, changes))
return nil
}); err != nil {
log.Fatal(err)
}
}()
//--------------------------------------------------
//- Send SQS queue notifications
//--------------------------------------------------
go func() {
changes := changeNotifier.Subscribe()
defer changeNotifier.Unsubscribe(changes)
if err = mongo.WithSession(ctx, sess, func(sctx mongo.SessionContext) error {
log.Fatal(notify.Watch(sctx, changes))
return nil
}); err != nil {
log.Fatal(err)
}
}()
//--------------------------------------------------
//- Routing
//--------------------------------------------------
startRouter(changeNotifier)
}