pcm-slurm/internal/logic/cronlogic.go

83 lines
2.6 KiB
Go

package logic
import (
"context"
"github.com/jinzhu/copier"
"github.com/zeromicro/go-zero/core/logx"
"gitlink.org.cn/jcce-pcm/pcm-coordinator/rpc/pcmcoreclient"
"gitlink.org.cn/jcce-pcm/pcm-participant-slurm/rpc/hpcTH"
"gitlink.org.cn/jcce-pcm/pcm-participant-slurm/rpc/internal/svc"
"gitlink.org.cn/jcce-pcm/utils/enum"
"gitlink.org.cn/jcce-pcm/utils/tool"
"strconv"
"time"
)
func InitCron(svc *svc.ServiceContext) {
svc.Cron.Start()
submitJobLogic := NewSubmitJobLogic(context.Background(), svc)
listLogic := NewListJobLogic(context.Background(), svc)
svc.Cron.AddFunc("*/5 * * * * ?", func() {
// 查询core端分发下来的任务列表
infoReq := pcmcoreclient.InfoListReq{
Kind: "hpc",
ServiceName: "th",
}
infoList, err := svc.PcmCoreRpc.InfoList(context.Background(), &infoReq)
if err != nil {
logx.Error(err)
return
}
// 提交任务
submitJob(infoList, submitJobLogic)
// 查询运行中的任务列表同步信息
listReq := hpcTH.ListJobReq{}
listJob, err := listLogic.ListJob(&listReq)
if err != nil {
logx.Error(err)
return
}
for index, _ := range infoList.HpcInfoList {
for _, job := range listJob.Jobs {
if job.Name == infoList.HpcInfoList[index].Name {
copier.CopyWithOption(&infoList.HpcInfoList[index], job, copier.Option{Converters: tool.Converters})
infoList.HpcInfoList[index].JobId = strconv.Itoa(int(job.JobId))
infoList.HpcInfoList[index].StartTime = time.Unix(job.StartTime, 0).String()
infoList.HpcInfoList[index].RunningTime = int64(time.Now().Sub(time.Unix(job.StartTime, 0)).Seconds())
infoList.HpcInfoList[index].Status = enum.State(job.JobState).String()
infoList.HpcInfoList[index].Version = "slurm 2.6.9"
}
}
}
// 同步信息到core端
if len(infoList.HpcInfoList) != 0 {
syncInfoReq := pcmcoreclient.SyncInfoReq{
Kind: "hpc",
ServiceName: "th",
HpcInfoList: infoList.HpcInfoList,
}
svc.PcmCoreRpc.SyncInfo(context.Background(), &syncInfoReq)
}
})
}
func submitJob(infoList *pcmcoreclient.InfoListResp, submitJobLogic *SubmitJobLogic) {
for index, _ := range infoList.HpcInfoList {
if infoList.HpcInfoList[index].Status == "Saved" {
submitReq := hpcTH.SubmitJobReq{
Account: infoList.HpcInfoList[index].Account,
Name: infoList.HpcInfoList[index].Name,
WorkDir: "/root",
Script: infoList.HpcInfoList[index].CmdScript,
UserId: 0,
MinNodes: 1,
}
jobResult, _ := submitJobLogic.SubmitJob(&submitReq)
// 任务提交成功
infoList.HpcInfoList[index].Status = "Pending"
infoList.HpcInfoList[index].JobId = strconv.Itoa(int(jobResult.SubmitResponseMsg[0].JobId))
}
}
}