Skip to content

Commit 2f3ace4

Browse files
committed
feat: improve the Listener interface
- pass in a context for easier cancellation of tasks and sharing of data (such as loggers)
1 parent 5fa5238 commit 2f3ace4

File tree

4 files changed

+11
-7
lines changed

4 files changed

+11
-7
lines changed

chrysom/listenerClient.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ func (c *ListenerClient) Start(ctx context.Context) error {
118118
outcome := SuccessOutcome
119119
items, err := c.reader.GetItems(ctx, "")
120120
if err == nil {
121-
c.observer.listener.Update(items)
121+
c.observer.listener.Update(ctx, items)
122122
} else {
123123
outcome = FailureOutcome
124124
logger.Error("Failed to get items for listeners", zap.Error(err))

chrysom/listenerClient_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import (
1919
)
2020

2121
var (
22-
mockListener = ListenerFunc((func(_ Items) {
22+
mockListener = ListenerFunc((func(context.Context, Items) {
2323
fmt.Println("Doing amazing work for 100ms")
2424
time.Sleep(time.Millisecond * 100)
2525
}))

chrysom/store.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,13 @@ type Listener interface {
2727
// additions, or updates.
2828
//
2929
// The list of hooks must contain only the current items.
30-
Update(items Items)
30+
Update(ctx context.Context, items Items)
3131
}
3232

33-
type ListenerFunc func(items Items)
33+
type ListenerFunc func(ctx context.Context, items Items)
3434

35-
func (l ListenerFunc) Update(items Items) {
36-
l(items)
35+
func (l ListenerFunc) Update(ctx context.Context, items Items) {
36+
l(ctx, items)
3737
}
3838

3939
type Reader interface {

service.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ import (
1111
"time"
1212

1313
"github.com/xmidt-org/ancla/chrysom"
14+
"github.com/xmidt-org/sallust"
1415
"go.uber.org/fx"
16+
"go.uber.org/zap"
1517
)
1618

1719
const errFmt = "%w: %v"
@@ -143,9 +145,11 @@ func prepArgusBasicClientConfig(cfg *Config) error {
143145

144146
func prepArgusListenerConfig(cfg *chrysom.ListenerConfig, metrics chrysom.Measures, watches ...Watch) {
145147
watches = append(watches, webhookListSizeWatch(metrics.WebhookListSizeGauge))
146-
cfg.Listener = chrysom.ListenerFunc(func(items chrysom.Items) {
148+
cfg.Listener = chrysom.ListenerFunc(func(ctx context.Context, items chrysom.Items) {
149+
logger := sallust.Get(ctx)
147150
iws, err := ItemsToInternalWebhooks(items)
148151
if err != nil {
152+
logger.Error("Failed to convert items to webhooks", zap.Error(err))
149153
return
150154
}
151155
for _, watch := range watches {

0 commit comments

Comments
 (0)