jobs_things.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. package v1
  2. import (
  3. "bufio"
  4. "errors"
  5. "fmt"
  6. "github.com/allanpk716/ChineseSubFinder/internal/pkg/global_value"
  7. "github.com/allanpk716/ChineseSubFinder/internal/pkg/my_util"
  8. task_queue2 "github.com/allanpk716/ChineseSubFinder/internal/pkg/task_queue"
  9. "github.com/allanpk716/ChineseSubFinder/internal/types/backend"
  10. "github.com/allanpk716/ChineseSubFinder/internal/types/common"
  11. "github.com/allanpk716/ChineseSubFinder/internal/types/task_queue"
  12. "github.com/gin-gonic/gin"
  13. "io"
  14. "net/http"
  15. "os"
  16. "path/filepath"
  17. )
  18. func (cb ControllerBase) JobsListHandler(c *gin.Context) {
  19. var err error
  20. defer func() {
  21. // 统一的异常处理
  22. cb.ErrorProcess(c, "JobsListHandler", err)
  23. }()
  24. bok, allJobs, err := cb.cronHelper.DownloadQueue.GetAllJobs()
  25. if err != nil {
  26. return
  27. }
  28. if bok == false {
  29. c.JSON(http.StatusOK, backend.ReplyAllJobs{
  30. AllJobs: make([]task_queue.OneJob, 0),
  31. })
  32. return
  33. }
  34. c.JSON(http.StatusOK, backend.ReplyAllJobs{
  35. AllJobs: allJobs,
  36. })
  37. }
  38. func (cb ControllerBase) ChangeJobStatusHandler(c *gin.Context) {
  39. var err error
  40. defer func() {
  41. // 统一的异常处理
  42. cb.ErrorProcess(c, "JobsListHandler", err)
  43. }()
  44. desJobStatus := backend.ReqChangeJobStatus{}
  45. err = c.ShouldBindJSON(&desJobStatus)
  46. if err != nil {
  47. return
  48. }
  49. bok, nowOneJob := cb.cronHelper.DownloadQueue.GetOneJobByID(desJobStatus.Id)
  50. if bok == false {
  51. err = errors.New("GetOneJobByID failed, id=" + desJobStatus.Id)
  52. return
  53. }
  54. if bok == false {
  55. c.JSON(http.StatusOK, backend.ReplyCommon{Message: "job not found"})
  56. return
  57. }
  58. if desJobStatus.TaskPriority == "high" {
  59. // high
  60. nowOneJob.TaskPriority = task_queue2.HighTaskPriorityLevel
  61. } else if desJobStatus.TaskPriority == "mddile" {
  62. // middle
  63. nowOneJob.TaskPriority = task_queue2.DefaultTaskPriorityLevel
  64. } else {
  65. // low
  66. nowOneJob.TaskPriority = task_queue2.LowTaskPriorityLevel
  67. }
  68. // 默认只能把任务改变为这两种状态
  69. if desJobStatus.JobStatus == task_queue.Waiting || desJobStatus.JobStatus == task_queue.Ignore {
  70. nowOneJob.JobStatus = desJobStatus.JobStatus
  71. } else {
  72. nowOneJob.JobStatus = task_queue.Waiting
  73. }
  74. bok, err = cb.cronHelper.DownloadQueue.Update(nowOneJob)
  75. if err != nil {
  76. return
  77. }
  78. if bok == false {
  79. c.JSON(http.StatusOK, backend.ReplyCommon{Message: "update job status failed"})
  80. return
  81. }
  82. c.JSON(http.StatusOK, backend.ReplyCommon{Message: "ok"})
  83. }
  84. func (cb ControllerBase) JobLogHandler(c *gin.Context) {
  85. var err error
  86. defer func() {
  87. // 统一的异常处理
  88. cb.ErrorProcess(c, "JobLogHandler", err)
  89. }()
  90. reqJobLog := backend.ReqJobLog{}
  91. err = c.ShouldBindJSON(&reqJobLog)
  92. if err != nil {
  93. return
  94. }
  95. pathRoot := filepath.Join(global_value.ConfigRootDirFPath(), "Logs")
  96. fileFPath := filepath.Join(pathRoot, common.OnceLogPrefix+reqJobLog.Id+".log")
  97. if my_util.IsFile(fileFPath) == true {
  98. // 存在
  99. // 一行一行的读取文件
  100. var fi *os.File
  101. fi, err = os.Open(fileFPath)
  102. if err != nil {
  103. fmt.Printf("Error: %s\n", err)
  104. return
  105. }
  106. defer fi.Close()
  107. ReplyJobLog := backend.ReplyJobLog{}
  108. ReplyJobLog.OneLine = make([]string, 0)
  109. br := bufio.NewReader(fi)
  110. for {
  111. a, _, c := br.ReadLine()
  112. if c == io.EOF {
  113. break
  114. }
  115. ReplyJobLog.OneLine = append(ReplyJobLog.OneLine, string(a))
  116. }
  117. c.JSON(http.StatusOK, ReplyJobLog)
  118. return
  119. } else {
  120. // 不存在
  121. c.JSON(http.StatusOK, backend.ReplyCommon{Message: "job log not found"})
  122. return
  123. }
  124. }