update task

This commit is contained in:
白茶清欢 2024-10-08 11:24:23 +08:00
parent 4bf786336e
commit 55be6d989e
5 changed files with 23 additions and 21 deletions

View File

@ -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
}

11
core.go
View File

@ -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())

View File

@ -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
}

View File

@ -18,7 +18,7 @@ type Config struct {
// ForbiddenCallback 禁用执行结果回调
ForbiddenCallback bool
// Param 任务执行参数
Param map[string]interface{}
Param map[string]any
// TaskFlag 任务标识
TaskFlag string
// TaskName 任务名称
@ -39,6 +39,6 @@ type Result struct {
TaskRunID string // 任务运行ID
TaskDescription string // 任务描述
TaskConfig *Config // 任务配置
Data map[string]interface{} // 任务结果数据
Data map[string]any // 任务结果数据
Err error // 异常信息, err == nil , 代表执行成功
}

11
task.go
View File

@ -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)
}