forked from nyaruka/goflow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.go
531 lines (426 loc) · 13.1 KB
/
run.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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
package runs
import (
"encoding/json"
"fmt"
"time"
"github.com/nyaruka/goflow/flows"
"github.com/nyaruka/goflow/flows/events"
"github.com/nyaruka/goflow/flows/inputs"
"github.com/nyaruka/goflow/flows/waits"
"github.com/nyaruka/goflow/utils"
uuid "github.com/satori/go.uuid"
)
type flowRun struct {
uuid flows.RunUUID
contact *flows.Contact
contactUUID flows.ContactUUID
flow flows.Flow
flowUUID flows.FlowUUID
channel *flows.Channel
channelUUID flows.ChannelUUID
extra json.RawMessage
results *flows.Results
context flows.Context
status flows.RunStatus
wait flows.Wait
webhook *utils.RequestResponse
input flows.Input
event flows.Event
parent flows.FlowRunReference
child flows.FlowRunReference
session flows.Session
path []flows.Step
flowTranslations flows.FlowTranslations
translations flows.Translations
environment flows.FlowEnvironment
language utils.Language
createdOn time.Time
modifiedOn time.Time
expiresOn *time.Time
timesOutOn *time.Time
exitedOn *time.Time
}
func (r *flowRun) UUID() flows.RunUUID { return r.uuid }
func (r *flowRun) ContactUUID() flows.ContactUUID { return r.contactUUID }
func (r *flowRun) Contact() *flows.Contact { return r.contact }
// Hydrate prepares a deserialized run for executions
func (r *flowRun) Hydrate(env flows.FlowEnvironment) error {
// start with a fresh output if we don't have one
if r.session == nil {
r.ResetSession()
}
// save off our environment
r.environment = env
// set our flow
runFlow, err := env.GetFlow(r.FlowUUID())
if err != nil {
return err
}
r.flow = runFlow
// make sure we have a contact
runContact, err := env.GetContact(r.ContactUUID())
if err != nil {
return err
}
r.contact = runContact
// build our context
r.context = NewContextForContact(runContact, r)
// populate our run references
if r.parent != nil {
parent, err := env.GetRun(r.parent.UUID())
if err != nil {
return err
}
r.parent = newReferenceFromRun(parent.(*flowRun))
}
if r.child != nil {
child, err := env.GetRun(r.child.UUID())
if err != nil {
return err
}
r.child = newReferenceFromRun(child.(*flowRun))
}
return nil
}
func (r *flowRun) FlowUUID() flows.FlowUUID { return r.flowUUID }
func (r *flowRun) Flow() flows.Flow { return r.flow }
func (r *flowRun) ChannelUUID() flows.ChannelUUID { return r.channelUUID }
func (r *flowRun) Channel() *flows.Channel { return r.channel }
func (r *flowRun) SetChannel(channel *flows.Channel) {
r.channelUUID = channel.UUID()
r.channel = channel
}
func (r *flowRun) Context() flows.Context { return r.context }
func (r *flowRun) Environment() flows.FlowEnvironment { return r.environment }
func (r *flowRun) Results() *flows.Results { return r.results }
func (r *flowRun) Session() flows.Session { return r.session }
func (r *flowRun) SetSession(session flows.Session) {
r.session = session
r.session.AddRun(r)
}
func (r *flowRun) ResetSession() {
r.SetSession(newSession())
}
func (r *flowRun) IsComplete() bool {
return r.status != flows.StatusActive
}
func (r *flowRun) setStatus(status flows.RunStatus) {
r.status = status
r.setModifiedOn(time.Now().UTC())
}
func (r *flowRun) Exit(status flows.RunStatus) {
r.setStatus(status)
r.exitedOn = &r.modifiedOn
}
func (r *flowRun) Status() flows.RunStatus { return r.status }
func (r *flowRun) Parent() flows.FlowRunReference { return r.parent }
func (r *flowRun) Child() flows.FlowRunReference { return r.child }
func (r *flowRun) Wait() flows.Wait { return r.wait }
func (r *flowRun) SetWait(wait flows.Wait) { r.wait = wait }
func (r *flowRun) Input() flows.Input { return r.input }
func (r *flowRun) SetInput(input flows.Input) { r.input = input }
func (r *flowRun) SetEvent(event flows.Event) { r.event = event }
func (r *flowRun) Event() flows.Event { return r.event }
func (r *flowRun) AddEvent(s flows.Step, e flows.Event) {
now := time.Now().UTC()
e.SetCreatedOn(now)
e.SetStep(s.UUID())
fs := s.(*step)
fs.addEvent(e)
r.Session().AddEvent(e)
r.setModifiedOn(now)
}
func (r *flowRun) AddError(step flows.Step, err error) {
r.AddEvent(step, &events.ErrorEvent{Text: err.Error()})
}
func (r *flowRun) Path() []flows.Step { return r.path }
func (r *flowRun) CreateStep(node flows.Node) flows.Step {
now := time.Now().UTC()
step := &step{stepUUID: flows.StepUUID(uuid.NewV4().String()), nodeUUID: node.UUID(), arrivedOn: now}
r.path = append(r.path, step)
r.setModifiedOn(now)
return step
}
func (r *flowRun) ClearPath() {
r.path = nil
}
func (r *flowRun) Webhook() *utils.RequestResponse { return r.webhook }
func (r *flowRun) SetWebhook(rr *utils.RequestResponse) {
r.webhook = rr
r.setModifiedOn(time.Now().UTC())
}
func (r *flowRun) Extra() utils.JSONFragment {
return utils.NewJSONFragment([]byte(r.extra))
}
func (r *flowRun) SetExtra(extra json.RawMessage) { r.extra = extra }
func (r *flowRun) CreatedOn() time.Time { return r.createdOn }
func (r *flowRun) ModifiedOn() time.Time { return r.modifiedOn }
func (r *flowRun) setModifiedOn(now time.Time) { r.modifiedOn = now }
func (r *flowRun) ExpiresOn() *time.Time { return r.expiresOn }
func (r *flowRun) TimesOutOn() *time.Time { return r.timesOutOn }
func (r *flowRun) ExitedOn() *time.Time { return r.exitedOn }
func (r *flowRun) updateTranslations() {
if r.flowTranslations != nil {
r.translations = r.flowTranslations.GetTranslations(r.language)
} else {
r.translations = nil
}
}
func (r *flowRun) SetFlowTranslations(ft flows.FlowTranslations) {
r.flowTranslations = ft
r.updateTranslations()
}
func (r *flowRun) SetLanguage(lang utils.Language) {
r.language = lang
r.updateTranslations()
}
func (r *flowRun) GetText(uuid flows.UUID, key string, backdown string) string {
if r.translations == nil {
return backdown
}
return r.translations.GetText(uuid, key, backdown)
}
func (r *flowRun) GetTranslations(uuid flows.UUID, key string, backdown []string) []string {
if r.translations == nil {
return backdown
}
return r.translations.GetTranslations(uuid, key, backdown)
}
// NewRun initializes a new context and flow run for the passed in flow and contact
func NewRun(env flows.FlowEnvironment, flow flows.Flow, contact *flows.Contact, parent flows.FlowRun) flows.FlowRun {
now := time.Now().UTC()
r := &flowRun{
uuid: flows.RunUUID(uuid.NewV4().String()),
flowUUID: flow.UUID(),
contactUUID: contact.UUID(),
results: flows.NewResults(),
environment: env,
contact: contact,
flow: flow,
status: flows.StatusActive,
createdOn: now,
modifiedOn: now,
}
// create our new context
r.context = NewContextForContact(contact, r)
// set our session
if parent != nil {
parentRun := parent.(*flowRun)
r.parent = newReferenceFromRun(parentRun)
parentRun.child = newReferenceFromRun(r)
r.session = parent.Session()
} else {
r.session = newSession()
}
r.session.AddRun(r)
return r
}
func (r *flowRun) Resolve(key string) interface{} {
switch key {
case "channel":
return r.Channel()
case "contact":
return r.Contact()
case "extra":
return r.Extra()
case "flow":
return r.Flow()
case "status":
return r.Status()
case "results":
return r.Results()
case "created_on":
return r.CreatedOn()
case "exited_on":
return r.ExitedOn()
}
return fmt.Errorf("No field '%s' on run", key)
}
func (r *flowRun) Default() interface{} {
return r
}
var _ utils.VariableResolver = (*flowRun)(nil)
// String returns the default string value for this run, which is just our status
func (r *flowRun) String() string {
return string(r.status)
}
// runReference provides a standalone and serializable version of a run reference. When a run is written
// this is what is written and what will be read (and needed) when resuming that run
type runReference struct {
uuid flows.RunUUID
run *flowRun
}
// Resolve provides a more limited set of results for parent and child references
func (r *runReference) Resolve(key string) interface{} {
switch key {
case "channel_uuid":
return r.ChannelUUID()
case "contact_uuid":
return r.ContactUUID()
case "flow_uuid":
return r.FlowUUID()
case "status":
return r.Status()
case "results":
return r.Results()
case "created_on":
return r.CreatedOn()
case "exited_on":
return r.ExitedOn()
}
return fmt.Errorf("No field '%s' on run reference", key)
}
func (r *runReference) Default() interface{} {
return r
}
var _ utils.VariableResolver = (*runReference)(nil)
func (r *runReference) String() string {
return string(r.Status())
}
func (r *runReference) UUID() flows.RunUUID { return r.uuid }
func (r *runReference) FlowUUID() flows.FlowUUID { return r.run.flowUUID }
func (r *runReference) ContactUUID() flows.ContactUUID { return r.run.contactUUID }
func (r *runReference) ChannelUUID() flows.ChannelUUID { return r.run.channelUUID }
func (r *runReference) Results() *flows.Results { return r.run.results }
func (r *runReference) Status() flows.RunStatus { return r.run.status }
func (r *runReference) CreatedOn() time.Time { return r.run.createdOn }
func (r *runReference) ModifiedOn() time.Time { return r.run.modifiedOn }
func (r *runReference) ExitedOn() *time.Time { return r.run.exitedOn }
func (r *runReference) ExpiresOn() *time.Time { return r.run.expiresOn }
func (r *runReference) TimesOutOn() *time.Time { return r.run.timesOutOn }
func newReferenceFromRun(r *flowRun) *runReference {
return &runReference{
uuid: r.UUID(),
run: r,
}
}
//------------------------------------------------------------------------------------------
// JSON Encoding / Decoding
//------------------------------------------------------------------------------------------
// ReadRun decodes a run from the passed in JSON
func ReadRun(data json.RawMessage) (flows.FlowRun, error) {
run := &flowRun{}
err := json.Unmarshal(data, run)
if err == nil {
// err = run.Validate()
}
return run, err
}
type runEnvelope struct {
UUID flows.RunUUID `json:"uuid"`
Flow flows.FlowUUID `json:"flow_uuid"`
Channel flows.ChannelUUID `json:"channel_uuid"`
Contact flows.ContactUUID `json:"contact_uuid"`
Path []*step `json:"path"`
Status flows.RunStatus `json:"status"`
Input *utils.TypedEnvelope `json:"input,omitempty"`
Wait *utils.TypedEnvelope `json:"wait,omitempty"`
Event *utils.TypedEnvelope `json:"event,omitempty"`
Parent flows.RunUUID `json:"parent_uuid,omitempty"`
Child flows.RunUUID `json:"child_uuid,omitempty"`
Results *flows.Results `json:"results,omitempty"`
Webhook *utils.RequestResponse `json:"webhook,omitempty"`
Extra json.RawMessage `json:"extra,omitempty"`
CreatedOn time.Time `json:"created_on"`
ModifiedOn time.Time `json:"modified_on"`
ExpiresOn *time.Time `json:"expires_on"`
TimesOutOn *time.Time `json:"timesout_on"`
ExitedOn *time.Time `json:"exited_on"`
}
func (r *flowRun) UnmarshalJSON(data []byte) error {
var envelope runEnvelope
var err error
err = json.Unmarshal(data, &envelope)
if err != nil {
return err
}
r.uuid = envelope.UUID
r.contactUUID = envelope.Contact
r.flowUUID = envelope.Flow
r.channelUUID = envelope.Channel
r.status = envelope.Status
r.createdOn = envelope.CreatedOn
r.modifiedOn = envelope.ModifiedOn
r.expiresOn = envelope.ExpiresOn
r.timesOutOn = envelope.TimesOutOn
r.exitedOn = envelope.ExitedOn
r.extra = envelope.Extra
if envelope.Parent != "" {
r.parent = &runReference{uuid: envelope.Parent}
}
if envelope.Child != "" {
r.child = &runReference{uuid: envelope.Child}
}
if envelope.Input != nil {
r.input, err = inputs.InputFromEnvelope(envelope.Input)
if err != nil {
return err
}
}
if envelope.Wait != nil {
r.wait, err = waits.WaitFromEnvelope(envelope.Wait)
if err != nil {
return err
}
}
if envelope.Event != nil {
r.event, err = events.EventFromEnvelope(envelope.Event)
if err != nil {
return err
}
}
if envelope.Results != nil {
r.results = envelope.Results
} else {
r.results = flows.NewResults()
}
if envelope.Webhook != nil {
r.webhook = envelope.Webhook
}
// read in our path
r.path = make([]flows.Step, len(envelope.Path))
for i, step := range envelope.Path {
r.path[i] = step
}
return err
}
func (r *flowRun) MarshalJSON() ([]byte, error) {
var re runEnvelope
var err error
re.UUID = r.uuid
re.Flow = r.FlowUUID()
re.Contact = r.ContactUUID()
re.Channel = r.ChannelUUID()
re.Status = r.status
re.CreatedOn = r.createdOn
re.ModifiedOn = r.modifiedOn
re.ExpiresOn = r.expiresOn
re.TimesOutOn = r.timesOutOn
re.ExitedOn = r.exitedOn
re.Results = r.results
re.Extra = r.extra
re.Webhook = r.webhook
if r.parent != nil {
re.Parent = r.parent.UUID()
}
if r.child != nil {
re.Child = r.child.UUID()
}
re.Input, err = utils.EnvelopeFromTyped(r.input)
if err != nil {
return nil, err
}
re.Wait, err = utils.EnvelopeFromTyped(r.wait)
if err != nil {
return nil, err
}
re.Event, err = utils.EnvelopeFromTyped(r.event)
if err != nil {
return nil, err
}
re.Path = make([]*step, len(r.path))
for i, s := range r.path {
re.Path[i] = s.(*step)
}
return json.Marshal(re)
}