-
Notifications
You must be signed in to change notification settings - Fork 71
/
Copy pathwrite.go
61 lines (45 loc) · 1.38 KB
/
write.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
package gcppubsub
import (
"context"
"time"
"cloud.google.com/go/pubsub"
"github.com/pkg/errors"
"github.com/batchcorp/plumber-schemas/build/go/protos/opts"
"github.com/batchcorp/plumber-schemas/build/go/protos/records"
"github.com/batchcorp/plumber/validate"
)
func (g *GCPPubSub) Write(ctx context.Context, writeOpts *opts.WriteOptions, errorCh chan<- *records.ErrorRecord, messages ...*records.WriteRecord) error {
if err := validateWriteOptions(writeOpts); err != nil {
return errors.New("unable to validate write options")
}
t := g.client.Topic(writeOpts.GcpPubsub.Args.TopicId)
for _, msg := range messages {
result := t.Publish(ctx, &pubsub.Message{
Data: []byte(msg.Input),
})
if _, err := result.Get(ctx); err != nil {
errorCh <- &records.ErrorRecord{
Error: err.Error(),
OccurredAtUnixTsUtc: time.Now().UTC().Unix(),
}
continue
}
g.log.Infof("Successfully wrote message to topic '%s'", writeOpts.GcpPubsub.Args.TopicId)
}
return nil
}
func validateWriteOptions(writeOpts *opts.WriteOptions) error {
if writeOpts == nil {
return validate.ErrEmptyWriteOpts
}
if writeOpts.GcpPubsub == nil {
return validate.ErrEmptyBackendGroup
}
if writeOpts.GcpPubsub.Args == nil {
return validate.ErrEmptyBackendArgs
}
if writeOpts.GcpPubsub.Args.TopicId == "" {
return errors.New("Topic ID cannot be empty")
}
return nil
}