第一版时间轮任务调度实现 #1

Merged
zhangdeman merged 15 commits from feature/timewheel into master 2023-09-04 20:13:17 +08:00
2 changed files with 149 additions and 0 deletions
Showing only changes of commit fe15933479 - Show all commits

View File

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

145
task.go Normal file
View File

@ -0,0 +1,145 @@
// Package task ...
//
// Description : task ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 2022-06-23 15:13
package task
import (
"context"
"fmt"
"runtime"
"sync"
"time"
)
var (
// Dispatch 调度实例
Dispatch *dispatch
)
func init() {
Dispatch = &dispatch{
lock: &sync.RWMutex{},
taskTable: make(map[string]ITask),
}
}
// dispatch 任务调度
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 15:14 2022/6/23
type dispatch struct {
// 锁
lock *sync.RWMutex
// 任务表
taskTable map[string]ITask
}
// Register 注册任务
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 15:16 2022/6/23
func (d *dispatch) Register(taskInstanceList ...ITask) error {
d.lock.Lock()
defer d.lock.Unlock()
for _, taskInstance := range taskInstanceList {
if nil == taskInstance {
continue
}
if _, exist := d.taskTable[taskInstance.Name()]; exist {
return fmt.Errorf("%s 任务重复注册! ", taskInstance.Name())
}
d.taskTable[taskInstance.Name()] = taskInstance
}
return nil
}
// Remove 移除任务
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 15:16 2022/6/23
func (d *dispatch) Remove(taskNameList ...string) error {
d.lock.Lock()
defer d.lock.Unlock()
for _, taskName := range taskNameList {
delete(d.taskTable, taskName)
}
return nil
}
// Run 执行任务
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 15:18 2022/6/23
func (d *dispatch) Run(ctx context.Context, cfg *Config) *Result {
var (
taskInstance ITask
exist bool
cancelFunc context.CancelFunc
)
if nil == ctx {
if cfg.Timeout > 0 {
ctx, cancelFunc = context.WithCancel(context.Background())
} else {
ctx = context.Background()
}
} else {
ctx, cancelFunc = context.WithCancel(ctx)
}
if nil != cancelFunc {
// 带了超时时间
cancelFunc()
}
result := &Result{
StartTime: time.Now().UnixNano(),
FinishTime: 0,
Used: 0,
TaskRunID: "",
TaskDescription: "",
TaskConfig: cfg,
Data: nil,
Err: nil,
}
defer func() {
result.FinishTime = time.Now().UnixNano()
result.Used = result.FinishTime - result.StartTime
}()
d.lock.RLock()
if taskInstance, exist = d.taskTable[cfg.TaskName]; !exist {
result.Err = fmt.Errorf("%v 任务未注册", cfg.TaskName)
}
d.lock.RUnlock()
if cfg.Async {
// 异步运行
go func() {
if e := recover(); nil != e {
switch e.(type) {
case runtime.Error: // 运行时错误
result.Err = fmt.Errorf("出现运行时Panic : %v", e)
default: // 非运行时错误
result.Err = fmt.Errorf("出现其他场景Panic : %v", e)
}
}
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)
}
return result
}