jobs_things.go 3.2 KB

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