-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathworker.go
172 lines (133 loc) · 4.21 KB
/
worker.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
package choreograph
import (
"context"
"reflect"
"sync"
"github.com/pkg/errors"
)
// ErrUnexpectedType indicates receiving unexpected type when casting.
var ErrUnexpectedType = errors.New("received unexpected type")
// Result is an output for a single piece of work.
type Result struct {
// Input contains what was provided as an input parameter.
Input interface{}
// RuntimeError contains an error that could stop processing the data.
RuntimeError error
// ExecutionErrors contains any information/error which occurred during processing the data.
ExecutionErrors []error
}
type worker struct {
steps Steps
err []error
}
// StartWorker starts listening process to handle new inputs.
func (w *worker) StartWorker(ctx context.Context, inputs <-chan interface{}, results chan<- Result, group *sync.WaitGroup) {
workerCtx := context.WithValue(ctx, DataBagContextKey, new(DataBag))
for i := range inputs {
results <- Result{
Input: i,
RuntimeError: w.run(workerCtx, i),
ExecutionErrors: w.err,
}
group.Done()
}
}
func (w *worker) run(ctx context.Context, input interface{}) error {
w.err = []error{}
if db, ok := ctx.Value(DataBagContextKey).(*DataBag); ok {
db.clear()
}
for idx := range w.steps {
select {
case <-ctx.Done():
return errors.Wrapf(ctx.Err(), "execution stopped before step '%s'", w.steps.StepName(idx))
default:
err := w.executeStep(ctx, idx, input)
if err != nil {
return errors.Wrapf(err, "step '%s' execution failed", w.steps.StepName(idx))
}
}
}
return nil
}
func (w *worker) executeStep(ctx context.Context, stepIdx int, input interface{}) error {
preCheckValue := reflect.ValueOf(w.steps[stepIdx].PreCheck)
preCheckType := reflect.TypeOf(w.steps[stepIdx].PreCheck)
preCheckParams, err := w.getCallParams(ctx, preCheckType, input)
if err != nil {
return errors.Wrap(err, "preparing preCheck parameters")
}
preCheckResp := preCheckValue.Call(preCheckParams)
if output, ok := isReturnOutput(preCheckResp); ok {
if db, ok := ctx.Value(DataBagContextKey).(*DataBag); ok {
db.SetPreCheckData(w.steps.StepName(stepIdx), output)
}
}
err = isReturnError(preCheckResp)
if err != nil {
w.err = append(w.err, errors.Wrapf(err, "preCheck '%s'", w.steps.StepName(stepIdx)))
if errors.Is(err, ErrExecutionCanceled) || errors.Is(err, ErrUnexpectedType) {
return errors.Wrap(err, "preCheck cancelled")
}
return nil
}
jobValue := reflect.ValueOf(w.steps[stepIdx].Job)
jobType := reflect.TypeOf(w.steps[stepIdx].Job)
jobParams, err := w.getCallParams(ctx, jobType, input)
if err != nil {
return errors.Wrap(err, "preparing job parameters")
}
jobResp := jobValue.Call(jobParams)
err = isReturnError(jobResp)
if err != nil {
w.err = append(w.err, errors.Wrapf(err, "job '%s'", w.steps.StepName(stepIdx)))
return ErrJobFailed
}
if output, ok := isReturnOutput(jobResp); ok {
if db, ok := ctx.Value(DataBagContextKey).(*DataBag); ok {
db.SetJobData(w.steps.StepName(stepIdx), output)
}
}
return nil
}
func (w *worker) getCallParams(ctx context.Context, callback reflect.Type, input interface{}) ([]reflect.Value, error) {
inputType := reflect.TypeOf(input)
inputValue := reflect.ValueOf(input)
params := make([]reflect.Value, 0)
params = append(params, reflect.ValueOf(ctx))
if callback.NumIn() > 1 {
var param reflect.Value
switch {
case !inputValue.IsValid():
param = reflect.New(callback.In(1)).Elem()
case !inputValue.IsZero():
if !inputType.AssignableTo(callback.In(1)) {
return nil, errors.Wrapf(
ErrUnassignableParameter,
"cannot assign '%s' to '%s' in preCheck",
inputType.String(),
callback.In(1).String(),
)
}
param = inputValue
}
params = append(params, param)
}
return params, nil
}
func isReturnOutput(result []reflect.Value) (interface{}, bool) {
if len(result) > 1 && result[0].CanInterface() {
return result[0].Interface(), true
}
return nil, false
}
func isReturnError(result []reflect.Value) error {
if len(result) > 0 && !result[len(result)-1].IsNil() {
err, ok := result[len(result)-1].Interface().(error)
if !ok {
return errors.Wrapf(ErrUnexpectedType, "got '%s' and expected 'error'", result[len(result)-1].Type())
}
return err
}
return nil
}