-
Notifications
You must be signed in to change notification settings - Fork 8
/
record_processor.go
93 lines (82 loc) · 2.54 KB
/
record_processor.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package outbox
import (
"fmt"
"github.com/pkritiotis/outbox/internal/time"
)
// defaultRecordProcessor checks and dispatches new messages to be sent
type defaultRecordProcessor struct {
messageBroker MessageBroker
store Store
time time.Provider
machineID string
retrialPolicy RetrialPolicy
}
// newProcessor constructs a new defaultRecordProcessor
func newProcessor(store Store, messageBroker MessageBroker, machineID string, retrialPolicy RetrialPolicy) *defaultRecordProcessor {
return &defaultRecordProcessor{
messageBroker: messageBroker,
store: store,
time: time.NewTimeProvider(),
machineID: machineID,
retrialPolicy: retrialPolicy,
}
}
// ProcessRecords locks unprocessed messages, tries to deliver them and then unlocks them
func (d defaultRecordProcessor) ProcessRecords() error {
err := d.lockUnprocessedEntities()
defer d.store.ClearLocksByLockID(d.machineID)
if err != nil {
return err
}
records, err := d.store.GetRecordsByLockID(d.machineID)
if err != nil {
return err
}
if len(records) == 0 {
return nil
}
return d.publishMessages(records)
}
func (d defaultRecordProcessor) publishMessages(records []Record) error {
for _, rec := range records {
// Send message to message broker
now := d.time.Now().UTC()
rec.LastAttemptOn = &now
rec.NumberOfAttempts++
err := d.messageBroker.Send(rec.Message)
// If an error occurs, remove the lock information, update retrial times and continue
if err != nil {
rec.LockedOn = nil
rec.LockID = nil
errorMsg := err.Error()
rec.Error = &errorMsg
if d.retrialPolicy.MaxSendAttemptsEnabled && rec.NumberOfAttempts == d.retrialPolicy.MaxSendAttempts {
rec.State = MaxAttemptsReached
}
dbErr := d.store.UpdateRecordByID(rec)
if dbErr != nil {
return fmt.Errorf("Could not update the record in the db: %w", dbErr)
}
return fmt.Errorf("An error occurred when trying to send the message to the broker: %w", err)
}
// Remove lock information and update state
rec.State = Delivered
rec.LockedOn = nil
rec.LockID = nil
rec.ProcessedOn = &now
err = d.store.UpdateRecordByID(rec)
if err != nil {
return fmt.Errorf("Could not update the record in the db: %w", err)
}
}
return nil
}
// lockUnprocessedEntities updates the messages with the current machine's lockID
func (d defaultRecordProcessor) lockUnprocessedEntities() error {
lockTime := d.time.Now().UTC()
lockErr := d.store.UpdateRecordLockByState(d.machineID, lockTime, PendingDelivery)
if lockErr != nil {
return lockErr
}
return nil
}