-
Notifications
You must be signed in to change notification settings - Fork 1
/
eventhandler.go
40 lines (36 loc) · 911 Bytes
/
eventhandler.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
package handler
import (
"context"
"encoding/json"
"fmt"
"log"
"github.com/apache/pulsar-client-go/pulsar"
"github.com/victor-nach/pulsar-microservice/src/deathstar-service/pb"
"github.com/victor-nach/pulsar-microservice/src/deathstar-service/server/repo"
)
// EventService - event service struct
type EventService struct {
Consumer pulsar.Consumer
Repo repo.RepoInterface
}
// Run - listen for events from the broker
func (e *EventService) Run() {
log.Println("Listeneing for events")
for {
msg, err := e.Consumer.Receive(context.Background())
if err != nil {
log.Fatal(err)
}
payload := msg.Payload()
var targets pb.Targets
err = json.Unmarshal(payload, &targets)
err = e.Repo.SaveTarget(targets)
if err != nil {
fmt.Errorf("Error saving target: %v", err)
return
}
// Acknowledge message
e.Consumer.AckID(msg.ID())
fmt.Println("Message acknowledged.")
}
}