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.

cloudbrain.go 2.3 kB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. // Copyright 2016 The Gogs Authors. All rights reserved.
  2. // Copyright 2018 The Gitea Authors. All rights reserved.
  3. // Use of this source code is governed by a MIT-style
  4. // license that can be found in the LICENSE file.
  5. package repo
  6. import (
  7. "code.gitea.io/gitea/modules/log"
  8. "net/http"
  9. "time"
  10. "code.gitea.io/gitea/models"
  11. "code.gitea.io/gitea/modules/cloudbrain"
  12. "code.gitea.io/gitea/modules/context"
  13. )
  14. // cloudbrain get job task by jobid
  15. func GetCloudbrainTask(ctx *context.APIContext) {
  16. // swagger:operation GET /repos/{owner}/{repo}/cloudbrain/{jobid} cloudbrain jobTask
  17. // ---
  18. // summary: Get a single task
  19. // produces:
  20. // - application/json
  21. // parameters:
  22. // - name: owner
  23. // in: path
  24. // description: owner of the repo
  25. // type: string
  26. // required: true
  27. // - name: repo
  28. // in: path
  29. // description: name of the repo
  30. // type: string
  31. // required: true
  32. // - name: jobid
  33. // in: path
  34. // description: id of cloudbrain jobid
  35. // type: string
  36. // required: true
  37. // responses:
  38. // "200":
  39. // "$ref": "#/responses/Label"
  40. var (
  41. err error
  42. )
  43. jobID := ctx.Params(":jobid")
  44. repoID := ctx.Repo.Repository.ID
  45. job, err := models.GetRepoCloudBrainByJobID(repoID, jobID)
  46. if err != nil {
  47. ctx.NotFound(err)
  48. return
  49. }
  50. jobResult, err := cloudbrain.GetJob(jobID)
  51. if err != nil {
  52. ctx.NotFound(err)
  53. return
  54. }
  55. result, err := models.ConvertToJobResultPayload(jobResult.Payload)
  56. if err != nil {
  57. ctx.NotFound(err)
  58. return
  59. }
  60. taskRoles := result.TaskRoles
  61. taskRes, _ := models.ConvertToTaskPod(taskRoles[cloudbrain.SubTaskName].(map[string]interface{}))
  62. job.ContainerIp = taskRes.TaskStatuses[0].ContainerIP
  63. job.ContainerID = taskRes.TaskStatuses[0].ContainerID
  64. job.Status = taskRes.TaskStatuses[0].State
  65. if result.JobStatus.State != string(models.JobWaiting) {
  66. err = models.UpdateJob(job)
  67. if err != nil {
  68. log.Error("UpdateJob failed:", err)
  69. }
  70. }
  71. ctx.JSON(http.StatusOK, map[string]interface{}{
  72. "JobID": result.Config.JobID,
  73. "JobStatus": result.JobStatus.State,
  74. "SubState": result.JobStatus.SubState,
  75. "CreatedTime": time.Unix(result.JobStatus.CreatedTime/1000, 0).Format("2006-01-02 15:04:05"),
  76. "CompletedTime": time.Unix(result.JobStatus.CompletedTime/1000, 0).Format("2006-01-02 15:04:05"),
  77. })
  78. }