-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtask.go
More file actions
363 lines (329 loc) · 9.31 KB
/
task.go
File metadata and controls
363 lines (329 loc) · 9.31 KB
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
package taskgraph
import (
"context"
set "github.com/deckarep/golang-set/v2"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/trace"
)
// A TaskSet defines a nestable collection of tasks. A Task fulfils the TaskSet interface, acting as
// a singleton set.
type TaskSet interface {
Tasks() []Task
}
type taskset []TaskSet
func (ts taskset) Tasks() []Task {
var result []Task
for _, el := range ts {
result = append(result, el.Tasks()...)
}
return result
}
// NewTaskSet creates a new TaskSet from Tasks or TaskSets (or a combination of both).
func NewTaskSet(els ...TaskSet) TaskSet {
return taskset(els)
}
// A Task represents a small unit of work within the graph (tasks form the nodes of the graph).
type Task interface {
// A task can be considered to be a singleton set.
TaskSet
// Name returns the name of the task given when the task was created.
Name() string
// Depends returns the IDs of the keys on which this task depends (i.e. the keys which must be
// provided as a graph input or by another task before this task can be executed)
Depends() []ID
// Provides returns the IDs of the keys for which this task provides bindings (i.e. the list of
// Bindings returned by Execute must exactly match this list aside from ordering).
Provides() []ID
// Execute performs the unit of work for this task, consuming its dependencies from the given
// Binder, and returning Bindings for each key the task has declared that it provides. Any error
// returned from Execute() will terminate the processing of the entire graph.
Execute(context.Context, Binder) ([]Binding, error)
// Location returns the file and line where this task was defined.
Location() string
}
type task struct {
name string
depends []ID
provides []ID
fn func(context.Context, Binder) ([]Binding, error)
location string
}
func (t *task) Tasks() []Task {
return []Task{t}
}
func (t *task) Name() string {
return t.name
}
func (t *task) Depends() []ID {
return t.depends
}
func (t *task) Provides() []ID {
return t.provides
}
func (t *task) Execute(ctx context.Context, b Binder) ([]Binding, error) {
return t.fn(ctx, b)
}
func (t *task) Location() string {
return t.location
}
// NewTask builds a task with any number of inputs and outputs.
func NewTask(
name string,
fn func(context.Context, Binder) ([]Binding, error),
depends, provides []ID,
) Task {
return &task{
name: name,
depends: depends,
provides: provides,
fn: fn,
location: getLocation(2),
}
}
// NoOutputTask builds a task which may consume inputs but produces no output bindings.
func NoOutputTask(name string, fn func(ctx context.Context, b Binder) error, depends ...ID) Task {
return &task{
name: name,
depends: depends,
fn: func(ctx context.Context, b Binder) ([]Binding, error) {
return nil, fn(ctx, b)
},
location: getLocation(2),
}
}
// SimpleTask builds a task which produces a single output binding.
func SimpleTask[T any](
name string,
key Key[T],
fn func(ctx context.Context, b Binder) (T, error),
depends ...ID,
) Task {
return &task{
name: name,
depends: depends,
provides: []ID{key.ID()},
fn: func(ctx context.Context, b Binder) ([]Binding, error) {
val, err := fn(ctx, b)
if err != nil {
return nil, err
}
return []Binding{key.Bind(val)}, nil
},
location: getLocation(2),
}
}
// SimpleTask1 builds a task from a function taking a single argument and returning a single value plus an error.
func SimpleTask1[A1, Res any](
name string,
resKey Key[Res],
fn func(ctx context.Context, arg1 A1) (Res, error),
depKey1 ReadOnlyKey[A1],
) Task {
return &task{
name: name,
depends: []ID{depKey1.ID()},
provides: []ID{resKey.ID()},
fn: func(ctx context.Context, b Binder) ([]Binding, error) {
arg1, err := depKey1.Get(b)
if err != nil {
return nil, err
}
res, err := fn(ctx, arg1)
if err != nil {
return nil, err
}
return []Binding{resKey.Bind(res)}, nil
},
location: getLocation(2),
}
}
// SimpleTask2 builds a task from a function taking two arguments and returning a single value plus an error.
func SimpleTask2[A1, A2, Res any](
name string,
resKey Key[Res],
fn func(ctx context.Context, arg1 A1, arg2 A2) (Res, error),
depKey1 ReadOnlyKey[A1],
depKey2 ReadOnlyKey[A2],
) Task {
return &task{
name: name,
depends: []ID{depKey1.ID(), depKey2.ID()},
provides: []ID{resKey.ID()},
fn: func(ctx context.Context, b Binder) ([]Binding, error) {
arg1, err := depKey1.Get(b)
if err != nil {
return nil, err
}
arg2, err := depKey2.Get(b)
if err != nil {
return nil, err
}
res, err := fn(ctx, arg1, arg2)
if err != nil {
return nil, err
}
return []Binding{resKey.Bind(res)}, nil
},
location: getLocation(2),
}
}
// Condition defines a condition for a Conditional task.
type Condition interface {
// Evaluate should return whether the conditional task should be executed.
Evaluate(ctx context.Context, b Binder) (bool, error)
// Deps should return the IDs of the keys used by the Evaluate function.
Deps() []ID
// Keys returns the keys in the conditional
Keys() []ReadOnlyKey[bool]
}
// ConditionAnd evaluates to true if and only if all of the keys it contains are bound to true.
type ConditionAnd []ReadOnlyKey[bool]
// Evaluate is Condition.Evaluate.
func (ca ConditionAnd) Evaluate(_ context.Context, b Binder) (bool, error) {
for _, k := range ca {
v, err := k.Get(b)
if err != nil {
return false, err
}
if !v {
return false, nil
}
}
return true, nil
}
// Deps is Condition.Deps
func (ca ConditionAnd) Deps() []ID {
var deps []ID
for _, k := range ca {
deps = append(deps, k.ID())
}
return deps
}
// Keys returns the keys in the conditional
func (ca ConditionAnd) Keys() []ReadOnlyKey[bool] {
return ca
}
// ConditionOr evaluates to true if any of the keys it contains are bound to true.
type ConditionOr []ReadOnlyKey[bool]
// Evaluate is Condition.Evaluate.
func (co ConditionOr) Evaluate(_ context.Context, b Binder) (bool, error) {
for _, k := range co {
v, err := k.Get(b)
if err != nil {
return false, err
}
if v {
return true, nil
}
}
return false, nil
}
// Deps is Condition.Deps
func (co ConditionOr) Deps() []ID {
var deps []ID
for _, k := range co {
deps = append(deps, k.ID())
}
return deps
}
// Keys returns the keys in the conditional
func (co ConditionOr) Keys() []ReadOnlyKey[bool] {
return co
}
// Conditional wraps tasks such that they are only run if given Condition evaluates to true. If it
// evaluates to false, the bindings in DefaultBindings are used, with any missing keys provided by
// the wrapped tasks bound as absent.
//
// Note that the tasks will not run until all of the wrapped task's dependencies and all of the
// condition's dependencies have been bound.
//
// To run tasks if keys of any type have been bound to some value (i.e. not bound as absent), use
// Presence() to wrap the key. To check for specific values, use Mapped() to wrap the key.
type Conditional struct {
NamePrefix string
Wrapped TaskSet
Condition Condition
DefaultBindings []Binding
location string
}
// Locate annotates the Conditional with its location in the source code, to make error messages
// easier to understand. Calling it is required.
func (c Conditional) Locate() Conditional {
c.location = getLocation(2)
return c
}
const (
traceTaskgraphConditionalPrefix = "taskgraph.conditional."
)
// Tasks satisfies TaskSet.Tasks.
func (c Conditional) Tasks() []Task {
defaultBindingsMap := map[ID]Binding{}
for _, b := range c.DefaultBindings {
defaultBindingsMap[b.ID()] = b
}
var res []Task
for _, t := range c.Wrapped.Tasks() {
// t is captured by the fn closure below
t := t
allDeps := set.NewSet[ID](t.Depends()...)
allDeps.Append(c.Condition.Deps()...)
res = append(res, &task{
name: c.NamePrefix + t.Name(),
depends: allDeps.ToSlice(),
provides: t.Provides(),
fn: func(ctx context.Context, b Binder) ([]Binding, error) {
shouldExecute, err := c.Condition.Evaluate(ctx, b)
if err != nil {
return nil, err
}
span := trace.SpanFromContext(ctx)
span.SetAttributes(
attribute.Bool(traceTaskgraphConditionalPrefix+"execute", shouldExecute),
)
for _, k := range c.Condition.Keys() {
v, err := k.Get(b)
if err != nil {
span.SetAttributes(
attribute.String(
traceTaskgraphConditionalPrefix+k.ID().String(),
err.Error(),
),
)
} else {
span.SetAttributes(
attribute.Bool(traceTaskgraphConditionalPrefix+k.ID().String(), v),
)
}
}
if shouldExecute {
return t.Execute(ctx, b)
}
var res []Binding
for _, id := range t.Provides() {
if b, ok := defaultBindingsMap[id]; ok {
res = append(res, b)
} else {
res = append(res, bindAbsent(id))
}
}
return res, nil
},
location: c.location,
})
}
return res
}
// AllBound returns a task which binds the result key to true without reading its dependencies.
//
// This is intended to be used with conditional tasks to wait for multiple tasks to be completed.
func AllBound(name string, result Key[bool], deps ...ID) Task {
return &task{
name: name,
depends: deps,
provides: []ID{result.ID()},
fn: func(_ context.Context, _ Binder) ([]Binding, error) {
return []Binding{result.Bind(true)}, nil
},
location: getLocation(2),
}
}