From ff7aae0a88284823def65d1547cacc48a48351f6 Mon Sep 17 00:00:00 2001 From: jagger Date: Tue, 30 Apr 2024 19:19:02 +0800 Subject: [PATCH] fix Signed-off-by: jagger Former-commit-id: 7ea02a9f7d2e00567c7fd8d9a6a09fa29fc9e3ce --- api/client/types.go | 22 +++++++------- api/internal/logic/core/pushtaskinfologic.go | 32 +++++++++++++++----- pkg/models/cloud/task_cloud.go | 2 +- 3 files changed, 36 insertions(+), 20 deletions(-) diff --git a/api/client/types.go b/api/client/types.go index 4bbce2e94..940c88df3 100644 --- a/api/client/types.go +++ b/api/client/types.go @@ -111,17 +111,17 @@ type HpcInfo struct { } type CloudInfo struct { - Id uint `json:"id,omitempty"` - TaskId int64 `json:"taskId,omitempty"` - AdapterId uint `json:"adapterId,omitempty"` - ClusterId uint `json:"clusterId,omitempty"` - ClusterName string `json:"clusterName,omitempty"` - Kind string `json:"kind,omitempty"` - Status string `json:"status,omitempty"` - StartTime *time.Time `json:"startTime,omitempty"` - YamlString string `json:"yamlString,omitempty"` - Result string `json:"result,omitempty"` - Namespace string `json:"namespace,omitempty"` + Id uint `json:"id,omitempty,optional"` + TaskId int64 `json:"taskId,omitempty,optional"` + AdapterId uint `json:"adapterId,omitempty,optional"` + ClusterId uint `json:"clusterId,omitempty,optional"` + ClusterName string `json:"clusterName,omitempty,optional"` + Kind string `json:"kind,omitempty,optional"` + Status string `json:"status,omitempty,optional"` + StartTime *time.Time `json:"startTime,omitempty,optional,string"` + YamlString string `json:"yamlString,omitempty,optional"` + Result string `json:"result,omitempty,optional"` + Namespace string `json:"namespace,omitempty,optional"` } type AiInfo struct { diff --git a/api/internal/logic/core/pushtaskinfologic.go b/api/internal/logic/core/pushtaskinfologic.go index 1c056a86d..dbc93f3a1 100644 --- a/api/internal/logic/core/pushtaskinfologic.go +++ b/api/internal/logic/core/pushtaskinfologic.go @@ -2,14 +2,15 @@ package core import ( "context" + "github.com/pkg/errors" + "github.com/zeromicro/go-zero/core/logx" clientCore "gitlink.org.cn/JointCloud/pcm-coordinator/api/client" + "gitlink.org.cn/JointCloud/pcm-coordinator/api/internal/svc" "gitlink.org.cn/JointCloud/pcm-coordinator/pkg/constants" "gitlink.org.cn/JointCloud/pcm-coordinator/pkg/models" "gorm.io/gorm" "strings" - - "github.com/zeromicro/go-zero/core/logx" - "gitlink.org.cn/JointCloud/pcm-coordinator/api/internal/svc" + "time" ) type PushTaskInfoLogic struct { @@ -33,9 +34,14 @@ func (l *PushTaskInfoLogic) PushTaskInfo(req *clientCore.PushTaskInfoReq) (*clie switch kind { case 0: for _, cloudInfo := range req.CloudInfoList { - l.svcCtx.DbEngin.Exec("update cloud set status = ?,start_time = ?,result = ? where participant_id = ? and id = ?", - cloudInfo.Status, cloudInfo.StartTime, cloudInfo.Result, req.AdapterId, cloudInfo.Id) - syncTask(l.svcCtx.DbEngin, cloudInfo.TaskId) + var taskId uint + result := l.svcCtx.DbEngin.Table("task_cloud").Select("task_id").Where("cluster_name=? and adapter_id=? and kind=?", cloudInfo.ClusterName, cloudInfo.AdapterId, cloudInfo.Kind).Find(&taskId) + if errors.Is(result.Error, gorm.ErrRecordNotFound) { + return nil, errors.New("Record does not exist") + } + l.svcCtx.DbEngin.Exec("update task_cloud set status = ?,start_time = ?,result = ? where cluster_name = ? and adapter_id = ?", + cloudInfo.Status, cloudInfo.StartTime, cloudInfo.Result, cloudInfo.ClusterName, cloudInfo.AdapterId) + syncTask(l.svcCtx.DbEngin, int64(taskId)) } case 2: for _, hpcInfo := range req.HpcInfoList { @@ -63,7 +69,7 @@ func (l *PushTaskInfoLogic) PushTaskInfo(req *clientCore.PushTaskInfoReq) (*clie func syncTask(gorm *gorm.DB, taskId int64) { var allStatus string - tx := gorm.Raw("SELECT CONCAT_WS(',',GROUP_CONCAT(DISTINCT h.status) ,GROUP_CONCAT(DISTINCT a.status) ,GROUP_CONCAT(DISTINCT c.status))as status from task t left join hpc h on t.id = h.task_id left join cloud c on t.id = c.task_id left join ai a on t.id = a.task_id where t.id = ?", taskId).Scan(&allStatus) + tx := gorm.Raw("SELECT CONCAT_WS(',',GROUP_CONCAT(DISTINCT h.status) ,GROUP_CONCAT(DISTINCT a.status) ,GROUP_CONCAT(DISTINCT c.status))as status from task t left join hpc h on t.id = h.task_id left join task_cloud c on t.id = c.task_id left join ai a on t.id = a.task_id where t.id = ?", taskId).Scan(&allStatus) if tx.Error != nil { logx.Error(tx.Error) } @@ -79,7 +85,7 @@ func syncTask(gorm *gorm.DB, taskId int64) { } if strings.Contains(allStatus, constants.Running) { - updateTask(gorm, taskId, constants.Running) + updateTaskRunning(gorm, taskId, constants.Running) } } @@ -93,6 +99,16 @@ func updateTask(gorm *gorm.DB, taskId int64, status string) { } } +func updateTaskRunning(gorm *gorm.DB, taskId int64, status string) { + var task models.Task + gorm.Where("id = ? ", taskId).Find(&task) + if task.Status != status { + task.Status = status + task.StartTime = time.Now().Format("2006-01-02 15:04:05") + gorm.Updates(&task) + } +} + func removeRepeatedElement(arr []string) (newArr []string) { newArr = make([]string, 0) for i := 0; i < len(arr); i++ { diff --git a/pkg/models/cloud/task_cloud.go b/pkg/models/cloud/task_cloud.go index 3dec32bc3..d60c236ea 100644 --- a/pkg/models/cloud/task_cloud.go +++ b/pkg/models/cloud/task_cloud.go @@ -13,7 +13,7 @@ type TaskCloudModel struct { ClusterName string `json:"clusterName" gorm:"not null;comment:集群名称"` Kind string `json:"kind" gorm:"comment:种类"` Status string `json:"status" gorm:"comment:状态"` - StartTime *time.Time `json:"startTime" gorm:"comment:开始时间"` + StartTime *time.Time `json:"startTime,string" gorm:"comment:开始时间"` YamlString string `json:"yamlString" gorm:"not null;comment:入参"` Result string `json:"result" gorm:"comment:运行结果"` Namespace string `json:"namespace" gorm:"comment:命名空间"`