-
Notifications
You must be signed in to change notification settings - Fork 139
/
ingestor.go
164 lines (128 loc) · 4.25 KB
/
ingestor.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
package ingestor
import (
"context"
"fmt"
"github.com/hatchet-dev/hatchet/internal/datautils"
"github.com/hatchet-dev/hatchet/internal/msgqueue"
"github.com/hatchet-dev/hatchet/internal/repository"
"github.com/hatchet-dev/hatchet/internal/repository/prisma/dbsqlc"
"github.com/hatchet-dev/hatchet/internal/repository/prisma/sqlchelpers"
"github.com/hatchet-dev/hatchet/internal/services/ingestor/contracts"
"github.com/hatchet-dev/hatchet/internal/services/shared/tasktypes"
"github.com/hatchet-dev/hatchet/internal/telemetry"
)
type Ingestor interface {
contracts.EventsServiceServer
IngestEvent(ctx context.Context, tenantId, eventName string, data []byte) (*dbsqlc.Event, error)
IngestReplayedEvent(ctx context.Context, tenantId string, replayedEvent *dbsqlc.Event) (*dbsqlc.Event, error)
}
type IngestorOptFunc func(*IngestorOpts)
type IngestorOpts struct {
eventRepository repository.EventEngineRepository
logRepository repository.LogsEngineRepository
mq msgqueue.MessageQueue
}
func WithEventRepository(r repository.EventEngineRepository) IngestorOptFunc {
return func(opts *IngestorOpts) {
opts.eventRepository = r
}
}
func WithLogRepository(r repository.LogsEngineRepository) IngestorOptFunc {
return func(opts *IngestorOpts) {
opts.logRepository = r
}
}
func WithMessageQueue(mq msgqueue.MessageQueue) IngestorOptFunc {
return func(opts *IngestorOpts) {
opts.mq = mq
}
}
func defaultIngestorOpts() *IngestorOpts {
return &IngestorOpts{}
}
type IngestorImpl struct {
contracts.UnimplementedEventsServiceServer
eventRepository repository.EventEngineRepository
logRepository repository.LogsEngineRepository
mq msgqueue.MessageQueue
}
func NewIngestor(fs ...IngestorOptFunc) (Ingestor, error) {
opts := defaultIngestorOpts()
for _, f := range fs {
f(opts)
}
if opts.eventRepository == nil {
return nil, fmt.Errorf("event repository is required. use WithEventRepository")
}
if opts.logRepository == nil {
return nil, fmt.Errorf("log repository is required. use WithLogRepository")
}
if opts.mq == nil {
return nil, fmt.Errorf("task queue is required. use WithMessageQueue")
}
return &IngestorImpl{
eventRepository: opts.eventRepository,
logRepository: opts.logRepository,
mq: opts.mq,
}, nil
}
func (i *IngestorImpl) IngestEvent(ctx context.Context, tenantId, key string, data []byte) (*dbsqlc.Event, error) {
ctx, span := telemetry.NewSpan(ctx, "ingest-event")
defer span.End()
event, err := i.eventRepository.CreateEvent(ctx, &repository.CreateEventOpts{
TenantId: tenantId,
Key: key,
Data: data,
})
if err != nil {
return nil, fmt.Errorf("could not create event: %w", err)
}
telemetry.WithAttributes(span, telemetry.AttributeKV{
Key: "event_id",
Value: event.ID,
})
err = i.mq.AddMessage(context.Background(), msgqueue.EVENT_PROCESSING_QUEUE, eventToTask(event))
if err != nil {
return nil, fmt.Errorf("could not add event to task queue: %w", err)
}
return event, nil
}
func (i *IngestorImpl) IngestReplayedEvent(ctx context.Context, tenantId string, replayedEvent *dbsqlc.Event) (*dbsqlc.Event, error) {
ctx, span := telemetry.NewSpan(ctx, "ingest-replayed-event")
defer span.End()
replayedId := sqlchelpers.UUIDToStr(replayedEvent.ID)
event, err := i.eventRepository.CreateEvent(ctx, &repository.CreateEventOpts{
TenantId: tenantId,
Key: replayedEvent.Key,
Data: replayedEvent.Data,
ReplayedEvent: &replayedId,
})
if err != nil {
return nil, fmt.Errorf("could not create event: %w", err)
}
err = i.mq.AddMessage(context.Background(), msgqueue.EVENT_PROCESSING_QUEUE, eventToTask(event))
if err != nil {
return nil, fmt.Errorf("could not add event to task queue: %w", err)
}
return event, nil
}
func eventToTask(e *dbsqlc.Event) *msgqueue.Message {
eventId := sqlchelpers.UUIDToStr(e.ID)
tenantId := sqlchelpers.UUIDToStr(e.TenantId)
payloadTyped := tasktypes.EventTaskPayload{
EventId: eventId,
EventKey: e.Key,
EventData: string(e.Data),
}
payload, _ := datautils.ToJSONMap(payloadTyped)
metadata, _ := datautils.ToJSONMap(tasktypes.EventTaskMetadata{
EventKey: e.Key,
TenantId: tenantId,
})
return &msgqueue.Message{
ID: "event",
Payload: payload,
Metadata: metadata,
Retries: 3,
}
}