From 55be6d989ec4ee9b5e150ec8715940a676268b8a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Tue, 8 Oct 2024 11:24:23 +0800 Subject: [PATCH] update task --- abstract.go | 2 +- core.go | 11 +++++++---- core_test.go | 2 +- define.go | 18 +++++++++--------- task.go | 11 +++++------ 5 files changed, 23 insertions(+), 21 deletions(-) diff --git a/abstract.go b/abstract.go index 3772291..5c3f7d3 100644 --- a/abstract.go +++ b/abstract.go @@ -22,5 +22,5 @@ type ITask interface { // Callback 任务执行成功的回调 Callback(result *Result) error // Execute 执行任务 - Execute(ctx context.Context, cfg *Config) (map[string]interface{}, error) + Execute(ctx context.Context, cfg *Config) *Result } diff --git a/core.go b/core.go index 3e3c316..526b21b 100644 --- a/core.go +++ b/core.go @@ -133,7 +133,7 @@ func (tw *TimeWheel) checkAndRunTask() { } }() - _, _ = task.Job.Execute(nil, nil) + _ = task.Job.Execute(nil, nil) }() } else if tw.job != nil { go func() { @@ -142,7 +142,7 @@ func (tw *TimeWheel) checkAndRunTask() { } }() - _, _ = tw.job.Execute(nil, nil) + _ = tw.job.Execute(nil, nil) }() } else { fmt.Println(fmt.Sprintf("The task %v don't have job to run", task.Key)) @@ -182,9 +182,12 @@ func (tw *TimeWheel) checkAndRunTask() { // // Date : 13:43 2023/8/4 func (tw *TimeWheel) AddTask(task *Task, byInterval bool) { + if nil == task { + return + } // 生成 run_id task.RunID = wrapper.StringFromRandom(128, "").Md5().Value - if nil != task && nil != task.Job { + if nil != task.Job { task.Key = task.Job.GetFlag() + "_" + task.RunID } var pos, circle int @@ -239,7 +242,7 @@ func (tw *TimeWheel) getPosAndCircleByInterval(d time.Duration) (int, int) { // Author : go_developer@163.com<白茶清欢> // // Date : 13:47 2023/8/4 -func (tw *TimeWheel) getPosAndCircleByCreatedTime(createdTime time.Time, d time.Duration, key interface{}) (int, int) { +func (tw *TimeWheel) getPosAndCircleByCreatedTime(createdTime time.Time, d time.Duration, key any) (int, int) { passedTime := time.Since(createdTime) passedSeconds := int(passedTime.Seconds()) diff --git a/core_test.go b/core_test.go index 9dbe5f0..3075317 100644 --- a/core_test.go +++ b/core_test.go @@ -61,7 +61,7 @@ func (t testTask) Callback(result *Result) error { return nil } -func (t testTask) Execute(ctx context.Context, cfg *Config) (map[string]interface{}, error) { +func (t testTask) Execute(ctx context.Context, cfg *Config) (map[string]any, error) { fmt.Println(wrapper.OwnTimeFromNow().ToString()) return nil, nil } diff --git a/define.go b/define.go index 8130850..a520f09 100644 --- a/define.go +++ b/define.go @@ -18,7 +18,7 @@ type Config struct { // ForbiddenCallback 禁用执行结果回调 ForbiddenCallback bool // Param 任务执行参数 - Param map[string]interface{} + Param map[string]any // TaskFlag 任务标识 TaskFlag string // TaskName 任务名称 @@ -33,12 +33,12 @@ type Config struct { // // Date : 14:43 2022/6/23 type Result struct { - StartTime int64 // 开始时间, 纳秒 - FinishTime int64 // 结束时间, 纳秒 - Used int64 // 耗时, 纳秒 - TaskRunID string // 任务运行ID - TaskDescription string // 任务描述 - TaskConfig *Config // 任务配置 - Data map[string]interface{} // 任务结果数据 - Err error // 异常信息, err == nil , 代表执行成功 + StartTime int64 // 开始时间, 纳秒 + FinishTime int64 // 结束时间, 纳秒 + Used int64 // 耗时, 纳秒 + TaskRunID string // 任务运行ID + TaskDescription string // 任务描述 + TaskConfig *Config // 任务配置 + Data map[string]any // 任务结果数据 + Err error // 异常信息, err == nil , 代表执行成功 } diff --git a/task.go b/task.go index c9d5ddc..249e932 100644 --- a/task.go +++ b/task.go @@ -10,6 +10,7 @@ package task import ( "context" "fmt" + "git.zhangdeman.cn/zhangdeman/wrapper" "runtime" "sync" "time" @@ -51,10 +52,10 @@ func (d *dispatch) Register(taskInstanceList ...ITask) error { if nil == taskInstance { continue } - if _, exist := d.taskTable[taskInstance.Name()]; exist { - return fmt.Errorf("%s 任务重复注册! ", taskInstance.Name()) + if _, exist := d.taskTable[taskInstance.GetFlag()]; exist { + return fmt.Errorf("%s 任务重复注册! ", taskInstance.GetFlag()) } - d.taskTable[taskInstance.Name()] = taskInstance + d.taskTable[taskInstance.GetFlag()] = taskInstance } return nil } @@ -104,7 +105,7 @@ func (d *dispatch) Run(ctx context.Context, cfg *Config) *Result { StartTime: time.Now().UnixNano(), FinishTime: 0, Used: 0, - TaskRunID: "", + TaskRunID: wrapper.StringFromRandom(64, "").Md5().Value, TaskDescription: "", TaskConfig: cfg, Data: nil, @@ -131,13 +132,11 @@ func (d *dispatch) Run(ctx context.Context, cfg *Config) *Result { } } result = taskInstance.Execute(ctx, cfg) - result.TaskRunID = taskInstance.GetRunID() result.TaskDescription = taskInstance.Description() _ = taskInstance.Callback(result) }() } else { result = taskInstance.Execute(ctx, cfg) - result.TaskRunID = taskInstance.GetRunID() result.TaskDescription = taskInstance.Description() _ = taskInstance.Callback(result) }