From 4ff16ce7f59b72bfc58dd7dfdfbfe98267adc161 Mon Sep 17 00:00:00 2001 From: zhangwei <894646498@qq.com> Date: Tue, 26 Nov 2024 15:54:39 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8F=96=E6=B6=88=E4=BB=BB=E5=8A=A1=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3=E5=A2=9E=E5=8A=A0api?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- routers/router.go | 1 + routers/v1/job.go | 65 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+) diff --git a/routers/router.go b/routers/router.go index 0c87193..6342f35 100644 --- a/routers/router.go +++ b/routers/router.go @@ -16,6 +16,7 @@ func InitRouter() *gin.Engine { namespace := apiv1.Group("job") namespace.POST("/submit", v1.SubmitJob) namespace.GET("/info", v1.JobInfo) + namespace.DELETE("/cancel", v1.CancelJob) } return r diff --git a/routers/v1/job.go b/routers/v1/job.go index b878160..245e309 100644 --- a/routers/v1/job.go +++ b/routers/v1/job.go @@ -28,6 +28,14 @@ type JobInfoReq struct { JobId string `json:"jobId" form:"jobId"` } +type JobCancelReq struct { + Server string `json:"server" form:"server""` + Version string `json:"version" form:"version""` + Username string `json:"username" form:"username"` + Token string `json:"token" form:"token"` + JobId string `json:"jobId" form:"jobId"` +} + type SubmitJobResp struct { Meta struct { Plugin struct { @@ -49,6 +57,24 @@ type SubmitJobResp struct { JobSubmitUserMsg string `json:"job_submit_user_msg"` } +type Job struct { + Meta struct { + Plugin struct { + Type string `json:"type"` + Name string `json:"name"` + } `json:"plugin"` + Slurm struct { + Version struct { + Major int `json:"major"` + Micro int `json:"micro"` + Minor int `json:"minor"` + } `json:"version"` + Release string `json:"release"` + } `json:"Slurm"` + } `json:"meta"` + Errors []interface{} `json:"errors"` +} + type JobOptions struct { Script string `json:"script"` Job *JobProperties `json:"job"` @@ -136,3 +162,42 @@ func JobInfo(ctx *gin.Context) { json.Unmarshal(body, &resp) ResponseData(ctx, &resp) } + +func CancelJob(ctx *gin.Context) { + var req JobCancelReq + if err := ctx.ShouldBindQuery(&req); err != nil { + Response(ctx, http.StatusBadRequest, "invalid request params.", "") + return + } + + url := req.Server + "/slurm/" + req.Version + "/job/" + req.JobId + client := &http.Client{} + request, err := http.NewRequest("DELETE", url, nil) + + if err != nil { + fmt.Println(err) + return + } + request.Header.Add("Accept", "*/*") + request.Header.Add("Host", req.Server) + request.Header.Add("X-SLURM-USER-NAME", req.Username) + request.Header.Add("X-SLURM-USER-TOKEN", req.Token) + request.Header.Add("Cookie", "X-SLURM-USER-NAME=;X-SLURM-USER-TOKEN=") + request.Header.Add("Content-Type", "application/json") + + res, err := client.Do(request) + if err != nil { + fmt.Println(err) + return + } + defer res.Body.Close() + + body, err := ioutil.ReadAll(res.Body) + if err != nil { + Response(ctx, http.StatusInternalServerError, "rest api error.", err.Error()) + return + } + var job Job + json.Unmarshal(body, &job) + ResponseData(ctx, &job) +}