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 3.8 kB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
4 years ago
4 years ago
4 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. "net/http"
  8. "sort"
  9. "time"
  10. "code.gitea.io/gitea/modules/log"
  11. "code.gitea.io/gitea/models"
  12. "code.gitea.io/gitea/modules/cloudbrain"
  13. "code.gitea.io/gitea/modules/context"
  14. )
  15. // cloudbrain get job task by jobid
  16. func GetCloudbrainTask(ctx *context.APIContext) {
  17. // swagger:operation GET /repos/{owner}/{repo}/cloudbrain/{jobid} cloudbrain jobTask
  18. // ---
  19. // summary: Get a single task
  20. // produces:
  21. // - application/json
  22. // parameters:
  23. // - name: owner
  24. // in: path
  25. // description: owner of the repo
  26. // type: string
  27. // required: true
  28. // - name: repo
  29. // in: path
  30. // description: name of the repo
  31. // type: string
  32. // required: true
  33. // - name: jobid
  34. // in: path
  35. // description: id of cloudbrain jobid
  36. // type: string
  37. // required: true
  38. // responses:
  39. // "200":
  40. // "$ref": "#/responses/Label"
  41. var (
  42. err error
  43. )
  44. jobName := ctx.Params(":jobname")
  45. job, err := models.GetCloudbrainByName(jobName)
  46. if err != nil {
  47. ctx.Data["error"] = err.Error()
  48. }
  49. jobResult, err := cloudbrain.GetJob(job.JobID)
  50. if err != nil {
  51. ctx.NotFound(err)
  52. return
  53. }
  54. result, err := models.ConvertToJobResultPayload(jobResult.Payload)
  55. if err != nil {
  56. ctx.NotFound(err)
  57. return
  58. }
  59. job.Status = result.JobStatus.State
  60. if result.JobStatus.State != string(models.JobWaiting) && result.JobStatus.State != string(models.JobFailed) {
  61. taskRoles := result.TaskRoles
  62. taskRes, _ := models.ConvertToTaskPod(taskRoles[cloudbrain.SubTaskName].(map[string]interface{}))
  63. job.ContainerIp = taskRes.TaskStatuses[0].ContainerIP
  64. job.ContainerID = taskRes.TaskStatuses[0].ContainerID
  65. job.Status = taskRes.TaskStatuses[0].State
  66. }
  67. if result.JobStatus.State != string(models.JobWaiting) {
  68. err = models.UpdateJob(job)
  69. if err != nil {
  70. log.Error("UpdateJob failed:", err)
  71. }
  72. }
  73. ctx.JSON(http.StatusOK, map[string]interface{}{
  74. "JobName": result.Config.JobName,
  75. "JobStatus": result.JobStatus.State,
  76. "SubState": result.JobStatus.SubState,
  77. "CreatedTime": time.Unix(result.JobStatus.CreatedTime/1000, 0).Format("2006-01-02 15:04:05"),
  78. "CompletedTime": time.Unix(result.JobStatus.CompletedTime/1000, 0).Format("2006-01-02 15:04:05"),
  79. })
  80. }
  81. func CloudbrainGetLog(ctx *context.Context) {
  82. jobID := ctx.Params(":jobid")
  83. _, err := models.GetCloudbrainByJobID(jobID)
  84. if err != nil {
  85. log.Error("GetCloudbrainByJobID failed: %v", err, ctx.Data["MsgID"])
  86. ctx.ServerError(err.Error(), err)
  87. return
  88. }
  89. var hits []models.Hits
  90. result, err := cloudbrain.GetJobLog(jobID)
  91. if err != nil {
  92. log.Error("GetJobLog failed: %v", err, ctx.Data["MsgID"])
  93. ctx.ServerError(err.Error(), err)
  94. return
  95. }
  96. hits = result.Hits.Hits
  97. //if the size equal page_size, then take the scroll_id to get all log and delete the scroll_id(the num of scroll_id is limited)
  98. if len(result.Hits.Hits) >= cloudbrain.LogPageSize {
  99. for {
  100. resultNext, err := cloudbrain.GetJobAllLog(result.ScrollID)
  101. if err != nil {
  102. log.Error("GetJobAllLog failed: %v", err, ctx.Data["MsgID"])
  103. } else {
  104. for _, hit := range resultNext.Hits.Hits {
  105. hits = append(hits, hit)
  106. }
  107. }
  108. if len(resultNext.Hits.Hits) < cloudbrain.LogPageSize {
  109. log.Info("get all log already")
  110. break
  111. }
  112. }
  113. }
  114. cloudbrain.DeleteJobLogToken(result.ScrollID)
  115. sort.Slice(hits, func(i, j int) bool {
  116. return hits[i].Sort[0] < hits[j].Sort[0]
  117. })
  118. var content string
  119. for _, log := range hits {
  120. content += log.Source.Message + "\n"
  121. }
  122. ctx.JSON(http.StatusOK, map[string]interface{}{
  123. "JobID": jobID,
  124. "Content": content,
  125. })
  126. return
  127. }