-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmessaging.go
348 lines (274 loc) · 8.26 KB
/
messaging.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
package messaging
import (
"encoding/json"
"fmt"
"time"
"github.com/ylem-co/shared-messaging/customers"
"github.com/ylem-co/shared-messaging/macaw"
"github.com/ylem-co/shared-messaging/sources"
"github.com/google/uuid"
log "github.com/sirupsen/logrus"
)
const (
HeaderMessageName = "X-Message-Name"
DateTimeFormat = "2006-01-02 15:04:05.123456"
// Task types
TaskTypeQuery = "query"
TaskTypeCondition = "condition"
TaskTypeAggregator = "aggregator"
TaskTypeTransformer = "transformer"
TaskTypeNotification = "notification"
TaskTypeApiCall = "api_call"
TaskTypeForEach = "for_each"
TaskTypeMerge = "merge"
TaskTypeFilter = "filter"
TaskTypeRunPipeline = "run_pipeline"
TaskTypeExternalTrigger = "external_trigger"
TaskTypeCode = "code"
TaskTypeOpenapiGpt = "gpt"
TaskTypeProcessor = "processor"
// Pipeline types
PipelineTypeGeneric = "generic"
PipelineTypeMetric = "metric"
// The codes here should be up to 9999. This is general error codes space
ErrorMessageDeserialization = 100
ErrorInternal = 101
ErrorBadRequest = 200
ErrorUnknownTaskType = 9999
IdListTypeEnabled = "enabled"
IdListTypeDisabled = "disabled"
)
type Envelope struct {
Headers map[string]string `json:"headers"`
Msg interface{} `json:"message"`
}
func (e *Envelope) WithHeader(header string, value string) *Envelope {
newEnvelope := NewEnvelope(e.Msg)
for k, v := range e.Headers {
newEnvelope.Headers[k] = v
}
return newEnvelope
}
func (e *Envelope) SetMsg(msg interface{}) {
e.Msg = msg
e.Headers[HeaderMessageName] = getMessageName(msg)
}
func (e *Envelope) UnmarshalJSON(input []byte) error {
s := &struct {
Headers map[string]string `json:"headers"`
}{}
err := json.Unmarshal(input, s)
if err != nil {
return err
}
val, ok := s.Headers[HeaderMessageName]
if !ok || val == "" {
return fmt.Errorf("mandatory header %s is not found", HeaderMessageName)
}
msg := newMsg(val)
s2 := struct {
Headers map[string]string `json:"headers"`
Msg interface{} `json:"message"`
}{
Msg: msg,
}
err = json.Unmarshal(input, &s2)
if err != nil {
return err
}
e.Headers = s2.Headers
e.SetMsg(s2.Msg)
return nil
}
func NewEnvelope(msg interface{}) *Envelope {
e := &Envelope{
Headers: make(map[string]string),
}
e.SetMsg(msg)
return e
}
func newMsg(messageName string) interface{} {
switch messageName {
case TaskRunQueryMessageName:
return &RunQueryTask{}
case TaskRunForEachMessageName:
return &RunForEachTask{}
case TaskCheckConditionMessageName:
return &CheckConditionTask{}
case TaskAggregateDataMessageName:
return &AggregateDataTask{}
case TaskTransformDataMessageName:
return &TransformDataTask{}
case TaskCallApiMessageName:
return &CallApiTask{}
case TaskSendNotificationMessageName:
return &SendNotificationTask{}
case TaskMergeMessageName:
return &MergeTask{}
case TaskFilterMessageName:
return &FilterTask{}
case TaskRunResultMessageName:
return &TaskRunResult{}
case customers.CustomerPasswordRecoveryRequestedMessageName:
return &customers.CustomerPasswordRecoveryRequested{}
case customers.CustomerRegisteredMessageName:
return &customers.CustomerRegistered{}
case customers.CustomerSendInviteMessageName:
return &customers.CustomerSendInvite{}
case sources.SourceStatusToggledMessageName:
return &sources.SourceStatusToggled{}
case TaskExternalTriggerMessageName:
return &ExternalTriggerTask{}
case TaskExecuteCodeMessageName:
return &ExecuteCodeTask{}
case TaskCallOpenapiGptMessageName:
return &CallOpenapiGptTask{}
case TaskProcessDataMessageName:
return &ProcessDataTask{}
case TaskRunPipelineMessageName:
return &RunPipelineTask{}
}
return nil
}
func getMessageName(msg interface{}) string {
switch in := msg.(type) {
case *RunQueryTask:
return TaskRunQueryMessageName
case *RunForEachTask:
return TaskRunForEachMessageName
case *CheckConditionTask:
return TaskCheckConditionMessageName
case *AggregateDataTask:
return TaskAggregateDataMessageName
case *TransformDataTask:
return TaskTransformDataMessageName
case *SendNotificationTask:
return TaskSendNotificationMessageName
case *CallApiTask:
return TaskCallApiMessageName
case *MergeTask:
return TaskMergeMessageName
case *FilterTask:
return TaskFilterMessageName
case *TaskRunResult:
return TaskRunResultMessageName
case *ExternalTriggerTask:
return TaskExternalTriggerMessageName
case *ExecuteCodeTask:
return TaskExecuteCodeMessageName
case *CallOpenapiGptTask:
return TaskCallOpenapiGptMessageName
case *ProcessDataTask:
return TaskProcessDataMessageName
case *RunPipelineTask:
return TaskRunPipelineMessageName
case *customers.CustomerRegistered,
*customers.CustomerPasswordRecoveryRequested,
*customers.CustomerSendInvite,
*sources.SourceStatusToggled:
return in.(macaw.Message).GetMacawMessageKey()
default:
return ""
}
}
type MessageCodec struct{}
func (c *MessageCodec) Encode(value interface{}) ([]byte, error) {
return json.Marshal(value)
}
func (c *MessageCodec) Decode(data []byte) (interface{}, error) {
e := &Envelope{}
err := json.Unmarshal(data, e)
if err != nil {
log.Errorf("Message decoding failure: %s", err)
}
return e, nil
}
const (
TaskRunResultMessageName = "result.task_run"
ErrorSeverityError = "error"
ErrorSeverityWarning = "warning"
)
type Task struct {
PipelineType string `json:"pipeline_type"`
PipelineRunUuid uuid.UUID `json:"pipeline_run_uuid"`
TaskRunUuid uuid.UUID `json:"task_run_uuid"`
TaskUuid uuid.UUID `json:"task_uuid"`
PipelineUuid uuid.UUID `json:"pipeline_uuid"`
OrganizationUuid uuid.UUID `json:"organization_uuid"`
CreatorUuid uuid.UUID `json:"creator_uuid"`
TaskName string `json:"task_name"`
IsInitialTask bool `json:"is_initial_task"`
IsFinalTask bool `json:"is_final_task"`
Input []byte `json:"input"`
Meta Meta `json:"meta"`
}
type TaskInterface interface {
GetPipelineUuid() uuid.UUID
GetPipelineRunUuid() uuid.UUID
GetOrganizationUuid() uuid.UUID
}
func (t *Task) GetPipelineUuid() uuid.UUID {
return t.PipelineUuid
}
func (t *Task) GetPipelineRunUuid() uuid.UUID {
return t.PipelineRunUuid
}
func (t *Task) GetOrganizationUuid() uuid.UUID {
return t.OrganizationUuid
}
type Meta struct {
SqlQueryColumnOrder []string
InputCount int64 // number of inputs in "merge" block
EnvVars map[string]interface{}
PipelineRunConfig PipelineRunConfig
}
type PipelineRunConfig struct {
TaskIds IdList `json:"task_ids"`
TaskTriggerIds IdList `json:"task_trigger_ids"`
}
type IdList struct {
Type string `json:"type"`
Ids []string `json:"ids"`
}
type TaskRunError struct {
Code uint
Severity string
Message string
}
type TaskRunResult struct {
PipelineType string `json:"pipeline_type"`
PipelineRunUuid uuid.UUID `json:"pipeline_run_uuid"`
TaskRunUuid uuid.UUID `json:"task_run_uuid"`
IsSuccessful bool `json:"is_successful"`
Errors []TaskRunError `json:"errors"`
Uuid uuid.UUID `json:"uuid"`
TaskUuid uuid.UUID `json:"task_uuid"`
TaskType string `json:"task_type"`
PipelineUuid uuid.UUID `json:"pipeline_uuid"`
OrganizationUuid uuid.UUID `json:"organization_uuid"`
CreatorUuid uuid.UUID `json:"creator_uuid"`
IsInitialTask bool `json:"is_initial_task"`
IsFinalTask bool `json:"is_final_task"`
Input []byte `json:"input"`
Output []byte `json:"output"`
ExecutedAt time.Time `json:"executedAt"`
Duration time.Duration `json:"duration"`
Meta Meta `json:"meta"`
}
func (tr TaskRunResult) MarshalJSON() ([]byte, error) {
type Alias TaskRunResult
return json.Marshal(&struct {
ExecutedAt string `json:"executed_at"`
Alias
}{
ExecutedAt: tr.ExecutedAt.Format(DateTimeFormat),
Alias: Alias(tr),
})
}
func NewTaskRunResult(taskUuid uuid.UUID) *TaskRunResult {
return &TaskRunResult{
Uuid: uuid.New(),
TaskUuid: taskUuid,
Output: make([]byte, 0),
}
}