-
Notifications
You must be signed in to change notification settings - Fork 9
/
attachments.go
232 lines (187 loc) · 5.11 KB
/
attachments.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
// Copyright 2022 Namespace Labs Inc; All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
package tasks
import (
"bytes"
"io"
"sync"
"namespacelabs.dev/foundation/internal/fnerrors"
fnsync "namespacelabs.dev/foundation/internal/sync"
"namespacelabs.dev/foundation/std/tasks/idtypes"
"namespacelabs.dev/go-ids"
)
type EventAttachments struct {
actionID ActionID
sink ActionSink
mu sync.Mutex // Protects below.
ResultData
// For the time being we just keep everything in memory for simplicity.
buffers map[string]attachedBuffer
insertionOrder []OutputName
}
type readerWriter interface {
Writer() io.Writer
Reader() io.ReadCloser
Snapshot(copy bool) []byte
}
type attachedBuffer struct {
buffer readerWriter
writer io.Writer
id string
name string
contentType string
}
func (ev *EventAttachments) IsRecording() bool { return ev != nil }
func (ev *EventAttachments) init() {
if ev.buffers == nil {
ev.buffers = map[string]attachedBuffer{}
}
}
func (ev *EventAttachments) seal() {
ev.mu.Lock()
defer ev.mu.Unlock()
for name, b := range ev.buffers {
if cb, ok := b.buffer.(*fnsync.ByteBuffer); ok {
sealed := cb.Seal()
ev.buffers[name] = attachedBuffer{
id: ids.NewRandomBase62ID(8),
buffer: sealed,
writer: sealed.Writer(),
name: b.name,
contentType: b.contentType,
}
}
}
}
func (ev *EventAttachments) attach(name OutputName, body []byte) {
ev.mu.Lock()
defer ev.mu.Unlock()
ev.init()
if _, ok := ev.buffers[name.computed]; !ok {
ev.insertionOrder = append(ev.insertionOrder, name)
}
ev.buffers[name.computed] = attachedBuffer{
id: ids.NewRandomBase62ID(8),
buffer: fnsync.Seal(body),
writer: io.Discard,
name: name.name,
contentType: name.contentType,
}
}
func (ev *EventAttachments) Attach(name OutputName, body []byte) {
if ev != nil {
ev.attach(name, body)
ev.sink.AttachmentsUpdated(ev.actionID, nil)
}
}
func (ev *EventAttachments) AttachSerializable(name, modifier string, body interface{}) error {
if ev == nil {
return fnerrors.InternalError("no running action while attaching serializable %q", name)
}
if !ev.IsRecording() {
return nil
}
bytes, err := idtypes.SerializeToBytes(body)
if err != nil {
return fnerrors.BadInputError("failed to serialize payload to bytes: %w", err)
}
contentType := "application/json"
if modifier != "" {
contentType += "+" + modifier
}
ev.Attach(Output(name, contentType), bytes)
return nil
}
func (ev *EventAttachments) addResult(key string, msg interface{}) ResultData {
ev.mu.Lock()
defer ev.mu.Unlock()
for _, item := range ev.Items {
if item.Name == key {
item.Msg = msg
return ev.ResultData
}
}
// Not found, add a new result.
ev.Items = append(ev.Items, &ActionArgument{key, msg})
return ev.ResultData
}
func (ev *EventAttachments) AddResult(key string, msg interface{}) *EventAttachments {
if ev != nil {
data := ev.addResult(key, msg)
// XXX this is racy as we don't guarantee the AttachmentsUpdated order if the caller
// is using multiple go-routines to update outputs.
ev.sink.AttachmentsUpdated(ev.actionID, &data)
}
return ev
}
func (ev *EventAttachments) SetProgress(p ActionProgress) *EventAttachments {
if ev != nil {
ev.mu.Lock()
ev.Progress = p
copy := ev.ResultData
ev.mu.Unlock()
ev.sink.AttachmentsUpdated(ev.actionID, ©)
}
return ev
}
func (ev *EventAttachments) ReaderByOutputName(outputName OutputName) io.ReadCloser {
return ev.ReaderByName(outputName.name)
}
func (ev *EventAttachments) ReaderByName(name string) io.ReadCloser {
if ev != nil {
ev.mu.Lock()
defer ev.mu.Unlock()
for _, b := range ev.buffers {
if b.name == name {
return b.buffer.Reader()
}
}
}
return io.NopCloser(bytes.NewReader(nil))
}
func (ev *EventAttachments) ensureOutput(name OutputName, outputType idtypes.CatOutputType, addIfMissing bool) (io.Writer, bool) {
if ev == nil {
return fnsync.Discard, false
}
ev.mu.Lock()
defer ev.mu.Unlock()
if !addIfMissing && ev.buffers == nil {
return fnsync.Discard, false
}
ev.init()
added := false
if _, ok := ev.buffers[name.computed]; !ok {
if !addIfMissing {
return fnsync.Discard, false
}
buf := fnsync.NewByteBuffer()
out := buf.Writer()
if sinkOutput := ev.sink.Output(name.name, name.contentType, outputType); sinkOutput != nil {
out = io.MultiWriter(out, sinkOutput)
}
ev.buffers[name.computed] = attachedBuffer{
id: ids.NewRandomBase62ID(8),
buffer: buf,
writer: out,
name: name.name,
contentType: name.contentType,
}
ev.insertionOrder = append(ev.insertionOrder, name)
added = true
}
return ev.buffers[name.computed].writer, added
}
func (ev *EventAttachments) Output(name OutputName, cat idtypes.CatOutputType) io.Writer {
w, added := ev.ensureOutput(name, cat, true)
if added {
ev.sink.AttachmentsUpdated(ev.actionID, nil)
}
return w
}
func (ev *EventAttachments) ActionID() ActionID {
if ev == nil {
return ""
}
return ev.actionID
}