You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

resty.go 21 kB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730
  1. package modelarts
  2. import (
  3. "code.gitea.io/gitea/models"
  4. "code.gitea.io/gitea/modules/log"
  5. "code.gitea.io/gitea/modules/setting"
  6. "crypto/tls"
  7. "encoding/json"
  8. "fmt"
  9. "github.com/go-resty/resty/v2"
  10. "net/http"
  11. "strconv"
  12. )
  13. var (
  14. restyClient *resty.Client
  15. HOST string
  16. TOKEN string
  17. )
  18. const (
  19. methodPassword = "password"
  20. urlGetToken = "/v3/auth/tokens"
  21. urlNotebook = "/demanager/instances"
  22. urlTrainJob = "/training-jobs"
  23. urlResourceSpecs = "/job/resource-specs"
  24. urlTrainJobConfig = "/training-job-configs"
  25. errorCodeExceedLimit = "ModelArts.0118"
  26. )
  27. func getRestyClient() *resty.Client {
  28. if restyClient == nil {
  29. restyClient = resty.New()
  30. restyClient.SetTLSClientConfig(&tls.Config{InsecureSkipVerify: true})
  31. }
  32. return restyClient
  33. }
  34. func checkSetting() {
  35. if len(HOST) != 0 && len(TOKEN) != 0 && restyClient != nil {
  36. return
  37. }
  38. err := getToken()
  39. if err != nil {
  40. log.Error("getToken failed:%v", err)
  41. }
  42. }
  43. func getToken() error {
  44. HOST = setting.ModelArtsHost
  45. client := getRestyClient()
  46. params := models.GetTokenParams{
  47. Auth: models.Auth{
  48. Identity: models.Identity{
  49. Methods: []string{methodPassword},
  50. Password: models.Password{
  51. User: models.NotebookUser{
  52. Name: setting.ModelArtsUsername,
  53. Password: setting.ModelArtsPassword,
  54. Domain: models.Domain{
  55. Name: setting.ModelArtsDomain,
  56. },
  57. },
  58. },
  59. },
  60. Scope: models.Scope{
  61. Project: models.Project{
  62. Name: setting.ProjectName,
  63. },
  64. },
  65. },
  66. }
  67. res, err := client.R().
  68. SetHeader("Content-Type", "application/json").
  69. SetBody(params).
  70. Post(setting.IamHost + urlGetToken)
  71. if err != nil {
  72. return fmt.Errorf("resty getToken: %v", err)
  73. }
  74. if res.StatusCode() != http.StatusCreated {
  75. return fmt.Errorf("getToken failed:%s", res.String())
  76. }
  77. TOKEN = res.Header().Get("X-Subject-Token")
  78. return nil
  79. }
  80. func createNotebook(createJobParams models.CreateNotebookParams) (*models.CreateNotebookResult, error) {
  81. checkSetting()
  82. client := getRestyClient()
  83. var result models.CreateNotebookResult
  84. retry := 0
  85. sendjob:
  86. res, err := client.R().
  87. SetHeader("Content-Type", "application/json").
  88. SetAuthToken(TOKEN).
  89. SetBody(createJobParams).
  90. SetResult(&result).
  91. Post(HOST + "/v1/" + setting.ProjectID + urlNotebook)
  92. if err != nil {
  93. return nil, fmt.Errorf("resty create notebook: %s", err)
  94. }
  95. if res.StatusCode() == http.StatusUnauthorized && retry < 1 {
  96. retry++
  97. _ = getToken()
  98. goto sendjob
  99. }
  100. var response models.NotebookResult
  101. err = json.Unmarshal(res.Body(), &response)
  102. if err != nil {
  103. log.Error("json.Unmarshal failed: %s", err.Error())
  104. return &result, fmt.Errorf("son.Unmarshal failed: %s", err.Error())
  105. }
  106. if len(response.ErrorCode) != 0 {
  107. log.Error("createNotebook failed(%s): %s", response.ErrorCode, response.ErrorMsg)
  108. if response.ErrorCode == errorCodeExceedLimit {
  109. response.ErrorMsg = "所选规格使用数量已超过最大配额限制。"
  110. }
  111. return &result, fmt.Errorf("createNotebook failed(%s): %s", response.ErrorCode, response.ErrorMsg)
  112. }
  113. return &result, nil
  114. }
  115. func GetJob(jobID string) (*models.GetNotebookResult, error) {
  116. checkSetting()
  117. client := getRestyClient()
  118. var result models.GetNotebookResult
  119. retry := 0
  120. sendjob:
  121. res, err := client.R().
  122. SetHeader("Content-Type", "application/json").
  123. SetAuthToken(TOKEN).
  124. SetResult(&result).
  125. Get(HOST + "/v1/" + setting.ProjectID + urlNotebook + "/" + jobID)
  126. if err != nil {
  127. return nil, fmt.Errorf("resty GetJob: %v", err)
  128. }
  129. if res.StatusCode() == http.StatusUnauthorized && retry < 1 {
  130. retry++
  131. _ = getToken()
  132. goto sendjob
  133. }
  134. var response models.NotebookResult
  135. err = json.Unmarshal(res.Body(), &response)
  136. if err != nil {
  137. log.Error("json.Unmarshal failed: %s", err.Error())
  138. return &result, fmt.Errorf("son.Unmarshal failed: %s", err.Error())
  139. }
  140. if len(response.ErrorCode) != 0 {
  141. log.Error("GetJob failed(%s): %s", response.ErrorCode, response.ErrorMsg)
  142. return &result, fmt.Errorf("GetJob failed(%s): %s", response.ErrorCode, response.ErrorMsg)
  143. }
  144. return &result, nil
  145. }
  146. func StopJob(jobID string, param models.NotebookAction) (*models.NotebookActionResult, error) {
  147. checkSetting()
  148. client := getRestyClient()
  149. var result models.NotebookActionResult
  150. retry := 0
  151. sendjob:
  152. res, err := client.R().
  153. SetHeader("Content-Type", "application/json").
  154. SetBody(param).
  155. SetAuthToken(TOKEN).
  156. SetResult(&result).
  157. Post(HOST + "/v1/" + setting.ProjectID + urlNotebook + "/" + jobID + "/action")
  158. if err != nil {
  159. return &result, fmt.Errorf("resty StopJob: %v", err)
  160. }
  161. if res.StatusCode() == http.StatusUnauthorized && retry < 1 {
  162. retry++
  163. _ = getToken()
  164. goto sendjob
  165. }
  166. var response models.NotebookResult
  167. err = json.Unmarshal(res.Body(), &response)
  168. if err != nil {
  169. log.Error("json.Unmarshal failed: %s", err.Error())
  170. return &result, fmt.Errorf("son.Unmarshal failed: %s", err.Error())
  171. }
  172. if len(response.ErrorCode) != 0 {
  173. log.Error("StopJob failed(%s): %s", response.ErrorCode, response.ErrorMsg)
  174. return &result, fmt.Errorf("StopJob failed(%s): %s", response.ErrorCode, response.ErrorMsg)
  175. }
  176. return &result, nil
  177. }
  178. func DelNotebook(jobID string) (*models.NotebookDelResult, error) {
  179. checkSetting()
  180. client := getRestyClient()
  181. var result models.NotebookDelResult
  182. retry := 0
  183. sendjob:
  184. res, err := client.R().
  185. SetHeader("Content-Type", "application/json").
  186. SetAuthToken(TOKEN).
  187. SetResult(&result).
  188. Delete(HOST + "/v1/" + setting.ProjectID + urlNotebook + "/" + jobID)
  189. if err != nil {
  190. return &result, fmt.Errorf("resty DelJob: %v", err)
  191. }
  192. if res.StatusCode() == http.StatusUnauthorized && retry < 1 {
  193. retry++
  194. _ = getToken()
  195. goto sendjob
  196. }
  197. var response models.NotebookResult
  198. err = json.Unmarshal(res.Body(), &response)
  199. if err != nil {
  200. log.Error("json.Unmarshal failed: %s", err.Error())
  201. return &result, fmt.Errorf("son.Unmarshal failed: %s", err.Error())
  202. }
  203. if len(response.ErrorCode) != 0 {
  204. log.Error("DelJob failed(%s): %s", response.ErrorCode, response.ErrorMsg)
  205. return &result, fmt.Errorf("DelJob failed(%s): %s", response.ErrorCode, response.ErrorMsg)
  206. }
  207. return &result, nil
  208. }
  209. func GetJobToken(jobID string) (*models.NotebookGetJobTokenResult, error) {
  210. checkSetting()
  211. client := getRestyClient()
  212. var result models.NotebookGetJobTokenResult
  213. retry := 0
  214. sendjob:
  215. res, err := client.R().
  216. SetHeader("Content-Type", "application/json").
  217. SetAuthToken(TOKEN).
  218. SetResult(&result).
  219. Get(HOST + "/v1/" + setting.ProjectID + urlNotebook + "/" + jobID + "/token")
  220. if err != nil {
  221. return &result, fmt.Errorf("resty GetJobToken: %v", err)
  222. }
  223. if res.StatusCode() == http.StatusUnauthorized && retry < 1 {
  224. retry++
  225. _ = getToken()
  226. goto sendjob
  227. }
  228. var response models.NotebookResult
  229. err = json.Unmarshal(res.Body(), &response)
  230. if err != nil {
  231. log.Error("json.Unmarshal failed: %s", err.Error())
  232. return &result, fmt.Errorf("son.Unmarshal failed: %s", err.Error())
  233. }
  234. if len(response.ErrorCode) != 0 {
  235. log.Error("GetJobToken failed(%s): %s", response.ErrorCode, response.ErrorMsg)
  236. return &result, fmt.Errorf("GetJobToken failed(%s): %s", response.ErrorCode, response.ErrorMsg)
  237. }
  238. return &result, nil
  239. }
  240. func createTrainJob(createJobParams models.CreateTrainJobParams) (*models.CreateTrainJobResult, error) {
  241. checkSetting()
  242. client := getRestyClient()
  243. var result models.CreateTrainJobResult
  244. retry := 0
  245. sendjob:
  246. res, err := client.R().
  247. SetHeader("Content-Type", "application/json").
  248. SetAuthToken(TOKEN).
  249. SetBody(createJobParams).
  250. SetResult(&result).
  251. Post(HOST + "/v1/" + setting.ProjectID + urlTrainJob)
  252. if err != nil {
  253. return nil, fmt.Errorf("resty create train-job: %s", err)
  254. }
  255. req, _ := json.Marshal(createJobParams)
  256. log.Info("%s", req)
  257. if res.StatusCode() == http.StatusUnauthorized && retry < 1 {
  258. retry++
  259. _ = getToken()
  260. goto sendjob
  261. }
  262. if res.StatusCode() != http.StatusOK {
  263. var temp models.ErrorResult
  264. if err = json.Unmarshal([]byte(res.String()), &temp); err != nil {
  265. log.Error("json.Unmarshal failed(%s): %v", res.String(), err.Error())
  266. return &result, fmt.Errorf("json.Unmarshal failed(%s): %v", res.String(), err.Error())
  267. }
  268. log.Error("createTrainJob failed(%d):%s(%s)", res.StatusCode(), temp.ErrorCode, temp.ErrorMsg)
  269. return &result, fmt.Errorf("createTrainJob failed(%d):%s(%s)", res.StatusCode(), temp.ErrorCode, temp.ErrorMsg)
  270. }
  271. if !result.IsSuccess {
  272. log.Error("createTrainJob failed(%s): %s", result.ErrorCode, result.ErrorMsg)
  273. return &result, fmt.Errorf("createTrainJob failed(%s): %s", result.ErrorCode, result.ErrorMsg)
  274. }
  275. return &result, nil
  276. }
  277. func GetResourceSpecs() (*models.GetResourceSpecsResult, error) {
  278. checkSetting()
  279. client := getRestyClient()
  280. var result models.GetResourceSpecsResult
  281. retry := 0
  282. sendjob:
  283. res, err := client.R().
  284. SetHeader("Content-Type", "application/json").
  285. SetAuthToken(TOKEN).
  286. SetResult(&result).
  287. Get(HOST + "/v1/" + setting.ProjectID + urlResourceSpecs)
  288. if err != nil {
  289. return nil, fmt.Errorf("resty GetResourceSpecs: %v", err)
  290. }
  291. if res.StatusCode() == http.StatusUnauthorized && retry < 1 {
  292. retry++
  293. _ = getToken()
  294. goto sendjob
  295. }
  296. if res.StatusCode() != http.StatusOK {
  297. var temp models.ErrorResult
  298. if err = json.Unmarshal([]byte(res.String()), &temp); err != nil {
  299. log.Error("json.Unmarshal failed(%s): %v", res.String(), err.Error())
  300. return &result, fmt.Errorf("json.Unmarshal failed(%s): %v", res.String(), err.Error())
  301. }
  302. log.Error("GetResourceSpecs failed(%d):%s(%s)", res.StatusCode(), temp.ErrorCode, temp.ErrorMsg)
  303. return &result, fmt.Errorf("GetResourceSpecs failed(%d):%s(%s)", res.StatusCode(), temp.ErrorCode, temp.ErrorMsg)
  304. }
  305. if !result.IsSuccess {
  306. log.Error("GetResourceSpecs failed(%s): %s", result.ErrorCode, result.ErrorMsg)
  307. return &result, fmt.Errorf("GetResourceSpecs failed(%s): %s", result.ErrorCode, result.ErrorMsg)
  308. }
  309. return &result, nil
  310. }
  311. func CreateTrainJobConfig(req models.CreateConfigParams) (*models.CreateTrainJobConfigResult, error) {
  312. checkSetting()
  313. client := getRestyClient()
  314. var result models.CreateTrainJobConfigResult
  315. retry := 0
  316. sendjob:
  317. res, err := client.R().
  318. SetHeader("Content-Type", "application/json").
  319. SetAuthToken(TOKEN).
  320. SetBody(req).
  321. SetResult(&result).
  322. Post(HOST + "/v1/" + setting.ProjectID + urlTrainJobConfig)
  323. if err != nil {
  324. return nil, fmt.Errorf("resty CreateTrainJobConfig: %s", err)
  325. }
  326. if res.StatusCode() == http.StatusUnauthorized && retry < 1 {
  327. retry++
  328. _ = getToken()
  329. goto sendjob
  330. }
  331. //temp, _ := json.Marshal(req)
  332. //log.Info("%s", temp)
  333. if res.StatusCode() != http.StatusOK {
  334. var temp models.ErrorResult
  335. if err = json.Unmarshal([]byte(res.String()), &temp); err != nil {
  336. log.Error("json.Unmarshal failed(%s): %v", res.String(), err.Error())
  337. return &result, fmt.Errorf("json.Unmarshal failed(%s): %v", res.String(), err.Error())
  338. }
  339. log.Error("CreateTrainJobConfig failed(%d):%s(%s)", res.StatusCode(), temp.ErrorCode, temp.ErrorMsg)
  340. return &result, fmt.Errorf("CreateTrainJobConfig failed(%d):%s(%s)", res.StatusCode(), temp.ErrorCode, temp.ErrorMsg)
  341. }
  342. if !result.IsSuccess {
  343. log.Error("CreateTrainJobConfig failed(%s): %s", result.ErrorCode, result.ErrorMsg)
  344. return &result, fmt.Errorf("CreateTrainJobConfig failed(%s): %s", result.ErrorCode, result.ErrorMsg)
  345. }
  346. return &result, nil
  347. }
  348. func GetConfigList(perPage, page int, sortBy, order, searchContent, configType string) (*models.GetConfigListResult, error) {
  349. checkSetting()
  350. client := getRestyClient()
  351. var result models.GetConfigListResult
  352. retry := 0
  353. sendjob:
  354. res, err := client.R().
  355. SetQueryParams(map[string]string{
  356. "per_page": strconv.Itoa(perPage),
  357. "page": strconv.Itoa(page),
  358. "sortBy": sortBy,
  359. "order": order,
  360. "search_content": searchContent,
  361. "config_type": configType,
  362. }).
  363. SetAuthToken(TOKEN).
  364. SetResult(&result).
  365. Get(HOST + "/v1/" + setting.ProjectID + urlTrainJobConfig)
  366. if err != nil {
  367. return nil, fmt.Errorf("resty GetConfigList: %v", err)
  368. }
  369. if res.StatusCode() == http.StatusUnauthorized && retry < 1 {
  370. retry++
  371. _ = getToken()
  372. goto sendjob
  373. }
  374. if res.StatusCode() != http.StatusOK {
  375. var temp models.ErrorResult
  376. if err = json.Unmarshal([]byte(res.String()), &temp); err != nil {
  377. log.Error("json.Unmarshal failed(%s): %v", res.String(), err.Error())
  378. return &result, fmt.Errorf("json.Unmarshal failed(%s): %v", res.String(), err.Error())
  379. }
  380. log.Error("GetConfigList failed(%d):%s(%s)", res.StatusCode(), temp.ErrorCode, temp.ErrorMsg)
  381. return &result, fmt.Errorf("获取参数配置列表失败(%d):%s(%s)", res.StatusCode(), temp.ErrorCode, temp.ErrorMsg)
  382. }
  383. if !result.IsSuccess {
  384. log.Error("GetConfigList failed(%s): %s", result.ErrorCode, result.ErrorMsg)
  385. return &result, fmt.Errorf("获取参数配置列表失败(%s): %s", result.ErrorCode, result.ErrorMsg)
  386. }
  387. return &result, nil
  388. }
  389. func GetParaConfig(configName, configType string) (models.GetConfigResult, error) {
  390. checkSetting()
  391. client := getRestyClient()
  392. var result models.GetConfigResult
  393. retry := 0
  394. sendjob:
  395. res, err := client.R().
  396. SetQueryParams(map[string]string{
  397. "config_type": configType,
  398. }).
  399. SetAuthToken(TOKEN).
  400. SetResult(&result).
  401. Get(HOST + "/v1/" + setting.ProjectID + urlTrainJobConfig + "/" + configName)
  402. if err != nil {
  403. return result, fmt.Errorf("resty GetParaConfig: %v", err)
  404. }
  405. if res.StatusCode() == http.StatusUnauthorized && retry < 1 {
  406. retry++
  407. _ = getToken()
  408. goto sendjob
  409. }
  410. if res.StatusCode() != http.StatusOK {
  411. var temp models.ErrorResult
  412. if err = json.Unmarshal([]byte(res.String()), &temp); err != nil {
  413. log.Error("json.Unmarshal failed(%s): %v", res.String(), err.Error())
  414. return result, fmt.Errorf("json.Unmarshal failed(%s): %v", res.String(), err.Error())
  415. }
  416. log.Error("GetParaConfig failed(%d):%s(%s)", res.StatusCode(), temp.ErrorCode, temp.ErrorMsg)
  417. return result, fmt.Errorf("获取参数配置详情失败(%d):%s(%s)", res.StatusCode(), temp.ErrorCode, temp.ErrorMsg)
  418. }
  419. if !result.IsSuccess {
  420. log.Error("GetParaConfig failed(%s): %s", result.ErrorCode, result.ErrorMsg)
  421. return result, fmt.Errorf("获取参数配置详情失败(%s): %s", result.ErrorCode, result.ErrorMsg)
  422. }
  423. return result, nil
  424. }
  425. func GetTrainJob(jobID, versionID string) (*models.GetTrainJobResult, error) {
  426. checkSetting()
  427. client := getRestyClient()
  428. var result models.GetTrainJobResult
  429. retry := 0
  430. sendjob:
  431. res, err := client.R().
  432. SetAuthToken(TOKEN).
  433. SetResult(&result).
  434. Get(HOST + "/v1/" + setting.ProjectID + urlTrainJob + "/" + jobID + "/versions/" + versionID)
  435. if err != nil {
  436. return nil, fmt.Errorf("resty GetTrainJob: %v", err)
  437. }
  438. if res.StatusCode() == http.StatusUnauthorized && retry < 1 {
  439. retry++
  440. _ = getToken()
  441. goto sendjob
  442. }
  443. if res.StatusCode() != http.StatusOK {
  444. var temp models.ErrorResult
  445. if err = json.Unmarshal([]byte(res.String()), &temp); err != nil {
  446. log.Error("json.Unmarshal failed(%s): %v", res.String(), err.Error())
  447. return &result, fmt.Errorf("json.Unmarshal failed(%s): %v", res.String(), err.Error())
  448. }
  449. log.Error("GetTrainJob failed(%d):%s(%s)", res.StatusCode(), temp.ErrorCode, temp.ErrorMsg)
  450. return &result, fmt.Errorf("获取作业详情失败(%d):%s(%s)", res.StatusCode(), temp.ErrorCode, temp.ErrorMsg)
  451. }
  452. if !result.IsSuccess {
  453. log.Error("GetTrainJob(%s) failed", jobID)
  454. return &result, fmt.Errorf("获取作业详情失败")
  455. }
  456. return &result, nil
  457. }
  458. func GetTrainJobLog(jobID, versionID, baseLine, logFile, order string, lines int) (*models.GetTrainJobLogResult, error) {
  459. checkSetting()
  460. client := getRestyClient()
  461. var result models.GetTrainJobLogResult
  462. retry := 0
  463. sendjob:
  464. res, err := client.R().
  465. SetQueryParams(map[string]string{
  466. "base_line": baseLine,
  467. "lines": strconv.Itoa(lines),
  468. "log_file": logFile,
  469. "order": order,
  470. }).
  471. SetAuthToken(TOKEN).
  472. SetResult(&result).
  473. Get(HOST + "/v1/" + setting.ProjectID + urlTrainJob + "/" + jobID + "/versions/" + versionID + "/aom-log")
  474. if err != nil {
  475. return nil, fmt.Errorf("resty GetTrainJobLog: %v", err)
  476. }
  477. if res.StatusCode() == http.StatusUnauthorized && retry < 1 {
  478. retry++
  479. _ = getToken()
  480. goto sendjob
  481. }
  482. if res.StatusCode() != http.StatusOK {
  483. var temp models.ErrorResult
  484. if err = json.Unmarshal([]byte(res.String()), &temp); err != nil {
  485. log.Error("json.Unmarshal failed(%s): %v", res.String(), err.Error())
  486. return &result, fmt.Errorf("json.Unmarshal failed(%s): %v", res.String(), err.Error())
  487. }
  488. log.Error("GetTrainJobLog failed(%d):%s(%s)", res.StatusCode(), temp.ErrorCode, temp.ErrorMsg)
  489. return &result, fmt.Errorf("获取作业日志失败(%d):%s(%s)", res.StatusCode(), temp.ErrorCode, temp.ErrorMsg)
  490. }
  491. if !result.IsSuccess {
  492. log.Error("GetTrainJobLog(%s) failed", jobID)
  493. return &result, fmt.Errorf("获取作业日志失败:%s", result.ErrorMsg)
  494. }
  495. return &result, nil
  496. }
  497. func GetTrainJobLogFileNames(jobID, versionID string) (*models.GetTrainJobLogFileNamesResult, error) {
  498. checkSetting()
  499. client := getRestyClient()
  500. var result models.GetTrainJobLogFileNamesResult
  501. retry := 0
  502. sendjob:
  503. res, err := client.R().
  504. SetAuthToken(TOKEN).
  505. SetResult(&result).
  506. Get(HOST + "/v1/" + setting.ProjectID + urlTrainJob + "/" + jobID + "/versions/" + versionID + "/log/file-names")
  507. if err != nil {
  508. return nil, fmt.Errorf("resty GetTrainJobLogFileNames: %v", err)
  509. }
  510. if res.StatusCode() == http.StatusUnauthorized && retry < 1 {
  511. retry++
  512. _ = getToken()
  513. goto sendjob
  514. }
  515. if res.StatusCode() != http.StatusOK {
  516. var temp models.ErrorResult
  517. if err = json.Unmarshal([]byte(res.String()), &temp); err != nil {
  518. log.Error("json.Unmarshal failed(%s): %v", res.String(), err.Error())
  519. return &result, fmt.Errorf("json.Unmarshal failed(%s): %v", res.String(), err.Error())
  520. }
  521. log.Error("GetTrainJobLogFileNames failed(%d):%s(%s)", res.StatusCode(), temp.ErrorCode, temp.ErrorMsg)
  522. return &result, fmt.Errorf("GetTrainJobLogFileNames failed(%d):%s(%s)", res.StatusCode(), temp.ErrorCode, temp.ErrorMsg)
  523. }
  524. if !result.IsSuccess {
  525. log.Error("GetTrainJobLogFileNames(%s) failed", jobID)
  526. return &result, fmt.Errorf("获取作业日志文件失败:%s", result.ErrorMsg)
  527. }
  528. return &result, nil
  529. }
  530. func DelTrainJob(jobID string) (*models.TrainJobResult, error) {
  531. checkSetting()
  532. client := getRestyClient()
  533. var result models.TrainJobResult
  534. retry := 0
  535. sendjob:
  536. res, err := client.R().
  537. SetAuthToken(TOKEN).
  538. SetResult(&result).
  539. Delete(HOST + "/v1/" + setting.ProjectID + urlTrainJob + "/" + jobID)
  540. if err != nil {
  541. return &result, fmt.Errorf("resty DelTrainJob: %v", err)
  542. }
  543. if res.StatusCode() == http.StatusUnauthorized && retry < 1 {
  544. retry++
  545. _ = getToken()
  546. goto sendjob
  547. }
  548. if res.StatusCode() != http.StatusOK {
  549. var temp models.ErrorResult
  550. if err = json.Unmarshal([]byte(res.String()), &temp); err != nil {
  551. log.Error("json.Unmarshal failed(%s): %v", res.String(), err.Error())
  552. return &result, fmt.Errorf("json.Unmarshal failed(%s): %v", res.String(), err.Error())
  553. }
  554. log.Error("DelTrainJob failed(%d):%s(%s)", res.StatusCode(), temp.ErrorCode, temp.ErrorMsg)
  555. return &result, fmt.Errorf("删除训练作业失败(%d):%s(%s)", res.StatusCode(), temp.ErrorCode, temp.ErrorMsg)
  556. }
  557. if !result.IsSuccess {
  558. log.Error("DelTrainJob(%s) failed", jobID)
  559. return &result, fmt.Errorf("删除训练作业失败:%s", result.ErrorMsg)
  560. }
  561. return &result, nil
  562. }
  563. func StopTrainJob(jobID, versionID string) (*models.TrainJobResult, error) {
  564. checkSetting()
  565. client := getRestyClient()
  566. var result models.TrainJobResult
  567. retry := 0
  568. sendjob:
  569. res, err := client.R().
  570. SetAuthToken(TOKEN).
  571. SetResult(&result).
  572. Post(HOST + "/v1/" + setting.ProjectID + urlTrainJob + "/" + jobID + "/versions/" + versionID + "/stop")
  573. if err != nil {
  574. return &result, fmt.Errorf("resty StopTrainJob: %v", err)
  575. }
  576. if res.StatusCode() == http.StatusUnauthorized && retry < 1 {
  577. retry++
  578. _ = getToken()
  579. goto sendjob
  580. }
  581. if res.StatusCode() != http.StatusOK {
  582. var temp models.ErrorResult
  583. if err = json.Unmarshal([]byte(res.String()), &temp); err != nil {
  584. log.Error("json.Unmarshal failed(%s): %v", res.String(), err.Error())
  585. return &result, fmt.Errorf("json.Unmarshal failed(%s): %v", res.String(), err.Error())
  586. }
  587. log.Error("StopTrainJob failed(%d):%s(%s)", res.StatusCode(), temp.ErrorCode, temp.ErrorMsg)
  588. return &result, fmt.Errorf("停止训练作业失败(%d):%s(%s)", res.StatusCode(), temp.ErrorCode, temp.ErrorMsg)
  589. }
  590. if !result.IsSuccess {
  591. log.Error("StopTrainJob(%s) failed", jobID)
  592. return &result, fmt.Errorf("停止训练作业失败:%s", result.ErrorMsg)
  593. }
  594. return &result, nil
  595. }