-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevent.go
165 lines (133 loc) · 3.39 KB
/
event.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package events
import (
"context"
"errors"
"time"
"github.com/google/uuid"
)
const (
TypeDomain EventType = "domain"
TypeApplication EventType = "application"
)
type EventName string
func (n EventName) String() string {
return string(n)
}
type EventType string
func (t EventType) String() string {
return string(t)
}
type HandlerResult struct {
LastProcessAttemptAt time.Time
LastError error
ProcessedAt *time.Time
}
type Event struct {
ID string
ArchivedAt *time.Time
BackoffUntil *time.Time
CorrelationID string // CorrelationID is a read-only field that is set by the storage layer.
Data map[string]any
EntityID *string
EntityName string
Errors int
HandlerResults map[HandlerName]*HandlerResult
Name EventName
ProcessedAt *time.Time
Timestamp time.Time
Type EventType
}
func NewApplicationEvent(name EventName, data map[string]any) (*Event, error) {
if len(name) == 0 {
return nil, errors.New("name is empty")
}
return &Event{
ID: uuid.Must(uuid.NewV7()).String(),
Type: TypeApplication,
Name: name,
Data: data,
Timestamp: time.Now(),
HandlerResults: map[HandlerName]*HandlerResult{},
}, nil
}
func NewDomainEvent(name EventName, entityID string, entityName string, data map[string]any) (*Event, error) {
if len(name) == 0 {
return nil, errors.New("name is empty")
}
return &Event{
ID: uuid.Must(uuid.NewV7()).String(),
Type: TypeDomain,
Name: EventName(name),
EntityID: &entityID,
EntityName: entityName,
Data: data,
Timestamp: time.Now(),
HandlerResults: map[HandlerName]*HandlerResult{},
}, nil
}
func (e *Event) Archive() {
e.ArchivedAt = Ptr(time.Now())
}
func (e *Event) Process(ctx context.Context, config *Config) error {
if e.Errors >= MaxEventErrors {
return errors.New("event has reached max errors")
}
if e.ProcessedAt != nil {
return errors.New("event is already processed")
}
for _, handler := range config.Handlers {
if result, ok := e.HandlerResults[handler.Name()]; ok {
if result.ProcessedAt != nil {
continue
}
}
now := time.Now()
err := handler.Do(ctx, e)
if err != nil {
e.HandlerResults[handler.Name()] = &HandlerResult{
LastProcessAttemptAt: now,
LastError: err,
}
} else {
e.HandlerResults[handler.Name()] = &HandlerResult{
LastProcessAttemptAt: now,
LastError: nil,
ProcessedAt: &now,
}
}
}
for _, result := range e.HandlerResults {
if result.ProcessedAt == nil {
e.Errors++
if e.Errors < MaxEventErrors {
var backoffUntil time.Time
if config.BackoffFn != nil {
backoffUntil = config.BackoffFn(e)
} else {
backoffUntil = DefaultBackoffFunc(e)
}
e.BackoffUntil = &backoffUntil
return ErrRetryable
}
e.BackoffUntil = nil
return errors.New("event has unprocessed handler")
}
}
now := time.Now()
e.ProcessedAt = &now
e.BackoffUntil = nil
return nil
}
func (e *Event) Reprocess() {
e.BackoffUntil = nil
e.Errors = 0
}
func (e *Event) UnprocessedHandlers() map[HandlerName]*HandlerResult {
out := make(map[HandlerName]*HandlerResult)
for handlerName, handlerResult := range e.HandlerResults {
if handlerResult.ProcessedAt == nil {
out[handlerName] = handlerResult
}
}
return out
}