74 lines
2.3 KiB
Go
74 lines
2.3 KiB
Go
package common
|
||
|
||
import (
|
||
"context"
|
||
"encoding/json"
|
||
"fmt"
|
||
"remote-task-excutor-cli/pkg/client"
|
||
"remote-task-excutor-cli/pkg/models"
|
||
)
|
||
|
||
// TaskSubmitter 任务提交器,处理通用的任务提交逻辑
|
||
type TaskSubmitter struct {
|
||
httpClient *client.HTTPClient
|
||
taskType string
|
||
}
|
||
|
||
// NewTaskSubmitter 创建新的任务提交器
|
||
func NewTaskSubmitter(httpClient *client.HTTPClient, taskType string) *TaskSubmitter {
|
||
return &TaskSubmitter{
|
||
httpClient: httpClient,
|
||
taskType: taskType,
|
||
}
|
||
}
|
||
|
||
// SubmitTask 提交任务的通用方法,返回 jobSetID、提交请求和错误
|
||
func (ts *TaskSubmitter) SubmitTask(ctx context.Context, authData *models.AuthData, config *models.RunConfig,
|
||
clusterID string, bindResultSet *models.BindResultSet) (string, *models.SubtaskRequest, error) {
|
||
|
||
// 构建任务请求
|
||
submitTaskReq := BuildSubmitTaskRequest(ctx, authData, config, clusterID, bindResultSet, ts.taskType)
|
||
|
||
// 设置认证头
|
||
ts.httpClient.SetHeader("Authorization", "Bearer "+authData.Token)
|
||
|
||
// 打印请求参数
|
||
fmt.Printf("%s任务提交参数:%+v\n", getTaskTypeName(ts.taskType), submitTaskReq)
|
||
|
||
// 提交任务
|
||
resp, err := ts.httpClient.PostJSON("/jsm/v2/jobs/submit", submitTaskReq)
|
||
if err != nil {
|
||
fmt.Printf("Submit %s task failed: %v\n", getTaskTypeName(ts.taskType), err)
|
||
return "", nil, err
|
||
}
|
||
|
||
fmt.Printf("提交%s任务结果:%s\n", getTaskTypeName(ts.taskType), string(resp))
|
||
|
||
// 解析响应
|
||
var submitTaskResp models.SubmitTaskResponse
|
||
if err := json.Unmarshal(resp, &submitTaskResp); err != nil {
|
||
fmt.Printf("Submit %s task response unmarshal failed: %v\n", getTaskTypeName(ts.taskType), err)
|
||
return "", nil, err
|
||
}
|
||
|
||
if submitTaskResp.Code != models.ResponseOK {
|
||
fmt.Printf("Submit %s task failed: %s\n", getTaskTypeName(ts.taskType), submitTaskResp.Code)
|
||
return "", nil, fmt.Errorf("submit %s task failed: %s", getTaskTypeName(ts.taskType), submitTaskResp.Code)
|
||
}
|
||
|
||
fmt.Printf("Submit %s task result: %s\n", getTaskTypeName(ts.taskType), string(resp))
|
||
return submitTaskResp.Data.JobSetID, &submitTaskReq, nil
|
||
}
|
||
|
||
// getTaskTypeName 获取任务类型的中文名称
|
||
func getTaskTypeName(taskType string) string {
|
||
switch taskType {
|
||
case TrainingTaskType:
|
||
return "训练"
|
||
case InferenceTaskType:
|
||
return "推理"
|
||
default:
|
||
return "未知"
|
||
}
|
||
}
|