-
Notifications
You must be signed in to change notification settings - Fork 41
/
repeat.go
111 lines (91 loc) · 2.89 KB
/
repeat.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
package builtin
import (
"fmt"
"github.com/fission/fission-workflows/pkg/types"
"github.com/fission/fission-workflows/pkg/types/typedvalues"
"github.com/fission/fission-workflows/pkg/types/typedvalues/controlflow"
"github.com/golang/protobuf/proto"
)
const (
Repeat = "repeat"
RepeatInputTimes = "times"
RepeatInputDo = "do"
RepeatInputPrev = "_prev"
)
/*
FunctionRepeat, as the name suggests, repeatedly executes a specific function.
The repeating is based on a static number, and is done sequentially.
The subsequent tasks can access the output of the previous task with `prev`.
**Specification**
**input** | required | types | description
----------------|----------|-------------------|--------------------------------------------------------
times | yes | number | Number of times to repeat the task.
do | yes | task | The task to execute.
Note: the task `do` gets the output of the previous task injected into `prev`.
**output** (*) The output of the last task.
**Example**
```yaml
# ...
RepeatExample:
run: repeat
inputs:
times: 5
do:
run: noop
inputs: { task().prev + 1 }}
# ...
```
A complete example of this function can be found in the [repeatwhale](../examples/whales/repeatwhale.wf.yaml) example.
*/
// TODO minor: chose between unrolled loop and dynamic loop based on number of tasks for performance
type FunctionRepeat struct{}
func (fn *FunctionRepeat) Invoke(spec *types.TaskInvocationSpec) (*typedvalues.TypedValue, error) {
n, ok := spec.Inputs[RepeatInputTimes]
if !ok {
return nil, fmt.Errorf("repeat needs '%s'", RepeatInputTimes)
}
// Wrap condition to a int
times, err := typedvalues.UnwrapInt64(n)
if err != nil {
return nil, err
}
// Wrap do task
// TODO does a workflow also work?
doVal, ok := spec.Inputs[RepeatInputDo]
if !ok {
return nil, fmt.Errorf("repeat needs '%s'", RepeatInputDo)
}
doTask, err := controlflow.UnwrapTask(doVal)
if err != nil {
return nil, err
}
doTask.Requires = map[string]*types.TaskDependencyParameters{}
if times > 0 {
// TODO add context
return typedvalues.MustWrap(&types.WorkflowSpec{
OutputTask: taskID(times - 1),
Tasks: createRepeatTasks(doTask, times),
}), nil
}
return nil, nil
}
func createRepeatTasks(task *types.TaskSpec, times int64) map[string]*types.TaskSpec {
tasks := map[string]*types.TaskSpec{}
for n := int64(0); n < times; n++ {
id := taskID(n)
do := proto.Clone(task).(*types.TaskSpec)
if n > 0 {
prevTask := taskID(n - 1)
do.Require(prevTask)
// TODO move prev to a reserved namespace, to avoid conflicts
prev := typedvalues.MustWrap(fmt.Sprintf("{output('%s')}", prevTask))
prev.SetMetadata(typedvalues.MetadataPriority, "100")
do.Input(RepeatInputPrev, prev)
}
tasks[id] = do
}
return tasks
}
func taskID(n int64) string {
return fmt.Sprintf("do_%d", n)
}