-
Notifications
You must be signed in to change notification settings - Fork 5
/
task.go
252 lines (197 loc) · 5.73 KB
/
task.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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
package runtaskbackground
import (
"context"
"fmt"
"sync"
"time"
"github.com/ethpandaops/assertoor/pkg/coordinator/types"
"github.com/sirupsen/logrus"
)
var (
TaskName = "run_task_background"
TaskDescriptor = &types.TaskDescriptor{
Name: TaskName,
Description: "Runs foreground and background task with configurable dependencies.",
Config: DefaultConfig(),
NewTask: NewTask,
}
)
type Task struct {
ctx *types.TaskContext
options *types.TaskOptions
config Config
logger logrus.FieldLogger
foregroundTask types.Task
backgroundTask types.Task
resultChanMtx sync.Mutex
resultChan chan taskResult
foregroundChan chan bool
}
type taskResult struct {
result types.TaskResult
err error
}
func NewTask(ctx *types.TaskContext, options *types.TaskOptions) (types.Task, error) {
return &Task{
ctx: ctx,
options: options,
logger: ctx.Logger.GetLogger(),
}, nil
}
func (t *Task) Name() string {
return TaskName
}
func (t *Task) Description() string {
return TaskDescriptor.Description
}
func (t *Task) Title() string {
return t.ctx.Vars.ResolvePlaceholders(t.options.Title)
}
func (t *Task) Config() interface{} {
return t.config
}
func (t *Task) Logger() logrus.FieldLogger {
return t.logger
}
func (t *Task) Timeout() time.Duration {
return t.options.Timeout.Duration
}
func (t *Task) LoadConfig() error {
config := DefaultConfig()
// parse static config
if t.options.Config != nil {
if err := t.options.Config.Unmarshal(&config); err != nil {
return fmt.Errorf("error parsing task config for %v: %w", TaskName, err)
}
}
// load dynamic vars
err := t.ctx.Vars.ConsumeVars(&config, t.options.ConfigVars)
if err != nil {
return err
}
// validate config
if err2 := config.Validate(); err2 != nil {
return err2
}
// init background task
if config.BackgroundTask != nil {
bgTaskOpts, err2 := t.ctx.Scheduler.ParseTaskOptions(config.BackgroundTask)
if err2 != nil {
return fmt.Errorf("failed parsing background task config: %w", err2)
}
t.backgroundTask, err = t.ctx.NewTask(bgTaskOpts, t.ctx.Vars.NewScope())
if err != nil {
return fmt.Errorf("failed initializing background task: %w", err)
}
}
// init foreground task
fgTaskOpts, err := t.ctx.Scheduler.ParseTaskOptions(config.ForegroundTask)
if err != nil {
return fmt.Errorf("failed parsing foreground task config: %w", err)
}
taskVars := t.ctx.Vars
if config.NewVariableScope {
taskVars = taskVars.NewScope()
}
t.foregroundTask, err = t.ctx.NewTask(fgTaskOpts, taskVars)
if err != nil {
return fmt.Errorf("failed initializing foreground task: %w", err)
}
t.config = config
return nil
}
func (t *Task) Execute(ctx context.Context) error {
t.resultChan = make(chan taskResult, 10)
t.foregroundChan = make(chan bool)
childCtx, cancel := context.WithCancel(ctx)
if t.backgroundTask != nil {
go t.execBackgroundTask(childCtx)
}
go t.execForegroundTask(childCtx)
result := <-t.resultChan
t.ctx.SetResult(result.result)
t.resultChanMtx.Lock()
t.resultChan = nil
t.resultChanMtx.Unlock()
cancel()
<-t.foregroundChan
return result.err
}
func (t *Task) completeWithResult(result types.TaskResult, err error) {
t.resultChanMtx.Lock()
defer t.resultChanMtx.Unlock()
if t.resultChan == nil {
return
}
t.resultChan <- taskResult{
result: result,
err: err,
}
}
func (t *Task) execBackgroundTask(ctx context.Context) {
err := t.ctx.Scheduler.ExecuteTask(ctx, t.backgroundTask, nil)
if ctx.Err() != nil {
return
}
taskResult := t.ctx.Scheduler.GetTaskStatus(t.backgroundTask)
//nolint:goconst // ignore
taskStatus := "success"
if taskResult.Result == types.TaskResultFailure {
taskStatus = "failure"
}
t.logger.Infof("background task completed. status: %v, err: %v", taskStatus, err)
switch t.config.OnBackgroundComplete {
case "fail":
t.completeWithResult(types.TaskResultFailure, fmt.Errorf("background task completed unexpectedly"))
case "success", "succeed":
t.completeWithResult(types.TaskResultSuccess, nil)
case "failOrIgnore":
if taskResult.Result == types.TaskResultFailure {
t.completeWithResult(types.TaskResultFailure, fmt.Errorf("background task completed with failure"))
}
}
}
func (t *Task) execForegroundTask(ctx context.Context) {
defer func() {
close(t.foregroundChan)
}()
err := t.ctx.Scheduler.ExecuteTask(ctx, t.foregroundTask, func(ctx context.Context, cancelFn context.CancelFunc, _ types.Task) {
t.watchTaskResult(ctx, cancelFn)
})
taskResult := t.ctx.Scheduler.GetTaskStatus(t.foregroundTask)
taskStatus := "success"
if taskResult.Result == types.TaskResultFailure {
taskStatus = "failure"
}
t.logger.Infof("foreground task completed. status: %v, err: %v", taskStatus, err)
t.completeWithResult(taskResult.Result, taskResult.Error)
}
func (t *Task) watchTaskResult(ctx context.Context, _ context.CancelFunc) {
currentResult := types.TaskResultNone
for {
updateChan := t.ctx.Scheduler.GetTaskResultUpdateChan(t.foregroundTask, currentResult)
if updateChan != nil {
select {
case <-ctx.Done():
return
case <-updateChan:
}
}
taskStatus := t.ctx.Scheduler.GetTaskStatus(t.foregroundTask)
if taskStatus.Result == currentResult {
continue
}
currentResult = taskStatus.Result
if t.config.ExitOnForegroundSuccess && currentResult == types.TaskResultSuccess {
t.logger.Infof("foreground task succeeded. stopping task with success result")
t.completeWithResult(types.TaskResultSuccess, nil)
return
}
if t.config.ExitOnForegroundFailure && currentResult == types.TaskResultFailure {
t.logger.Infof("foreground task failed. stopping task with failure result")
t.completeWithResult(types.TaskResultFailure, taskStatus.Error)
return
}
t.ctx.SetResult(currentResult)
}
}