forked from shammishailaj/go-astiencoder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathworkflow_recording.go
137 lines (116 loc) · 2.59 KB
/
workflow_recording.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
package astiencoder
import (
"context"
"encoding/base64"
"encoding/csv"
"encoding/json"
"fmt"
"io/ioutil"
"os"
"strconv"
"time"
"github.com/asticode/go-astikit"
)
type WorkflowRecording struct {
c *astikit.Chan
cancel context.CancelFunc
ctx context.Context
l astikit.SeverityLogger
o WorkflowRecordingOptions
path string
w *csv.Writer
wf *Workflow
}
type WorkflowRecordingOptions struct {
Dst string
Logger astikit.StdLogger
}
func (w *Workflow) NewRecording(o WorkflowRecordingOptions) (r *WorkflowRecording) {
// Create recording
r = &WorkflowRecording{
c: astikit.NewChan(astikit.ChanOptions{
ProcessAll: true,
}),
l: astikit.AdaptStdLogger(o.Logger),
o: o,
wf: w,
}
// Adapt event handler
serverEventHandlerAdapter(w.eh, func(name EventName, payload interface{}) {
// Recording not started
if r.ctx == nil {
return
}
// Write
if err := r.write(name, payload); err != nil {
r.l.Error(fmt.Errorf("astiencoder: writing to recording failed: %w", err))
}
})
return
}
func (r *WorkflowRecording) Start(ctx context.Context) (err error) {
// Recording already started
if r.ctx != nil {
return
}
// Create context
r.ctx, r.cancel = context.WithCancel(ctx)
// Make sure to reset context
defer func() {
r.cancel()
r.ctx = nil
r.cancel = nil
}()
// Create destination
var f *os.File
if r.o.Dst != "" {
if f, err = os.Create(r.o.Dst); err != nil {
err = fmt.Errorf("astiencoder: creating %s failed: %w", r.o.Dst, err)
return
}
} else {
if f, err = ioutil.TempFile("", "astiencoder-recording-*.csv"); err != nil {
err = fmt.Errorf("astiencoder: creating temp file failed: %w", err)
return
}
}
defer f.Close()
// Update path
r.path = f.Name()
// Create csv writer
r.w = csv.NewWriter(f)
// Write init
if err = r.write("init", newServerWorkflow(r.wf)); err != nil {
err = fmt.Errorf("astiencoder: adding init failed: %w", err)
return
}
// Start chan
r.c.Start(r.ctx)
// Reset chan
r.c.Reset()
// Flush csv
r.w.Flush()
// Reset csv writer
r.w = nil
return
}
func (r *WorkflowRecording) write(name EventName, payload interface{}) (err error) {
// Marshal payload
var b []byte
if b, err = json.Marshal(payload); err != nil {
err = fmt.Errorf("astiencoder: marshaling failed: %w", err)
return
}
// Write
r.c.Add(func() {
r.w.Write([]string{strconv.Itoa(int(time.Now().UTC().Unix())), string(name), base64.StdEncoding.EncodeToString(b)}) //nolint:errcheck
r.w.Flush()
})
return
}
func (r *WorkflowRecording) Stop() {
// Cancel context
if r.cancel != nil {
r.cancel()
}
}