-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathworker.go
56 lines (48 loc) · 1.47 KB
/
worker.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
package outbox
import (
"context"
"errors"
"time"
"github.com/kamal-github/outbox/datastore"
"github.com/kamal-github/outbox/pubsub"
"go.uber.org/zap"
)
// Worker is the outbox worker which runs repeatedly until asked to stop.
type Worker struct {
MineSweeper datastore.MineSweeper
Dispatcher pubsub.Dispatcher
MineInterval time.Duration
Logger *zap.Logger
}
// Start starts the outbox worker and iterative looks for new outbox rows (ready to process)
// after each given MineInterval and publishes to one of the configured Messaging system.
//
// When no ready to process message are found, it keep looking for new ones.
//
// Exit as soon as ctx is cancelled.
func (w Worker) Start(ctx context.Context, done chan<- struct{}) {
ticker := time.NewTicker(w.MineInterval)
defer ticker.Stop()
for range ticker.C {
select {
case <-ctx.Done():
done <- struct{}{}
return
default:
}
ctx := context.Background()
events, err := w.MineSweeper.Mine(ctx)
if errors.Is(err, datastore.ErrNoEvents) {
w.Logger.Error("failed while Err no event collecting event from datastore", zap.Error(err))
continue
}
// Validation error (e.g metadata incorrect format) or network error.
if err != nil {
w.Logger.Error("failed while collecting event from datastore", zap.Error(err))
continue
}
if err = w.Dispatcher.Dispatch(ctx, events); err != nil {
w.Logger.Error("failed while sending event to Dispatcher", zap.Error(err), zap.String("pubsub", "rabbitmq"))
}
}
}