| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142 |
- package v1
- import (
- "bufio"
- "errors"
- "fmt"
- "github.com/allanpk716/ChineseSubFinder/internal/pkg/global_value"
- "github.com/allanpk716/ChineseSubFinder/internal/pkg/my_util"
- task_queue2 "github.com/allanpk716/ChineseSubFinder/internal/pkg/task_queue"
- "github.com/allanpk716/ChineseSubFinder/internal/types/backend"
- "github.com/allanpk716/ChineseSubFinder/internal/types/common"
- "github.com/allanpk716/ChineseSubFinder/internal/types/task_queue"
- "github.com/gin-gonic/gin"
- "io"
- "net/http"
- "os"
- "path/filepath"
- )
- func (cb ControllerBase) JobsListHandler(c *gin.Context) {
- var err error
- defer func() {
- // 统一的异常处理
- cb.ErrorProcess(c, "JobsListHandler", err)
- }()
- bok, allJobs, err := cb.cronHelper.DownloadQueue.GetAllJobs()
- if err != nil {
- return
- }
- if bok == false {
- c.JSON(http.StatusOK, backend.ReplyAllJobs{
- AllJobs: make([]task_queue.OneJob, 0),
- })
- return
- }
- c.JSON(http.StatusOK, backend.ReplyAllJobs{
- AllJobs: allJobs,
- })
- }
- func (cb ControllerBase) ChangeJobStatusHandler(c *gin.Context) {
- var err error
- defer func() {
- // 统一的异常处理
- cb.ErrorProcess(c, "JobsListHandler", err)
- }()
- desJobStatus := backend.ReqChangeJobStatus{}
- err = c.ShouldBindJSON(&desJobStatus)
- if err != nil {
- return
- }
- bok, nowOneJob := cb.cronHelper.DownloadQueue.GetOneJobByID(desJobStatus.Id)
- if bok == false {
- err = errors.New("GetOneJobByID failed, id=" + desJobStatus.Id)
- return
- }
- if bok == false {
- c.JSON(http.StatusOK, backend.ReplyCommon{Message: "job not found"})
- return
- }
- if desJobStatus.TaskPriority == "high" {
- // high
- nowOneJob.TaskPriority = task_queue2.HighTaskPriorityLevel
- } else if desJobStatus.TaskPriority == "mddile" {
- // middle
- nowOneJob.TaskPriority = task_queue2.DefaultTaskPriorityLevel
- } else {
- // low
- nowOneJob.TaskPriority = task_queue2.LowTaskPriorityLevel
- }
- // 默认只能把任务改变为这两种状态
- if desJobStatus.JobStatus == task_queue.Waiting || desJobStatus.JobStatus == task_queue.Ignore {
- nowOneJob.JobStatus = desJobStatus.JobStatus
- } else {
- nowOneJob.JobStatus = task_queue.Waiting
- }
- bok, err = cb.cronHelper.DownloadQueue.Update(nowOneJob)
- if err != nil {
- return
- }
- if bok == false {
- c.JSON(http.StatusOK, backend.ReplyCommon{Message: "update job status failed"})
- return
- }
- c.JSON(http.StatusOK, backend.ReplyCommon{Message: "ok"})
- }
- func (cb ControllerBase) JobLogHandler(c *gin.Context) {
- var err error
- defer func() {
- // 统一的异常处理
- cb.ErrorProcess(c, "JobLogHandler", err)
- }()
- reqJobLog := backend.ReqJobLog{}
- err = c.ShouldBindJSON(&reqJobLog)
- if err != nil {
- return
- }
- pathRoot := filepath.Join(global_value.ConfigRootDirFPath(), "Logs")
- fileFPath := filepath.Join(pathRoot, common.OnceLogPrefix+reqJobLog.Id+".log")
- if my_util.IsFile(fileFPath) == true {
- // 存在
- // 一行一行的读取文件
- var fi *os.File
- fi, err = os.Open(fileFPath)
- if err != nil {
- fmt.Printf("Error: %s\n", err)
- return
- }
- defer fi.Close()
- ReplyJobLog := backend.ReplyJobLog{}
- ReplyJobLog.OneLine = make([]string, 0)
- br := bufio.NewReader(fi)
- for {
- a, _, c := br.ReadLine()
- if c == io.EOF {
- break
- }
- ReplyJobLog.OneLine = append(ReplyJobLog.OneLine, string(a))
- }
- c.JSON(http.StatusOK, ReplyJobLog)
- return
- } else {
- // 不存在
- c.JSON(http.StatusOK, backend.ReplyCommon{Message: "job log not found"})
- return
- }
- }
|