/
servicealerts.go
72 lines (61 loc) · 1.7 KB
/
servicealerts.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
package dbwatch
import (
"context"
"encoding/json"
"time"
"github.com/adjust/rmq/v5"
"github.com/rs/zerolog/log"
"github.com/travigo/travigo/pkg/ctdf"
"github.com/travigo/travigo/pkg/database"
"github.com/travigo/travigo/pkg/redis_client"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
)
type ServiceAlertsWatch struct {
EventQueue rmq.Queue
}
func NewServiceAlertsWatch() *ServiceAlertsWatch {
eventQueue, err := redis_client.QueueConnection.OpenQueue("events-queue")
if err != nil {
log.Fatal().Err(err).Msg("Failed to start event queue")
}
return &ServiceAlertsWatch{
EventQueue: eventQueue,
}
}
func (w *ServiceAlertsWatch) Run() {
log.Info().Msg("Starting dbwatch on collection service_alerts")
collection := database.GetCollection("service_alerts")
matchPipeline := bson.D{
{
Key: "$match", Value: bson.D{
{Key: "operationType", Value: "insert"},
},
},
}
stream, err := collection.Watch(context.Background(), mongo.Pipeline{matchPipeline})
if err != nil {
panic(err)
}
defer stream.Close(context.Background())
for stream.Next(context.Background()) {
var data struct {
OperationType string `bson:"operationType"`
FullDocument *ctdf.ServiceAlert `bson:"fullDocument"`
}
if err := stream.Decode(&data); err != nil {
log.Error().Err(err).Msg("Failed to decode event")
continue
}
if data.OperationType != "insert" {
continue
}
log.Info().Str("id", data.FullDocument.PrimaryIdentifier).Msg("New ServiceAlert inserted")
eventBytes, _ := json.Marshal(ctdf.Event{
Type: ctdf.EventTypeServiceAlertCreated,
Timestamp: time.Now(),
Body: data.FullDocument,
})
w.EventQueue.PublishBytes(eventBytes)
}
}