pcm-hpc/ac/job_impl.go

90 lines
2.3 KiB
Go

package ac
import (
"encoding/json"
"errors"
"github.com/go-resty/resty/v2"
"log"
"strconv"
"strings"
"sync"
)
type job struct {
sync.RWMutex
client *client
options *JobOptions
log log.Logger
}
func newJob(client *client, options *JobOptions) (*job, error) {
job := &job{
RWMutex: sync.RWMutex{},
client: client,
options: options,
log: log.Logger{},
}
return job, nil
}
func (j *job) ListJob(listJobReq ListJobReq) string {
jobUrl := "/hpc/openapi/v2/jobs?"
clusterId := 1637920656
params := map[string]string{
"strClusterIDList": strconv.FormatInt(int64(clusterId), 10),
}
httpClient := resty.New().R()
result, _ := httpClient.SetHeader("token", j.client.token).SetQueryParams(params).Get(j.client.baseEndpoint + jobUrl)
return string(result.Body())
}
func (j *job) GetJob(getJobReq GetJobReq) (getJobResp GetJobResp, err error) {
jobUrl := "/hpc/openapi/v2/jobs?"
clusterId := 1637920656
params := map[string]string{
"strClusterIDList": strconv.FormatInt(int64(clusterId), 10),
"jobId": getJobReq.JobId,
}
httpClient := resty.New().R()
result, _ := httpClient.SetHeader("token", j.client.token).SetQueryParams(params).Get(j.client.baseEndpoint + jobUrl)
json.Unmarshal(result.Body(), &getJobResp)
if getJobResp.Code != "200" {
return getJobResp, errors.New(getJobResp.Msg)
}
return getJobResp, nil
}
func (j *job) CancelJob(cancelJobReq CancelJobReq) string {
//TODO implement me
panic("implement me")
}
func (j *job) SubmitJob(submitJobReq SubmitJobReq) (submitJobResp SubmitJobResp, err error) {
jobSubmitUrl := "/hpc/openapi/v2/apptemplates/{apptype}/{appname}/job"
jobSubmitUrl = strings.Replace(jobSubmitUrl, "{apptype}", submitJobReq.MapAppJobInfo.GapAppType, -1)
jobSubmitUrl = strings.Replace(jobSubmitUrl, "{appname}", submitJobReq.MapAppJobInfo.GapAppName, -1)
httpClient := resty.New().R()
jsonStr, _ := json.Marshal(submitJobReq)
params := map[string]string{
"content-type": "application/json",
"token": j.client.token,
}
result, _ := httpClient.SetHeaders(params).SetBody(jsonStr).Post(j.client.baseEndpoint + jobSubmitUrl)
err = json.Unmarshal(result.Body(), &submitJobResp)
if err != nil {
return SubmitJobResp{}, err
}
if submitJobResp.Code != "200" {
return submitJobResp, errors.New(submitJobResp.Msg)
}
return submitJobResp, nil
}