2023-08-15 15:58:07 +08:00
|
|
|
|
package task
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
2023-09-13 10:43:12 +08:00
|
|
|
|
"fmt"
|
|
|
|
|
|
"reflect"
|
|
|
|
|
|
|
2023-08-15 15:58:07 +08:00
|
|
|
|
"gitlink.org.cn/cloudream/common/pkgs/task"
|
2023-09-13 10:43:12 +08:00
|
|
|
|
myreflect "gitlink.org.cn/cloudream/common/utils/reflect"
|
|
|
|
|
|
exectsk "gitlink.org.cn/cloudream/scheduler/common/pkgs/mq/executor/task"
|
2023-09-04 09:51:20 +08:00
|
|
|
|
reporter "gitlink.org.cn/cloudream/scheduler/executor/internal/reporter"
|
2023-08-15 15:58:07 +08:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
type TaskContext struct {
|
2023-09-04 09:51:20 +08:00
|
|
|
|
reporter *reporter.Reporter
|
2023-08-15 15:58:07 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 需要在Task结束后主动调用,completing函数将在Manager加锁期间被调用,
|
|
|
|
|
|
// 因此适合进行执行结果的设置
|
|
|
|
|
|
type CompleteFn = task.CompleteFn
|
|
|
|
|
|
|
2023-09-13 10:43:12 +08:00
|
|
|
|
type Manager struct {
|
|
|
|
|
|
task.Manager[TaskContext]
|
|
|
|
|
|
}
|
2023-08-15 15:58:07 +08:00
|
|
|
|
|
|
|
|
|
|
type TaskBody = task.TaskBody[TaskContext]
|
|
|
|
|
|
|
|
|
|
|
|
type Task = task.Task[TaskContext]
|
|
|
|
|
|
|
|
|
|
|
|
type CompleteOption = task.CompleteOption
|
|
|
|
|
|
|
2023-09-04 09:51:20 +08:00
|
|
|
|
func NewManager(reporter *reporter.Reporter) Manager {
|
2023-09-13 10:43:12 +08:00
|
|
|
|
return Manager{
|
|
|
|
|
|
Manager: task.NewManager(TaskContext{
|
|
|
|
|
|
reporter: reporter,
|
|
|
|
|
|
}),
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-11-09 17:22:10 +08:00
|
|
|
|
func (m *Manager) StartByInfo(info exectsk.TaskInfo) (*Task, error) {
|
2023-09-13 10:43:12 +08:00
|
|
|
|
infoType := myreflect.TypeOfValue(info)
|
|
|
|
|
|
|
|
|
|
|
|
ctor, ok := taskFromInfoCtors[infoType]
|
|
|
|
|
|
if !ok {
|
|
|
|
|
|
return nil, fmt.Errorf("unknow info type")
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return m.StartNew(ctor(info)), nil
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-11-09 17:22:10 +08:00
|
|
|
|
var taskFromInfoCtors map[reflect.Type]func(exectsk.TaskInfo) TaskBody = make(map[reflect.Type]func(exectsk.TaskInfo) task.TaskBody[TaskContext])
|
2023-09-13 10:43:12 +08:00
|
|
|
|
|
2023-11-09 17:22:10 +08:00
|
|
|
|
func Register[TInfo exectsk.TaskInfo, TTaskBody TaskBody](ctor func(info TInfo) TTaskBody) {
|
|
|
|
|
|
taskFromInfoCtors[myreflect.TypeOf[TInfo]()] = func(info exectsk.TaskInfo) TaskBody {
|
2023-09-13 10:43:12 +08:00
|
|
|
|
return ctor(info.(TInfo))
|
|
|
|
|
|
}
|
2023-08-15 15:58:07 +08:00
|
|
|
|
}
|