2022-06-23 15:33:29 +08:00
|
|
|
// Package task ...
|
|
|
|
//
|
|
|
|
// Description : task ...
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 2022-06-23 15:13
|
|
|
|
package task
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-06-23 15:36:55 +08:00
|
|
|
"fmt"
|
2024-10-08 11:24:23 +08:00
|
|
|
"git.zhangdeman.cn/zhangdeman/wrapper"
|
2022-06-23 16:46:22 +08:00
|
|
|
"runtime"
|
2022-06-23 15:33:29 +08:00
|
|
|
"sync"
|
2022-06-23 16:46:22 +08:00
|
|
|
"time"
|
2022-06-23 15:33:29 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
2024-10-08 11:24:23 +08:00
|
|
|
if _, exist := d.taskTable[taskInstance.GetFlag()]; exist {
|
|
|
|
return fmt.Errorf("%s 任务重复注册! ", taskInstance.GetFlag())
|
2022-06-23 15:36:55 +08:00
|
|
|
}
|
2024-10-08 11:24:23 +08:00
|
|
|
d.taskTable[taskInstance.GetFlag()] = taskInstance
|
2022-06-23 15:33:29 +08:00
|
|
|
}
|
|
|
|
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
|
2022-06-23 16:46:22 +08:00
|
|
|
func (d *dispatch) Run(ctx context.Context, cfg *Config) *Result {
|
|
|
|
var (
|
|
|
|
taskInstance ITask
|
|
|
|
exist bool
|
2022-06-27 11:54:25 +08:00
|
|
|
cancelFunc context.CancelFunc
|
2022-06-23 16:46:22 +08:00
|
|
|
)
|
|
|
|
|
2022-06-27 11:54:25 +08:00
|
|
|
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()
|
|
|
|
}
|
|
|
|
|
2022-06-23 16:46:22 +08:00
|
|
|
result := &Result{
|
|
|
|
StartTime: time.Now().UnixNano(),
|
|
|
|
FinishTime: 0,
|
|
|
|
Used: 0,
|
2024-10-08 11:24:23 +08:00
|
|
|
TaskRunID: wrapper.StringFromRandom(64, "").Md5().Value,
|
2022-06-23 16:46:22 +08:00
|
|
|
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)
|
|
|
|
}
|
2022-06-25 19:42:25 +08:00
|
|
|
d.lock.RUnlock()
|
2022-06-23 16:46:22 +08:00
|
|
|
if cfg.Async {
|
|
|
|
// 异步运行
|
|
|
|
go func() {
|
2022-06-25 19:42:25 +08:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
2023-08-24 11:19:48 +08:00
|
|
|
result = taskInstance.Execute(ctx, cfg)
|
|
|
|
result.TaskDescription = taskInstance.Description()
|
2022-06-25 19:44:05 +08:00
|
|
|
_ = taskInstance.Callback(result)
|
2022-06-23 16:46:22 +08:00
|
|
|
}()
|
|
|
|
} else {
|
2023-08-24 11:19:48 +08:00
|
|
|
result = taskInstance.Execute(ctx, cfg)
|
|
|
|
result.TaskDescription = taskInstance.Description()
|
2022-06-25 19:44:05 +08:00
|
|
|
_ = taskInstance.Callback(result)
|
2022-06-23 16:46:22 +08:00
|
|
|
}
|
|
|
|
return result
|
2022-06-23 15:33:29 +08:00
|
|
|
}
|