From a91b93ad73485d31ac6f74e920f184087351b4b3 Mon Sep 17 00:00:00 2001 From: zhangwei <894646498@qq.com> Date: Fri, 15 Sep 2023 10:04:08 +0800 Subject: [PATCH] =?UTF-8?q?goctl=E7=94=9F=E6=88=90=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Former-commit-id: ac8a60ae04326314ea2b61e1135d37adbd40080d --- api/desc/core/pcm-core.api | 4 ++ api/desc/pcm.api | 4 +- .../handler/core/deletetaskhandler.go | 25 +++++++ api/internal/handler/routes.go | 13 ++-- api/internal/logic/core/deletetasklogic.go | 34 ++++++++++ api/internal/logic/fifo/data.go | 65 +++++++++++++++++++ api/internal/logic/fifo/queue.go | 31 +++++++++ api/internal/logic/fifo/takFIFO.go | 57 ++++++++++++++++ api/internal/types/types.go | 4 ++ model/cloudmodel_gen.go | 30 ++++----- model/taskmodel_gen.go | 1 + 11 files changed, 246 insertions(+), 22 deletions(-) create mode 100644 api/internal/handler/core/deletetaskhandler.go create mode 100644 api/internal/logic/core/deletetasklogic.go create mode 100644 api/internal/logic/fifo/data.go create mode 100644 api/internal/logic/fifo/queue.go create mode 100644 api/internal/logic/fifo/takFIFO.go diff --git a/api/desc/core/pcm-core.api b/api/desc/core/pcm-core.api index adbe4db4..b8ba58aa 100644 --- a/api/desc/core/pcm-core.api +++ b/api/desc/core/pcm-core.api @@ -170,6 +170,10 @@ type ( } ) +type deleteTaskReq { + Id int64 `path:"id"` +} + type ( scheduleTaskByYamlReq { diff --git a/api/desc/pcm.api b/api/desc/pcm.api index a7fc4b12..efcd9913 100644 --- a/api/desc/pcm.api +++ b/api/desc/pcm.api @@ -24,13 +24,15 @@ info( group : core ) service pcm { - @handler participantListHandler get /core/participantList returns (participantListResp) @handler scheduleTaskByYamlHandler post /core/scheduleTaskByYaml (scheduleTaskByYamlReq) + @handler deleteTaskHandler + delete /core/deleteTask/:id (deleteTaskReq) + @handler scheduleTaskHandler post /core/scheduleTask (scheduleTaskReq) diff --git a/api/internal/handler/core/deletetaskhandler.go b/api/internal/handler/core/deletetaskhandler.go new file mode 100644 index 00000000..a2cd712b --- /dev/null +++ b/api/internal/handler/core/deletetaskhandler.go @@ -0,0 +1,25 @@ +package core + +import ( + "gitlink.org.cn/jcce-pcm/utils/result" + "net/http" + + "github.com/zeromicro/go-zero/rest/httpx" + "gitlink.org.cn/jcce-pcm/pcm-coordinator/api/internal/logic/core" + "gitlink.org.cn/jcce-pcm/pcm-coordinator/api/internal/svc" + "gitlink.org.cn/jcce-pcm/pcm-coordinator/api/internal/types" +) + +func DeleteTaskHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + var req types.DeleteTaskReq + if err := httpx.Parse(r, &req); err != nil { + httpx.ErrorCtx(r.Context(), w, err) + return + } + + l := core.NewDeleteTaskLogic(r.Context(), svcCtx) + err := l.DeleteTask(&req) + result.HttpResult(r, w, nil, err) + } +} diff --git a/api/internal/handler/routes.go b/api/internal/handler/routes.go index d420ca8d..1b74af67 100644 --- a/api/internal/handler/routes.go +++ b/api/internal/handler/routes.go @@ -30,6 +30,11 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) { Path: "/core/scheduleTaskByYaml", Handler: core.ScheduleTaskByYamlHandler(serverCtx), }, + { + Method: http.MethodDelete, + Path: "/core/deleteTask/:id", + Handler: core.DeleteTaskHandler(serverCtx), + }, { Method: http.MethodPost, Path: "/core/scheduleTask", @@ -474,22 +479,22 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) { }, { Method: http.MethodDelete, - Path: "/storelink/deleteImageList", + Path: "/storelink/deleteImage", Handler: storelink.DeleteLinkImageHandler(serverCtx), }, { Method: http.MethodPost, - Path: "/storelink/submitLinkTask", + Path: "/storelink/submitTask", Handler: storelink.SubmitLinkTaskHandler(serverCtx), }, { Method: http.MethodGet, - Path: "/storelink/getLinkTask", + Path: "/storelink/getTask", Handler: storelink.GetLinkTaskHandler(serverCtx), }, { Method: http.MethodDelete, - Path: "/storelink/deleteLinkTask", + Path: "/storelink/deleteTask", Handler: storelink.DeleteLinkTaskHandler(serverCtx), }, }, diff --git a/api/internal/logic/core/deletetasklogic.go b/api/internal/logic/core/deletetasklogic.go new file mode 100644 index 00000000..5371711c --- /dev/null +++ b/api/internal/logic/core/deletetasklogic.go @@ -0,0 +1,34 @@ +package core + +import ( + "context" + "gitlink.org.cn/jcce-pcm/pcm-coordinator/model" + + "gitlink.org.cn/jcce-pcm/pcm-coordinator/api/internal/svc" + "gitlink.org.cn/jcce-pcm/pcm-coordinator/api/internal/types" + + "github.com/zeromicro/go-zero/core/logx" +) + +type DeleteTaskLogic struct { + logx.Logger + ctx context.Context + svcCtx *svc.ServiceContext +} + +func NewDeleteTaskLogic(ctx context.Context, svcCtx *svc.ServiceContext) *DeleteTaskLogic { + return &DeleteTaskLogic{ + Logger: logx.WithContext(ctx), + ctx: ctx, + svcCtx: svcCtx, + } +} + +func (l *DeleteTaskLogic) DeleteTask(req *types.DeleteTaskReq) error { + // todo: add your logic here and delete this line + tx := l.svcCtx.DbEngin.Delete(&model.Task{}, req.Id) + if tx.Error != nil { + return tx.Error + } + return nil +} diff --git a/api/internal/logic/fifo/data.go b/api/internal/logic/fifo/data.go new file mode 100644 index 00000000..c61b9750 --- /dev/null +++ b/api/internal/logic/fifo/data.go @@ -0,0 +1,65 @@ +package fifo + +const ( + ADD = "add" + UPDATE = "update" + DELETE = "delete" + RETRY = "retry" +) + +type Data struct { + Event string + TaskType string + Data interface{} +} + +type IData interface { +} + +type Cloud struct { + Id int64 `db:"id"` // id + TaskId int64 `db:"task_id"` // 任务id + ParticipantId int64 `db:"participant_id"` // 集群静态信息id + ApiVersion string `db:"api_version"` //api版本 + Name string `db:"name"` // 名称 + Namespace string `db:"namespace"` // 命名空间 + Kind string `db:"kind"` // 种类 + Status string `db:"status"` // 状态 + StartTime string `db:"start_time"` // 开始时间 + RunningTime int64 `db:"running_time"` // 运行时长 + YamlString string `db:"yaml_string"` + Result string `db:"result"` // 运行结果 +} + +type Hpc struct { + Id int64 `db:"id"` // id + TaskId int64 `db:"task_id"` // 任务id + ParticipantId int64 `db:"participant_id"` // 集群静态信息id + JobId string `db:"job_id"` // 作业id + Name string `db:"name"` // 名称 + Status string `db:"status"` // 状态 + StartTime string `db:"start_time"` // 开始时间 + RunningTime int64 `db:"running_time"` // 运行时间 + CardCount int64 `db:"card_count"` // 卡数 + WorkDir string `db:"work_dir"` + WallTime string `db:"wall_time"` + Result string `db:"result"` + YamlString string `db:"yaml_string"` + CmdScript string `db:"cmd_script"` + //DerivedEs string `db:"derived_es"` + //Cluster string `db:"cluster"` + //BlockId string `db:"block_id"` + //AllocNodes uint32 `db:"alloc_nodes"` + //AllocCpu uint32 `db:"alloc_cpu"` + //Version string `db:"version"` + //Account string `db:"account"` + //ExitCode uint32 `db:"exit_code"` + //AssocId uint32 `db:"assoc_id"` + AppType string `db:"app_type"` + AppName string `db:"app_name"` + Queue string `db:"queue"` + SubmitType string `db:"submit_type"` + NNode string `db:"n_node"` + StdOutFile string `db:"std_out_file"` + StdErrFile string `db:"std_err_file"` +} diff --git a/api/internal/logic/fifo/queue.go b/api/internal/logic/fifo/queue.go new file mode 100644 index 00000000..dca376e6 --- /dev/null +++ b/api/internal/logic/fifo/queue.go @@ -0,0 +1,31 @@ +package fifo + +import ( + "sync" +) + +type Queue struct { + DataList []*IData + mutex sync.Mutex + ResourceType string +} + +func (q *Queue) Push(data IData) { + q.mutex.Lock() + defer q.mutex.Unlock() + + q.DataList = append(q.DataList, &data) +} + +func (q *Queue) Pop() IData { + q.mutex.Lock() + defer q.mutex.Unlock() + + if len(q.DataList) <= 0 { + return nil + } + + var data = q.DataList[0] + q.DataList = q.DataList[1:] + return data +} diff --git a/api/internal/logic/fifo/takFIFO.go b/api/internal/logic/fifo/takFIFO.go new file mode 100644 index 00000000..e9e873bc --- /dev/null +++ b/api/internal/logic/fifo/takFIFO.go @@ -0,0 +1,57 @@ +package fifo + +import "strings" + +const ( + CLOUD string = "Cloud" + AI string = "Ai" + HPC string = "Hpc" +) + +type TaskFIFO struct { + Queues []*Queue +} + +// NewChannel 初始化队列 +func NewChannel() *TaskFIFO { + channel := TaskFIFO{ + Queues: []*Queue{ + { + ResourceType: CLOUD, + }, + { + ResourceType: AI, + }, + { + ResourceType: HPC, + }, + }, + } + + return &channel +} + +// SelectQueue 根据资源类型查询队列数组 +func (c *TaskFIFO) SelectQueue(resourceType string) *Queue { + for _, queue := range c.Queues { + if strings.EqualFold(queue.ResourceType, resourceType) { + return queue + } + } + return nil +} + +func main() { + channel := NewChannel() + cloudQueue := channel.SelectQueue("cloud") + + dataList := []IData{ + &Data{ + TaskType: "cloud", + Data: &Cloud{}}, + } + + cloudQueue.Push(&dataList) + + println(len(dataList)) +} diff --git a/api/internal/types/types.go b/api/internal/types/types.go index 605d446b..2b5724b3 100644 --- a/api/internal/types/types.go +++ b/api/internal/types/types.go @@ -152,6 +152,10 @@ type Region struct { RunningJobs int64 `json:"runningJobs"` } +type DeleteTaskReq struct { + Id int64 `path:"id"` +} + type ScheduleTaskByYamlReq struct { Name string `yaml:"name"` Synergy string `yaml:"synergy"` diff --git a/model/cloudmodel_gen.go b/model/cloudmodel_gen.go index c0fbdb4f..fab903f2 100644 --- a/model/cloudmodel_gen.go +++ b/model/cloudmodel_gen.go @@ -34,23 +34,19 @@ type ( } Cloud struct { - Id int64 `db:"id"` // id - TaskId int64 `db:"task_id"` // 任务id - ParticipantId int64 `db:"participant_id"` // 集群静态信息id - ApiVersion string `db:"api_version"` //api版本 - Name string `db:"name"` // 名称 - Namespace string `db:"namespace"` // 命名空间 - Kind string `db:"kind"` // 种类 - Status string `db:"status"` // 状态 - StartTime string `db:"start_time"` // 开始时间 - RunningTime int64 `db:"running_time"` // 运行时长 - CreatedBy int64 `db:"created_by"` // 创建人 - CreatedTime sql.NullTime `db:"created_time"` // 创建时间 - UpdatedBy int64 `db:"updated_by"` // 更新人 - UpdatedTime sql.NullTime `db:"updated_time"` // 更新时间 - DeletedFlag int64 `db:"deleted_flag"` // 是否删除(0-否,1-是) - YamlString string `db:"yaml_string"` - Result string `db:"result"` // 运行结果 + Id int64 `db:"id"` // id + TaskId int64 `db:"task_id"` // 任务id + ParticipantId int64 `db:"participant_id"` // 集群静态信息id + ApiVersion string `db:"api_version"` //api版本 + Name string `db:"name"` // 名称 + Namespace string `db:"namespace"` // 命名空间 + Kind string `db:"kind"` // 种类 + Status string `db:"status"` // 状态 + StartTime string `db:"start_time"` // 开始时间 + RunningTime int64 `db:"running_time"` // 运行时长 + DeletedFlag int64 `db:"deleted_flag"` // 是否删除(0-否,1-是) + YamlString string `db:"yaml_string"` + Result string `db:"result"` // 运行结果 } ) diff --git a/model/taskmodel_gen.go b/model/taskmodel_gen.go index 5ff3e48c..d3c31193 100644 --- a/model/taskmodel_gen.go +++ b/model/taskmodel_gen.go @@ -45,6 +45,7 @@ type ( RunningTime int64 `db:"running_time"` // 已运行时间(单位秒) YamlString string `db:"yaml_string"` Result string `db:"result"` // 作业结果 + Deleted_flag int64 `gorm:"softDelete:flag"` } )