Skip to content

Commit a034b03

Browse files
authored
fix: Task framework adds timeout exit parameter (#11206)
1 parent 470f4e4 commit a034b03

File tree

1 file changed

+25
-18
lines changed

1 file changed

+25
-18
lines changed

core/app/task/task.go

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111

1212
"github.com/1Panel-dev/1Panel/core/app/model"
1313
"github.com/1Panel-dev/1Panel/core/app/repo"
14+
"github.com/1Panel-dev/1Panel/core/buserr"
1415
"github.com/1Panel-dev/1Panel/core/constant"
1516
"github.com/1Panel-dev/1Panel/core/global"
1617
"github.com/1Panel-dev/1Panel/core/i18n"
@@ -21,27 +22,29 @@ type ActionFunc func(*Task) error
2122
type RollbackFunc func(*Task)
2223

2324
type Task struct {
24-
Name string
25-
TaskID string
26-
Logger *log.Logger
27-
SubTasks []*SubTask
28-
Rollbacks []RollbackFunc
29-
logFile *os.File
30-
taskRepo repo.ITaskRepo
31-
Task *model.Task
32-
ParentID string
25+
Name string
26+
TaskID string
27+
Logger *log.Logger
28+
SubTasks []*SubTask
29+
Rollbacks []RollbackFunc
30+
logFile *os.File
31+
taskRepo repo.ITaskRepo
32+
Task *model.Task
33+
ParentID string
34+
CancleWhenTimeout bool
3335
}
3436

3537
type SubTask struct {
36-
RootTask *Task
37-
Name string
38-
StepAlias string
39-
Retry int
40-
Timeout time.Duration
41-
Action ActionFunc
42-
Rollback RollbackFunc
43-
Error error
44-
IgnoreErr bool
38+
RootTask *Task
39+
Name string
40+
StepAlias string
41+
Retry int
42+
Timeout time.Duration
43+
Action ActionFunc
44+
Rollback RollbackFunc
45+
Error error
46+
IgnoreErr bool
47+
CancleWhenTimeout bool
4548
}
4649

4750
const (
@@ -143,6 +146,9 @@ func (s *SubTask) Execute() error {
143146
select {
144147
case <-ctx.Done():
145148
s.RootTask.Log(i18n.GetWithName("TaskTimeout", subTaskName))
149+
if s.CancleWhenTimeout {
150+
return buserr.New(i18n.GetWithName("TaskTimeout", subTaskName))
151+
}
146152
case err = <-done:
147153
if err != nil {
148154
s.RootTask.Log(i18n.GetWithNameAndErr("SubTaskFailed", subTaskName, err))
@@ -173,6 +179,7 @@ func (t *Task) Execute() error {
173179
var err error
174180
t.Log(i18n.GetWithName("TaskStart", t.Name))
175181
for _, subTask := range t.SubTasks {
182+
subTask.CancleWhenTimeout = t.CancleWhenTimeout
176183
t.Task.CurrentStep = subTask.StepAlias
177184
t.updateTask(t.Task)
178185
if err = subTask.Execute(); err == nil {

0 commit comments

Comments
 (0)