jobs_things.go 3.2 KB

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