-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpubsub.go
68 lines (59 loc) · 1.7 KB
/
pubsub.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
package pubsub
import (
"cloud.google.com/go/pubsub"
"context"
"github.com/maksimru/event-scheduler/channel"
"github.com/maksimru/event-scheduler/message"
pubsubconfig "github.com/maksimru/event-scheduler/publisher/pubsub/config"
log "github.com/sirupsen/logrus"
"google.golang.org/api/option"
)
type Publisher struct {
config pubsubconfig.DestinationConfig
client *pubsub.Client
context context.Context
}
func NewPubSubPublisher(ctx context.Context, config channel.DestinationConfig) *Publisher {
cfg := config.(pubsubconfig.DestinationConfig)
psclient, err := makePubsubClient(ctx, cfg)
if err != nil {
panic("exception during publisher boot: " + err.Error())
}
return &Publisher{
config: cfg,
client: psclient,
context: ctx,
}
}
func (p *Publisher) SetPubsubClient(client *pubsub.Client) {
p.client = client
}
func makePubsubClient(ctx context.Context, config pubsubconfig.DestinationConfig) (*pubsub.Client, error) {
client, err := pubsub.NewClient(ctx, config.ProjectID, option.WithCredentialsFile(config.KeyFile))
if err != nil {
log.Error("publisher client boot failure: ", err.Error())
return nil, err
}
return client, err
}
func (p *Publisher) Close() error {
err := p.client.Close()
if err != nil {
log.Error("publisher client termination failure: ", err.Error())
return err
}
return nil
}
func (p *Publisher) Dispatch(msg message.Message) error {
t := p.client.Topic(p.config.TopicID)
result := t.Publish(p.context, &pubsub.Message{
Data: []byte(msg.GetBody().(string)),
})
id, err := result.Get(p.context)
if err != nil {
log.Warn("publisher message delivery exception:", err.Error())
return err
}
log.Trace("publisher message published: ", id)
return nil
}