Skip to content

Commit

Permalink
feat: add recover for step
Browse files Browse the repository at this point in the history
  • Loading branch information
ifooth committed Sep 4, 2024
1 parent ede9d52 commit 63f2292
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 8 deletions.
6 changes: 3 additions & 3 deletions bcs-common/common/task/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
8 changes: 8 additions & 0 deletions bcs-common/common/task/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"context"
"errors"
"fmt"
"runtime/debug"
"sync"
"time"

Expand Down Expand Up @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion bcs-common/common/task/stores/mysql/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down
4 changes: 2 additions & 2 deletions bcs-common/common/task/types/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion bcs-common/common/task/types/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down
2 changes: 1 addition & 1 deletion bcs-common/common/task/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand Down

0 comments on commit 63f2292

Please sign in to comment.