| @@ -22,6 +22,11 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) { | |||||
| Path: "/listHistoryJob", | Path: "/listHistoryJob", | ||||
| Handler: listHistoryJobHandler(serverCtx), | Handler: listHistoryJobHandler(serverCtx), | ||||
| }, | }, | ||||
| { | |||||
| Method: http.MethodPost, | |||||
| Path: "/scheduleTask", | |||||
| Handler: scheduleTaskHandler(serverCtx), | |||||
| }, | |||||
| }, | }, | ||||
| ) | ) | ||||
| } | } | ||||
| @@ -0,0 +1,30 @@ | |||||
| package handler | |||||
| import ( | |||||
| "PCM/adaptor/slurm/slurmCore/api/internal/logic" | |||||
| "PCM/adaptor/slurm/slurmCore/api/internal/svc" | |||||
| "PCM/adaptor/slurm/slurmCore/api/internal/types" | |||||
| "PCM/common/tool" | |||||
| "github.com/zeromicro/go-zero/rest/httpx" | |||||
| "net/http" | |||||
| ) | |||||
| func scheduleTaskHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { | |||||
| return func(w http.ResponseWriter, r *http.Request) { | |||||
| var req types.ScheduleTaskReq | |||||
| if err := httpx.Parse(r, &req); err != nil { | |||||
| httpx.ErrorCtx(r.Context(), w, err) | |||||
| return | |||||
| } | |||||
| l := logic.NewScheduleTaskLogic(r.Context(), svcCtx) | |||||
| // 解析yaml文件 | |||||
| _, fileHeader, err := r.FormFile("file") | |||||
| err = tool.Yaml2struct(fileHeader, &req) | |||||
| resp, err := l.ScheduleTask(&req) | |||||
| if err != nil { | |||||
| httpx.ErrorCtx(r.Context(), w, err) | |||||
| } else { | |||||
| httpx.OkJsonCtx(r.Context(), w, resp) | |||||
| } | |||||
| } | |||||
| } | |||||
| @@ -0,0 +1,33 @@ | |||||
| package logic | |||||
| import ( | |||||
| "context" | |||||
| "PCM/adaptor/slurm/slurmCore/api/internal/svc" | |||||
| "PCM/adaptor/slurm/slurmCore/api/internal/types" | |||||
| "github.com/zeromicro/go-zero/core/logx" | |||||
| ) | |||||
| type ScheduleTaskLogic struct { | |||||
| logx.Logger | |||||
| ctx context.Context | |||||
| svcCtx *svc.ServiceContext | |||||
| } | |||||
| func NewScheduleTaskLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ScheduleTaskLogic { | |||||
| return &ScheduleTaskLogic{ | |||||
| Logger: logx.WithContext(ctx), | |||||
| ctx: ctx, | |||||
| svcCtx: svcCtx, | |||||
| } | |||||
| } | |||||
| func (l *ScheduleTaskLogic) ScheduleTask(req *types.ScheduleTaskReq) (resp *types.ScheduleTaskResp, err error) { | |||||
| // todo | |||||
| switch req.Kind { | |||||
| } | |||||
| println(req.Metadata) | |||||
| return resp, err | |||||
| } | |||||
| @@ -162,3 +162,14 @@ type ListHistoryJobResp struct { | |||||
| RecordCount int32 `json:"recordCount"` | RecordCount int32 `json:"recordCount"` | ||||
| HistoryJobs []HistoryJob `json:"historyJobs"` | HistoryJobs []HistoryJob `json:"historyJobs"` | ||||
| } | } | ||||
| type ScheduleTaskReq struct { | |||||
| Operate string `yaml:"operate"` | |||||
| Kind string `yaml:"kind"` | |||||
| Metadata string `yaml:"metadata"` | |||||
| } | |||||
| type ScheduleTaskResp struct { | |||||
| Code int32 `json:"code"` | |||||
| Msg string `json:"msg"` | |||||
| } | |||||
| @@ -176,6 +176,18 @@ type ( | |||||
| } | } | ||||
| ) | ) | ||||
| type ( | |||||
| scheduleTaskReq { | |||||
| Operate string `yaml:"operate"` | |||||
| Kind string `yaml:"kind"` | |||||
| Metadata string `yaml:"metadata"` | |||||
| } | |||||
| scheduleTaskResp { | |||||
| Code int32 `json:"code"` | |||||
| Msg string `json:"msg"` | |||||
| } | |||||
| ) | |||||
| service slurmcore-api { | service slurmcore-api { | ||||
| @handler listJobHandler | @handler listJobHandler | ||||
| @@ -183,4 +195,8 @@ service slurmcore-api { | |||||
| @handler listHistoryJobHandler | @handler listHistoryJobHandler | ||||
| get /listHistoryJob (listHistoryJobReq) returns (listHistoryJobResp) | get /listHistoryJob (listHistoryJobReq) returns (listHistoryJobResp) | ||||
| @handler scheduleTaskHandler | |||||
| post /scheduleTask (scheduleTaskReq) returns (scheduleTaskResp) | |||||
| } | } | ||||
| @@ -0,0 +1,20 @@ | |||||
| package tool | |||||
| import ( | |||||
| "io" | |||||
| "mime/multipart" | |||||
| "sigs.k8s.io/yaml" | |||||
| ) | |||||
| func Yaml2struct(fileHeader *multipart.FileHeader, req interface{}) error { | |||||
| file, err := fileHeader.Open() | |||||
| if err != nil { | |||||
| return err | |||||
| } | |||||
| fileByte, err := io.ReadAll(file) | |||||
| if err != nil { | |||||
| return err | |||||
| } | |||||
| yaml.Unmarshal(fileByte, &req) | |||||
| return nil | |||||
| } | |||||