pcm-ac/internal/logic/submitjoblogic.go

69 lines
1.6 KiB
Go

package logic
import (
"bytes"
"context"
"encoding/json"
"gitlink.org.cn/jcce-pcm/pcm-ac/hpcAC"
"gitlink.org.cn/jcce-pcm/pcm-ac/internal/common"
"gitlink.org.cn/jcce-pcm/pcm-ac/internal/svc"
"io/ioutil"
"log"
"net/http"
"strings"
"time"
"github.com/zeromicro/go-zero/core/logx"
)
type SubmitJobLogic struct {
ctx context.Context
svcCtx *svc.ServiceContext
logx.Logger
}
func NewSubmitJobLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SubmitJobLogic {
return &SubmitJobLogic{
ctx: ctx,
svcCtx: svcCtx,
Logger: logx.WithContext(ctx),
}
}
func (l *SubmitJobLogic) SubmitJob(in *hpcAC.SubmitJobReq) (*hpcAC.SubmitJobResp, error) {
resp := &hpcAC.SubmitJobResp{}
jobSubmitUrl := "/hpc/openapi/v2/apptemplates/{apptype}/{appname}/job"
jobSubmitUrl = strings.Replace(jobSubmitUrl, "{apptype}", in.Apptype, -1)
jobSubmitUrl = strings.Replace(jobSubmitUrl, "{appname}", in.Appname, -1)
jsonStr, _ := json.Marshal(in)
req_url, err := http.NewRequest("POST", common.HpcCenterUrlPrefix()+jobSubmitUrl, bytes.NewBuffer(jsonStr))
getTokenLogic := NewGetACTokenLogic(l.ctx, l.svcCtx)
tokenResp, _ := getTokenLogic.GetACToken(&hpcAC.ACTokenReq{})
token := tokenResp.GetData().Token
req_url.Header.Add("content-type", "application/json")
req_url.Header.Add("token", token)
if err != nil {
log.Fatal(err)
}
defer req_url.Body.Close()
c := http.Client{Timeout: time.Duration(5) * time.Second}
//c := &http.Client{Timeout: 5 * time.Second}
respUrl, err := c.Do(req_url)
if err != nil {
return resp, err
}
result, _ := ioutil.ReadAll(respUrl.Body)
json.Unmarshal([]byte(string(result)), &resp)
return resp, nil
}