81 lines
1.9 KiB
Go
81 lines
1.9 KiB
Go
package task
|
|
|
|
import (
|
|
"gitlink.org.cn/cloudream/common/pkgs/logger"
|
|
"gitlink.org.cn/cloudream/common/utils/reflect2"
|
|
"gitlink.org.cn/cloudream/common/utils/sync2"
|
|
"gitlink.org.cn/cloudream/scheduler/common/pkgs/mq/executor"
|
|
exectsk "gitlink.org.cn/cloudream/scheduler/common/pkgs/mq/executor/task"
|
|
mgrmq "gitlink.org.cn/cloudream/scheduler/common/pkgs/mq/manager"
|
|
"gitlink.org.cn/cloudream/scheduler/executor/internal/globals"
|
|
"reflect"
|
|
)
|
|
|
|
type TaskChan[T any] struct {
|
|
Chan sync2.UnboundChannel[T]
|
|
}
|
|
|
|
func NewTaskChan[T any]() *TaskChan[T] {
|
|
return &TaskChan[T]{Chan: *sync2.NewUnboundChannel[T]()}
|
|
}
|
|
|
|
type Task struct {
|
|
id string
|
|
taskChan TaskChan[any]
|
|
TaskStatusChan TaskChan[any]
|
|
}
|
|
|
|
type TaskContext struct{}
|
|
|
|
func NewTask(id string) *Task {
|
|
return &Task{
|
|
taskChan: *NewTaskChan[any](),
|
|
TaskStatusChan: *NewTaskChan[any](),
|
|
id: id,
|
|
//body: body,
|
|
}
|
|
}
|
|
|
|
type TaskBody interface {
|
|
Execute(task *Task, ctx TaskContext)
|
|
}
|
|
|
|
func (c *Task) SendStatus(status exectsk.TaskStatus) {
|
|
|
|
taskStatus := mgrmq.NewExecutorTaskStatus(globals.ExecutorID, c.ID(), status)
|
|
err := c.TaskStatusChan.Chan.Send(taskStatus)
|
|
if err != nil {
|
|
logger.Error("send task status error: ", err.Error())
|
|
}
|
|
}
|
|
|
|
func (c *Task) SendTaskOperate(info executor.TaskOperateInfo) {
|
|
|
|
err := c.taskChan.Chan.Send(info)
|
|
if err != nil {
|
|
logger.Error(err.Error())
|
|
}
|
|
}
|
|
|
|
func (c *Task) WaitTaskOperate() *any {
|
|
|
|
receive, err := c.taskChan.Chan.Receive()
|
|
if err != nil {
|
|
logger.Error(err.Error())
|
|
return nil
|
|
}
|
|
return &receive
|
|
}
|
|
|
|
func (t *Task) ID() string {
|
|
return t.id
|
|
}
|
|
|
|
var TaskFromInfoCtors map[reflect.Type]func(exectsk.TaskInfo) TaskBody = make(map[reflect.Type]func(exectsk.TaskInfo) TaskBody)
|
|
|
|
func Register[TInfo exectsk.TaskInfo, TTaskBody TaskBody](ctor func(info TInfo) TTaskBody) {
|
|
TaskFromInfoCtors[reflect2.TypeOf[TInfo]()] = func(info exectsk.TaskInfo) TaskBody {
|
|
return ctor(info.(TInfo))
|
|
}
|
|
}
|