feat: change history data field

This commit is contained in:
Simon Ding
2024-07-15 10:56:05 +08:00
parent f37a8eeb5f
commit b2c4b69d87
15 changed files with 178 additions and 142 deletions

View File

@@ -11,7 +11,6 @@ import (
type Activity struct {
*ent.History
InBackgroud bool `json:"in_backgroud"`
Progress int `json:"progress"`
}
@@ -26,12 +25,6 @@ func (s *Server) GetAllActivities(c *gin.Context) (interface{}, error) {
if h.ID == id && task.Exists() {
a.Progress = task.Progress()
}
if h.ID == id && task.Processing {
a.InBackgroud = true
}
}
if a.Completed {
a.Progress = 100
}
activities = append(activities, a)
}

View File

@@ -4,6 +4,7 @@ import (
"fmt"
"polaris/db"
"polaris/ent"
"polaris/ent/history"
"polaris/log"
"polaris/pkg/torznab"
"polaris/pkg/transmission"
@@ -119,7 +120,7 @@ func (s *Server) searchAndDownload(seriesId, seasonNum, episodeNum int) (*string
EpisodeID: ep.ID,
SourceTitle: r1.Name,
TargetDir: dir,
Completed: false,
Status: history.StatusRunning,
Saved: torrent.Save(),
})
if err != nil {

View File

@@ -3,6 +3,7 @@ package server
import (
"path/filepath"
"polaris/ent"
"polaris/ent/history"
storage1 "polaris/ent/storage"
"polaris/log"
"polaris/pkg"
@@ -34,14 +35,9 @@ func (s *Server) checkTasks() {
log.Infof("task no longer exists: %v", id)
continue
}
if t.Processing {
continue
}
log.Infof("task (%s) percentage done: %d%%", t.Name(), t.Progress())
if t.Progress() == 100 {
log.Infof("task is done: %v", t.Name())
t.Processing = true
go func() {
if err := s.moveCompletedTask(id); err != nil {
log.Infof("post tasks for id %v fail: %v", id, err)
@@ -51,9 +47,22 @@ func (s *Server) checkTasks() {
}
}
func (s *Server) moveCompletedTask(id int) error {
func (s *Server) moveCompletedTask(id int) (err error) {
torrent := s.tasks[id]
r := s.db.GetHistory(id)
if r.Status == history.StatusUploading {
log.Infof("task %d is laready uploading, skip", id)
return nil
}
s.db.SetHistoryStatus(r.ID, history.StatusUploading)
defer func () {
if err != nil {
s.db.SetHistoryStatus(r.ID, history.StatusFail)
} else {
s.db.SetHistoryStatus(r.ID, history.StatusSuccess)
}
}()
series := s.db.GetSeriesDetails(r.SeriesID)
if series == nil {
@@ -86,7 +95,7 @@ func (s *Server) moveCompletedTask(id int) error {
log.Infof("move downloaded files to target dir success, file: %v, target dir: %v", torrent.Name(), r.TargetDir)
torrent.Remove()
delete(s.tasks, r.ID)
s.db.SetHistoryComplete(r.ID)
s.db.SetHistoryStatus(r.ID, history.StatusSuccess)
return nil
}
@@ -168,6 +177,6 @@ func (s *Server) checkFileExists(series *ent.Series) error {
}
type Task struct {
Processing bool
//Processing bool
pkg.Torrent
}