定时任务代码简化

This commit is contained in:
zhangwei 2023-04-27 16:14:40 +08:00
parent dbd3eb0139
commit adeacc8b3f
3 changed files with 29 additions and 44 deletions

View File

@ -14,12 +14,12 @@ func InitCron(svc *svc.ServiceContext) {
submitJobLogic := NewSubmitJobLogic(context.Background(), svc) submitJobLogic := NewSubmitJobLogic(context.Background(), svc)
listLogic := NewListJobLogic(context.Background(), svc) listLogic := NewListJobLogic(context.Background(), svc)
svc.Cron.AddFunc("*/5 * * * * ?", func() { svc.Cron.AddFunc("*/5 * * * * ?", func() {
syncInfoReq := pcmcoreclient.SyncInfoReq{ // 查询core端分发下来的任务列表
infoReq := pcmcoreclient.InfoListReq{
Kind: "hpc", Kind: "hpc",
ServiceName: "ac", ServiceName: "ac",
} }
// 查询core端分发下来的任务列表 infoList, err := svc.PcmCoreRpc.InfoList(context.Background(), &infoReq)
infoList, err := queryCoreInfoList(svc)
if err != nil { if err != nil {
logx.Error(err) logx.Error(err)
return return
@ -27,8 +27,7 @@ func InitCron(svc *svc.ServiceContext) {
// 提交任务 // 提交任务
submitJob(infoList, submitJobLogic) submitJob(infoList, submitJobLogic)
// 查询运行中的任务列表同步信息 // 查询运行中的任务列表同步信息
listReq := hpcAC.ListJobReq{} listJob, err := listLogic.ListJob(&hpcAC.ListJobReq{})
listJob, err := listLogic.ListJob(&listReq)
if err != nil { if err != nil {
logx.Error(err) logx.Error(err)
return return
@ -45,7 +44,11 @@ func InitCron(svc *svc.ServiceContext) {
} }
// 同步信息到core端 // 同步信息到core端
if len(infoList.HpcInfoList) != 0 { if len(infoList.HpcInfoList) != 0 {
syncInfoReq.HpcInfoList = infoList.HpcInfoList syncInfoReq := pcmcoreclient.SyncInfoReq{
Kind: "hpc",
ServiceName: "ac",
HpcInfoList: infoList.HpcInfoList,
}
svc.PcmCoreRpc.SyncInfo(context.Background(), &syncInfoReq) svc.PcmCoreRpc.SyncInfo(context.Background(), &syncInfoReq)
} }
}) })
@ -77,21 +80,9 @@ func submitJob(infoList *pcmcoreclient.InfoListResp, submitJobLogic *SubmitJobLo
infoList.HpcInfoList[index].Status = "Pending" infoList.HpcInfoList[index].Status = "Pending"
infoList.HpcInfoList[index].JobId = jobResult.Data infoList.HpcInfoList[index].JobId = jobResult.Data
} else { } else {
infoList.HpcInfoList[index].Result = "Failed" infoList.HpcInfoList[index].Status = "Failed"
infoList.HpcInfoList[index].Result = jobResult.Msg infoList.HpcInfoList[index].Result = jobResult.Msg
} }
} }
} }
} }
func queryCoreInfoList(svc *svc.ServiceContext) (*pcmcoreclient.InfoListResp, error) {
infoReq := pcmcoreclient.InfoListReq{
Kind: "hpc",
ServiceName: "ac",
}
infoList, err := svc.PcmCoreRpc.InfoList(context.Background(), &infoReq)
if err != nil {
return nil, err
}
return infoList, nil
}

View File

