Skip to content

Commit 5405a8a

Browse files
committed
added setting to retry failed task in run_task_options task
1 parent 83b48f4 commit 5405a8a

File tree

3 files changed

+54
-29
lines changed

3 files changed

+54
-29
lines changed

pkg/coordinator/tasks/run_task_options/README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,12 @@ The `run_task_options` task is designed to execute a single task with configurab
2020
- **`ignoreFailure`**:\
2121
When `true`, any failure result from the child task is ignored, and the `run_task_options` task will return a success result instead. This is useful for cases where the child task's failure is an acceptable outcome.
2222

23+
- **`retryOnFailure`**:\
24+
If set to `true`, the task will retry the execution of the child task if it fails, up to the maximum number of retries specified by `maxRetryCount`.
25+
26+
- **`maxRetryCount`**:\
27+
The maximum number of times the child task will be retried if it fails and `retryOnFailure` is true. A value of 0 means no retries.
28+
2329
- **`newVariableScope`**:\
2430
Determines whether to create a new variable scope for the child task. If `false`, the current scope is passed through, allowing the child task to share the same variable context as the `run_task_options` task.
2531

@@ -35,5 +41,7 @@ Default settings for the `run_task_options` task:
3541
invertResult: false
3642
expectFailure: false
3743
ignoreFailure: false
44+
retryOnFailure: false
45+
maxRetryCount: 0
3846
newVariableScope: false
3947
```

pkg/coordinator/tasks/run_task_options/config.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ type Config struct {
1212
InvertResult bool `yaml:"invertResult" json:"invertResult"`
1313
ExpectFailure bool `yaml:"expectFailure" json:"expectFailure"`
1414
IgnoreFailure bool `yaml:"ignoreFailure" json:"ignoreFailure"`
15+
RetryOnFailure bool `yaml:"retryOnFailure" json:"retryOnFailure"`
16+
MaxRetryCount uint `yaml:"maxRetryCount" json:"maxRetryCount"`
1517
NewVariableScope bool `yaml:"newVariableScope" json:"newVariableScope"`
1618
}
1719

pkg/coordinator/tasks/run_task_options/task.go

Lines changed: 44 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -80,45 +80,60 @@ func (t *Task) LoadConfig() error {
8080
return err2
8181
}
8282

83-
// init child task
84-
taskOpts, err := t.ctx.Scheduler.ParseTaskOptions(config.Task)
85-
if err != nil {
86-
return fmt.Errorf("failed parsing child task config: %w", err)
87-
}
88-
89-
taskVars := t.ctx.Vars
90-
if config.NewVariableScope {
91-
taskVars = taskVars.NewScope()
92-
}
93-
94-
t.task, err = t.ctx.NewTask(taskOpts, taskVars)
95-
if err != nil {
96-
return fmt.Errorf("failed initializing child task: %w", err)
97-
}
98-
9983
t.config = config
10084

10185
return nil
10286
}
10387

10488
func (t *Task) Execute(ctx context.Context) error {
105-
err := t.ctx.Scheduler.ExecuteTask(ctx, t.task, func(ctx context.Context, cancelFn context.CancelFunc, _ types.Task) {
106-
t.watchTaskResult(ctx, cancelFn)
107-
})
108-
109-
switch {
110-
case t.config.ExpectFailure:
111-
if err == nil {
112-
return fmt.Errorf("child task succeeded, but should have failed")
113-
}
114-
case t.config.IgnoreFailure:
89+
retryCount := uint(0)
90+
91+
for {
92+
// init child task
93+
taskOpts, err := t.ctx.Scheduler.ParseTaskOptions(t.config.Task)
11594
if err != nil {
116-
t.logger.Warnf("child task failed: %w", err)
95+
return fmt.Errorf("failed parsing child task config: %w", err)
96+
}
97+
98+
taskVars := t.ctx.Vars
99+
if t.config.NewVariableScope {
100+
taskVars = taskVars.NewScope()
117101
}
118-
default:
102+
103+
t.task, err = t.ctx.NewTask(taskOpts, taskVars)
119104
if err != nil {
120-
return fmt.Errorf("child task failed: %w", err)
105+
return fmt.Errorf("failed initializing child task: %w", err)
106+
}
107+
108+
// execute task
109+
err = t.ctx.Scheduler.ExecuteTask(ctx, t.task, func(ctx context.Context, cancelFn context.CancelFunc, _ types.Task) {
110+
t.watchTaskResult(ctx, cancelFn)
111+
})
112+
113+
switch {
114+
case t.config.RetryOnFailure && retryCount < t.config.MaxRetryCount:
115+
if err != nil {
116+
retryCount++
117+
118+
t.logger.Warnf("child task failed: %w (retrying)", err)
119+
120+
continue
121+
}
122+
case t.config.ExpectFailure:
123+
if err == nil {
124+
return fmt.Errorf("child task succeeded, but should have failed")
125+
}
126+
case t.config.IgnoreFailure:
127+
if err != nil {
128+
t.logger.Warnf("child task failed: %w", err)
129+
}
130+
default:
131+
if err != nil {
132+
return fmt.Errorf("child task failed: %w", err)
133+
}
121134
}
135+
136+
break
122137
}
123138

124139
return nil

0 commit comments

Comments
 (0)