JCC-CSScheduler/executor/internal/task/pcm_schedule_task.go

89 lines
2.0 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package task
import (
"fmt"
"time"
"gitlink.org.cn/cloudream/common/api/pcm"
"gitlink.org.cn/cloudream/common/pkgs/logger"
"gitlink.org.cn/cloudream/common/pkgs/task"
"gitlink.org.cn/cloudream/scheduler/common/globals"
exectsk "gitlink.org.cn/cloudream/scheduler/common/pkgs/mq/executor/task"
)
type PCMScheduleTask struct {
nodeID int64
envs []map[string]string
imageID int64
cmdLine string
}
func NewPCMScheduleTask(nodeID int64, envs []map[string]string, imageID int64, cmdLine string) *PCMScheduleTask {
return &PCMScheduleTask{
nodeID: nodeID,
envs: envs,
imageID: imageID,
cmdLine: cmdLine,
}
}
func (t *PCMScheduleTask) Execute(task *task.Task[TaskContext], ctx TaskContext, complete CompleteFn) {
log := logger.WithType[PCMScheduleTask]("Task")
log.Debugf("begin")
defer log.Debugf("end")
err := t.do(task.ID(), ctx)
if err != nil {
//TODO 若任务失败上报的状态failed字段根据情况修改
ctx.reporter.Report(task.ID(), exectsk.NewScheduleTaskStatus("failed", err.Error(), 0))
}
ctx.reporter.ReportNow()
complete(err, CompleteOption{
RemovingDelay: time.Minute,
})
}
func (t *PCMScheduleTask) do(taskID string, ctx TaskContext) error {
pcmCli, err := globals.PCMPool.Acquire()
if err != nil {
return fmt.Errorf("new pcm client: %w", err)
}
defer pcmCli.Close()
resp, err := pcmCli.ScheduleTask(pcm.ScheduleTaskReq{
NodeID: t.nodeID,
Envs: t.envs,
ImageID: t.imageID,
CMDLine: t.cmdLine,
})
if err != nil {
return err
}
var prevStatus string
for {
tsResp, err := pcmCli.GetTaskStatus(pcm.GetTaskStatusReq{
NodeID: t.nodeID,
PCMJobID: resp.PCMJobID,
})
if err != nil {
return err
}
if tsResp.Status != prevStatus {
ctx.reporter.Report(taskID, exectsk.NewScheduleTaskStatus(tsResp.Status, "", resp.PCMJobID))
}
prevStatus = tsResp.Status
// TODO 根据接口result返回情况修改
// 根据返回的result判定任务是否完成若完成 跳出循环,结束任务
if tsResp.Status == "Completed" {
return nil
}
}
}