@ -16,12 +16,12 @@ func InitCron(svc *svc.ServiceContext) {
submitJobLogic := NewSubmitJobLogic(context.Background(), svc) submitJobLogic := NewSubmitJobLogic(context.Background(), svc)
listLogic := NewListJobLogic(context.Background(), svc) listLogic := NewListJobLogic(context.Background(), svc)
svc.Cron.AddFunc("*/5 * * * * ?", func() { svc.Cron.AddFunc("*/5 * * * * ?", func() {
syncInfoReq := pcmcoreclient.SyncInfoReq{ // 查询core端分发下来的任务列表
infoReq := pcmcoreclient.InfoListReq{
Kind: "hpc", Kind: "hpc",
ServiceName: "th", ServiceName: "th",
} }
// 查询core端分发下来的任务列表 infoList, err := svc.PcmCoreRpc.InfoList(context.Background(), &infoReq)
infoList, err := queryCoreInfoList(svc)
if err != nil { if err != nil {
logx.Error(err) logx.Error(err)
return return
@ -40,14 +40,18 @@ func InitCron(svc *svc.ServiceContext) {
if job.Name == infoList.HpcInfoList[index].Name { if job.Name == infoList.HpcInfoList[index].Name {
infoList.HpcInfoList[index].JobId = strconv.Itoa(int(job.JobId)) infoList.HpcInfoList[index].JobId = strconv.Itoa(int(job.JobId))
infoList.HpcInfoList[index].StartTime = time.Unix(job.StartTime, 0).String() infoList.HpcInfoList[index].StartTime = time.Unix(job.StartTime, 0).String()
infoList.HpcInfoList[index].RunningTime = time.Now().Sub(time.Unix(job.StartTime, 0)).Milliseconds() 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].Status = enum.State(job.JobState).String()
} }
} }
} }
// 同步信息到core端 // 同步信息到core端
if len(infoList.HpcInfoList) != 0 { if len(infoList.HpcInfoList) != 0 {
syncInfoReq.HpcInfoList = infoList.HpcInfoList syncInfoReq := pcmcoreclient.SyncInfoReq{
Kind: "hpc",
ServiceName: "th",
HpcInfoList: infoList.HpcInfoList,
}
svc.PcmCoreRpc.SyncInfo(context.Background(), &syncInfoReq) svc.PcmCoreRpc.SyncInfo(context.Background(), &syncInfoReq)
} }
}) })
@ -68,15 +72,3 @@ func submitJob(infoList *pcmcoreclient.InfoListResp, submitJobLogic *SubmitJobLo
} }
} }
} }
func queryCoreInfoList(svc *svc.ServiceContext) (*pcmcoreclient.InfoListResp, error) {
infoReq := pcmcoreclient.InfoListReq{
Kind: "hpc",
ServiceName: "th",
}
infoList, err := svc.PcmCoreRpc.InfoList(context.Background(), &infoReq)
if err != nil {
return nil, err
}
return infoList, nil
}

View File

@ -14,11 +14,6 @@ import (
func InitCron(svc *svc.ServiceContext) { func InitCron(svc *svc.ServiceContext) {
svc.Cron.AddFunc("*/5 * * * * ?", func() { svc.Cron.AddFunc("*/5 * * * * ?", func() {
SyncInfoReq := pcmcoreclient.SyncInfoReq{
Kind: "cloud",
ServiceName: "kubeNative",
}
// 查询core端分发下来的任务列表 // 查询core端分发下来的任务列表
infoReq := pcmcoreclient.InfoListReq{ infoReq := pcmcoreclient.InfoListReq{
Kind: "cloud", Kind: "cloud",
@ -68,8 +63,15 @@ func InitCron(svc *svc.ServiceContext) {
} }
} }
} }
if len(infoList.CloudInfoList) != 0 {
// 同步信息到core端 // 同步信息到core端
SyncInfoReq.CloudInfoList = infoList.CloudInfoList SyncInfoReq := pcmcoreclient.SyncInfoReq{
Kind: "cloud",
ServiceName: "kubeNative",
CloudInfoList: infoList.CloudInfoList,
}
svc.PcmCoreRpc.SyncInfo(context.Background(), &SyncInfoReq) svc.PcmCoreRpc.SyncInfo(context.Background(), &SyncInfoReq)
}
}) })
} }