From bbbba683b68659c2c73dc11e288bd29eaed9d580 Mon Sep 17 00:00:00 2001 From: ifooth Date: Wed, 4 Sep 2024 15:59:10 +0800 Subject: [PATCH] feat: add recover for step --- bcs-common/common/task/builder.go | 6 +++--- bcs-common/common/task/manager.go | 8 ++++++++ bcs-common/common/task/stores/mysql/table.go | 3 ++- bcs-common/common/task/types/task.go | 4 ++-- bcs-common/common/task/types/type.go | 2 +- bcs-common/common/task/utils.go | 2 +- 6 files changed, 17 insertions(+), 8 deletions(-) diff --git a/bcs-common/common/task/builder.go b/bcs-common/common/task/builder.go index 5266514802..5a23c1a1b9 100644 --- a/bcs-common/common/task/builder.go +++ b/bcs-common/common/task/builder.go @@ -34,11 +34,11 @@ func NewByTaskBuilder(builder types.TaskBuilder, opts ...types.TaskOption) (*typ task.Steps = steps task.CurrentStep = steps[0].GetName() - // 自定义extraJson等 - newTask, err := builder.BuildTask(*task) + // 自定义任务超时, commonParams, commonPayload等 + err = builder.FinalizeTask(task) if err != nil { return nil, err } - return &newTask, nil + return task, nil } diff --git a/bcs-common/common/task/manager.go b/bcs-common/common/task/manager.go index 719f26bcab..c15595e62e 100644 --- a/bcs-common/common/task/manager.go +++ b/bcs-common/common/task/manager.go @@ -17,6 +17,7 @@ import ( "context" "errors" "fmt" + "runtime/debug" "sync" "time" @@ -359,6 +360,13 @@ func (m *TaskManager) doWork(taskID string, stepName string) error { // nolint tmpCh := make(chan error, 1) go func() { + defer func() { + if r := recover(); r != nil { + log.ERROR.Printf("[%s-%s][recover] panic: %v, stack %s", taskID, stepName, r, debug.Stack()) + tmpCh <- fmt.Errorf("%w by a panic: %v", istep.ErrRevoked, r) + } + }() + // call step worker execCtx := istep.NewContext(stepCtx, GetGlobalStorage(), state.GetTask(), step) tmpCh <- stepExecutor.Execute(execCtx) diff --git a/bcs-common/common/task/stores/mysql/table.go b/bcs-common/common/task/stores/mysql/table.go index 943b2c2362..1a2162c62b 100644 --- a/bcs-common/common/task/stores/mysql/table.go +++ b/bcs-common/common/task/stores/mysql/table.go @@ -24,7 +24,8 @@ import ( 字段规范: 1. 字段名使用驼峰命名法,表字段使用 _ 分隔 2. bool/int/float/datetime 等类型使用默认字段类型 -3. string 类型必须指定类型和长度,字段是索引的,设置为 varchar(191) +3. string 类型必须指定类型和长度 +4. index 固定varchar(191), (mysql 5.6索引长度限制767byte, utf8mb4下最长191) **/ var ( diff --git a/bcs-common/common/task/types/task.go b/bcs-common/common/task/types/task.go index c0264f7368..442697f1a8 100644 --- a/bcs-common/common/task/types/task.go +++ b/bcs-common/common/task/types/task.go @@ -24,8 +24,8 @@ import ( // TaskBuilder ... type TaskBuilder interface { // nolint TaskInfo() TaskInfo - Steps() ([]*Step, error) // Steps init step and define StepSequence - BuildTask(t Task) (Task, error) + Steps() ([]*Step, error) // Steps init step and define StepSequence + FinalizeTask(t *Task) error // FinalizeTask for custom task } // TaskOptions xxx diff --git a/bcs-common/common/task/types/type.go b/bcs-common/common/task/types/type.go index 5299aef0ff..ce4a09b35e 100644 --- a/bcs-common/common/task/types/type.go +++ b/bcs-common/common/task/types/type.go @@ -56,7 +56,7 @@ var ( type Task struct { TaskIndex string `json:"taskIndex"` TaskIndexType string `json:"taskIndexType"` - TaskID string `json:"taskId"` + TaskID string `json:"taskID"` TaskType string `json:"taskType"` TaskName string `json:"taskName"` CurrentStep string `json:"currentStep"` diff --git a/bcs-common/common/task/utils.go b/bcs-common/common/task/utils.go index d0c86f06c8..c7a6453e10 100644 --- a/bcs-common/common/task/utils.go +++ b/bcs-common/common/task/utils.go @@ -26,7 +26,7 @@ import ( // RecoverPrintStack capture panic and print stack func RecoverPrintStack(proc string) { if r := recover(); r != nil { - log.ERROR.Printf("[%s][recover] panic: %v, stack %v\n", proc, r, string(debug.Stack())) + log.ERROR.Printf("[%s][recover] panic: %v, stack %s", proc, r, debug.Stack()) return } }