This commit is contained in:
白茶清欢 2023-08-24 11:19:48 +08:00
parent dcd2281f33
commit 87f8651bed
2 changed files with 10 additions and 5 deletions

View File

@ -19,6 +19,10 @@ type Config struct {
ForbiddenCallback bool ForbiddenCallback bool
// Param 任务执行参数 // Param 任务执行参数
Param map[string]interface{} Param map[string]interface{}
// TaskName 任务名称
TaskName string
// Async 是否异步
Async bool
} }
// Result 执行结果 // Result 执行结果

11
task.go
View File

@ -109,7 +109,6 @@ func (d *dispatch) Run(ctx context.Context, cfg *Config) *Result {
TaskConfig: cfg, TaskConfig: cfg,
Data: nil, Data: nil,
Err: nil, Err: nil,
Async: cfg.Async,
} }
defer func() { defer func() {
result.FinishTime = time.Now().UnixNano() result.FinishTime = time.Now().UnixNano()
@ -120,8 +119,6 @@ func (d *dispatch) Run(ctx context.Context, cfg *Config) *Result {
result.Err = fmt.Errorf("%v 任务未注册", cfg.TaskName) result.Err = fmt.Errorf("%v 任务未注册", cfg.TaskName)
} }
d.lock.RUnlock() d.lock.RUnlock()
result.TaskRunID = taskInstance.GetRunID()
result.TaskDescription = taskInstance.Description()
if cfg.Async { if cfg.Async {
// 异步运行 // 异步运行
go func() { go func() {
@ -133,11 +130,15 @@ func (d *dispatch) Run(ctx context.Context, cfg *Config) *Result {
result.Err = fmt.Errorf("出现其他场景Panic : %v", e) result.Err = fmt.Errorf("出现其他场景Panic : %v", e)
} }
} }
result.Data, result.Err = taskInstance.Execute(ctx, cfg) result = taskInstance.Execute(ctx, cfg)
result.TaskRunID = taskInstance.GetRunID()
result.TaskDescription = taskInstance.Description()
_ = taskInstance.Callback(result) _ = taskInstance.Callback(result)
}() }()
} else { } else {
result.Data, result.Err = taskInstance.Execute(ctx, cfg) result = taskInstance.Execute(ctx, cfg)
result.TaskRunID = taskInstance.GetRunID()
result.TaskDescription = taskInstance.Description()
_ = taskInstance.Callback(result) _ = taskInstance.Callback(result)
} }
return result return result