-
Notifications
You must be signed in to change notification settings - Fork 3
/
pull.go
46 lines (39 loc) · 1.17 KB
/
pull.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
package statusemail
import (
"context"
"github.com/vanti-dev/sc-bos/pkg/gen"
"github.com/vanti-dev/sc-bos/pkg/util/chans"
"github.com/vanti-dev/sc-bos/pkg/util/pull"
)
func pullFrom(ctx context.Context, name string, client gen.StatusApiClient, c chan<- *gen.StatusLog, opts ...pull.Option) error {
puller := statusLogPuller{client: client, name: name}
return pull.Changes[*gen.StatusLog](ctx, puller, c, opts...)
}
type statusLogPuller struct {
client gen.StatusApiClient
name string
}
func (s statusLogPuller) Pull(ctx context.Context, changes chan<- *gen.StatusLog) error {
stream, err := s.client.PullCurrentStatus(ctx, &gen.PullCurrentStatusRequest{Name: s.name})
if err != nil {
return err
}
for {
res, err := stream.Recv()
if err != nil {
return err
}
for _, change := range res.Changes {
if err := chans.SendContext(ctx, changes, change.CurrentStatus); err != nil {
return err
}
}
}
}
func (s statusLogPuller) Poll(ctx context.Context, changes chan<- *gen.StatusLog) error {
status, err := s.client.GetCurrentStatus(ctx, &gen.GetCurrentStatusRequest{Name: s.name})
if err != nil {
return err
}
return chans.SendContext(ctx, changes, status)
}