refactor: change method to restore tasks for better maintains

This commit is contained in:
Simon Ding
2024-10-10 09:57:17 +08:00
parent e0d0ab80b6
commit 1728690860
9 changed files with 107 additions and 104 deletions

View File

@@ -483,7 +483,7 @@ func (c *Client) SaveHistoryRecord(h ent.History) (*ent.History, error) {
}
return c.ent.History.Create().SetMediaID(h.MediaID).SetEpisodeID(h.EpisodeID).SetDate(time.Now()).
SetStatus(h.Status).SetTargetDir(h.TargetDir).SetSourceTitle(h.SourceTitle).SetIndexerID(h.IndexerID).
SetDownloadClientID(h.DownloadClientID).SetSize(h.Size).SetSaved(h.Saved).Save(context.TODO())
SetDownloadClientID(h.DownloadClientID).SetSize(h.Size).SetSaved(h.Saved).SetLink(h.Link).Save(context.TODO())
}
func (c *Client) SetHistoryStatus(id int, status history.Status) error {

View File

@@ -37,7 +37,7 @@ type History struct {
Link string `json:"link,omitempty"`
// Status holds the value of the "status" field.
Status history.Status `json:"status,omitempty"`
// Saved holds the value of the "saved" field.
// deprecated
Saved string `json:"saved,omitempty"`
selectValues sql.SelectValues
}

View File

@@ -23,7 +23,7 @@ func (History) Fields() []ent.Field {
field.Int("indexer_id").Optional(),
field.String("link").Optional(), //should be magnet link
field.Enum("status").Values("running", "success", "fail", "uploading", "seeding"),
field.String("saved").Optional(),
field.String("saved").Optional().Comment("deprecated"), //deprecated
}
}

View File

@@ -6,11 +6,11 @@ type Torrent interface {
Stop() error
Start() error
Remove() error
Save() string
//Save() string
Exists() bool
SeedRatio() (float64, error)
GetHash() string
Reload() error
//Reload() error
}
type Downloader interface {

View File

@@ -47,8 +47,8 @@ func (c *Client) GetAll() ([]pkg.Torrent, error) {
for _, t := range tt {
t1 := &Torrent{
c: c.c,
Hash: t.Hash,
Info: c.Info,
hash: t.Hash,
//Info: c.Info,
}
res = append(res, t1)
}
@@ -64,27 +64,46 @@ func (c *Client) Download(link, dir string) (pkg.Torrent, error) {
if err != nil {
return nil, errors.Wrap(err, "qbt download")
}
return &Torrent{Hash: hash, c: c.c, Info: c.Info}, nil
return &Torrent{hash: hash, c: c.c, }, nil
}
func NewTorrent(info Info, magnet string) (*Torrent, error) {
c, err := NewClient(info.URL, info.User, info.Password)
if err != nil {
return nil, err
}
hash, err := utils.MagnetHash(magnet)
if err != nil {
return nil, err
}
t := &Torrent{
c: c.c,
hash: hash,
}
if !t.Exists() {
return nil, errors.Errorf("torrent not exist: %v", magnet)
}
return t, nil
}
type Torrent struct {
c *qbt.Client
Hash string
Info
hash string
//info Info
}
func (t *Torrent) GetHash() string {
return t.Hash
return t.hash
}
func (t *Torrent) getTorrent() (*qbt.TorrentInfo, error) {
all, err := t.c.Torrents(qbt.TorrentsOptions{Hashes: []string{t.Hash}})
all, err := t.c.Torrents(qbt.TorrentsOptions{Hashes: []string{t.hash}})
if err != nil {
return nil, err
}
if len(all) == 0 {
return nil, fmt.Errorf("no such torrent: %v", t.Hash)
return nil, fmt.Errorf("no such torrent: %v", t.hash)
}
return &all[0], nil
}
@@ -115,11 +134,11 @@ func (t *Torrent) Progress() (int, error) {
}
func (t *Torrent) Stop() error {
return t.c.Pause([]string{t.Hash})
return t.c.Pause([]string{t.hash})
}
func (t *Torrent) Start() error {
ok, err := t.c.Resume([]string{t.Hash})
ok, err := t.c.Resume([]string{t.hash})
if err != nil {
return err
}
@@ -130,7 +149,7 @@ func (t *Torrent) Start() error {
}
func (t *Torrent) Remove() error {
ok, err := t.c.Delete([]string{t.Hash}, true)
ok, err := t.c.Delete([]string{t.hash}, true)
if err != nil {
return err
}
@@ -157,15 +176,3 @@ func (t *Torrent) SeedRatio() (float64, error) {
}
return qb.Ratio, nil
}
func (t *Torrent) Reload() error {
c, err := NewClient(t.URL, t.User, t.Password)
if err != nil {
return err
}
t.c = c.c
if !t.Exists() {
return errors.Errorf("torrent not exists: %v", t.Hash)
}
return nil
}

View File

@@ -2,7 +2,6 @@ package transmission
import (
"context"
"encoding/json"
"fmt"
"net/url"
"polaris/log"
@@ -53,9 +52,9 @@ func (c *Client) GetAll() ([]pkg.Torrent, error) {
var torrents []pkg.Torrent
for _, t := range all {
torrents = append(torrents, &Torrent{
Hash: *t.HashString,
c: c.c,
Config: c.cfg,
hash: *t.HashString,
c: c.c,
//cfg: c.cfg,
})
}
return torrents, nil
@@ -74,33 +73,42 @@ func (c *Client) Download(link, dir string) (pkg.Torrent, error) {
log.Debugf("get torrent info: %+v", t)
return &Torrent{
Hash: hash,
c: c.c,
Config: c.cfg,
hash: hash,
c: c.c,
//cfg: c.cfg,
}, err
}
func NewTorrent(cfg Config, magnet string) (*Torrent, error) {
c, err := NewClient(cfg)
if err != nil {
return nil, err
}
hash, err := utils.MagnetHash(magnet)
if err != nil {
return nil, err
}
t := &Torrent{
c: c.c,
hash: hash,
//cfg: cfg,
}
if !t.Exists() {
return nil, errors.Errorf("torrent not exist: %v", magnet)
}
return t, nil
}
type Torrent struct {
//t *transmissionrpc.Torrent
c *transmissionrpc.Client
Hash string `json:"hash"`
Config
}
func (t *Torrent) Reload() error {
c, err := NewClient(t.Config)
if err != nil {
return err
}
t.c = c.c
if !t.Exists() {
return errors.Errorf("torrent not exists: %v", t.Hash)
}
return nil
hash string `json:"hash"`
//cfg Config
}
func (t *Torrent) getTorrent() (transmissionrpc.Torrent, error) {
r, err := t.c.TorrentGetAllForHashes(context.TODO(), []string{t.Hash})
r, err := t.c.TorrentGetAllForHashes(context.TODO(), []string{t.hash})
if err != nil {
log.Errorf("get torrent info for error: %v", err)
}
@@ -111,7 +119,7 @@ func (t *Torrent) getTorrent() (transmissionrpc.Torrent, error) {
}
func (t *Torrent) Exists() bool {
r, err := t.c.TorrentGetAllForHashes(context.TODO(), []string{t.Hash})
r, err := t.c.TorrentGetAllForHashes(context.TODO(), []string{t.hash})
if err != nil {
log.Errorf("get torrent info for error: %v", err)
}
@@ -149,7 +157,7 @@ func (t *Torrent) Progress() (int, error) {
}
func (t *Torrent) Stop() error {
return t.c.TorrentStopHashes(context.TODO(), []string{t.Hash})
return t.c.TorrentStopHashes(context.TODO(), []string{t.hash})
}
func (t *Torrent) SeedRatio() (float64, error) {
@@ -164,7 +172,7 @@ func (t *Torrent) SeedRatio() (float64, error) {
}
func (t *Torrent) Start() error {
return t.c.TorrentStartHashes(context.TODO(), []string{t.Hash})
return t.c.TorrentStartHashes(context.TODO(), []string{t.hash})
}
func (t *Torrent) Remove() error {
@@ -186,12 +194,6 @@ func (t *Torrent) Size() (int, error) {
return int(tt.TotalSize.Byte()), nil
}
func (t *Torrent) Save() string {
d, _ := json.Marshal(*t)
return string(d)
}
func (t *Torrent) GetHash() string {
return t.Hash
return t.hash
}

View File

@@ -1,7 +1,6 @@
package core
import (
"encoding/json"
"polaris/db"
"polaris/ent"
"polaris/ent/downloadclients"
@@ -27,20 +26,20 @@ func NewClient(db *db.Client, language string) *Client {
type scheduler struct {
cron string
f func() error
f func() error
}
type Client struct {
db *db.Client
cron *cron.Cron
tasks map[int]*Task
language string
db *db.Client
cron *cron.Cron
tasks map[int]*Task
language string
schedulers utils.Map[string, scheduler]
}
func (c *Client) registerCronJob(name string, cron string, f func() error) {
c.schedulers.Store(name, scheduler{
cron: cron,
f: f,
f: f,
})
}
@@ -52,44 +51,39 @@ func (c *Client) Init() {
func (c *Client) reloadTasks() {
allTasks := c.db.GetRunningHistories()
for _, t := range allTasks {
var torrent pkg.Torrent
if tt, err := c.reloadTransmiision(t.Saved); err == nil {
torrent = tt
log.Infof("load transmission task: %v", t.Saved)
} else if tt, err := c.reloadQbit(t.Saved); err == nil {
torrent = tt
log.Infof("load qbit task: %v", t.Saved)
} else {
log.Warnf("load task fail: %v", t.Saved)
dl, err := c.db.GetDownloadClient(t.DownloadClientID)
if err != nil {
log.Warnf("no download client related: %v", t.SourceTitle)
continue
}
c.tasks[t.ID] = &Task{Torrent: torrent}
if dl.Implementation == downloadclients.ImplementationTransmission {
to, err := transmission.NewTorrent(transmission.Config{
URL: dl.URL,
User: dl.User,
Password: dl.Password,
}, t.Link)
if err != nil {
log.Warnf("get task error: %v", err)
continue
}
c.tasks[t.ID] = &Task{Torrent: to}
} else if dl.Implementation == downloadclients.ImplementationQbittorrent {
to, err := qbittorrent.NewTorrent(qbittorrent.Info{
URL: dl.URL,
User: dl.User,
Password: dl.Password,
}, t.Link)
if err != nil {
log.Warnf("get task error: %v", err)
continue
}
c.tasks[t.ID] = &Task{Torrent: to}
}
}
}
func (c *Client) reloadTransmiision(s string) (pkg.Torrent, error) {
var t transmission.Torrent
if err := json.Unmarshal([]byte(s), &t); err != nil {
return nil, err
}
if err := t.Reload(); err != nil {
return nil, err
}
return &t, nil
}
func (c *Client) reloadQbit(s string) (pkg.Torrent, error) {
var t qbittorrent.Torrent
if err := json.Unmarshal([]byte(s), &t); err != nil {
return nil, err
}
if err := t.Reload(); err != nil {
return nil, err
}
return &t, nil
}
func (c *Client) GetDownloadClient() (pkg.Downloader, *ent.DownloadClients, error) {
downloaders := c.db.GetAllDonloadClients()
for _, d := range downloaders {
@@ -107,7 +101,7 @@ func (c *Client) GetDownloadClient() (pkg.Downloader, *ent.DownloadClients, erro
continue
}
return trc, d, nil
} else if d.Implementation == downloadclients.ImplementationQbittorrent {
qbt, err := qbittorrent.NewClient(d.URL, d.User, d.Password)
if err != nil {

View File

@@ -59,7 +59,7 @@ func (c *Client) DownloadEpisodeTorrent(r1 torznab.Result, seriesId, seasonNum,
TargetDir: dir,
Status: history.StatusRunning,
Size: r1.Size,
Saved: torrent.Save(),
//Saved: torrent.Save(),
Link: r1.Link,
DownloadClientID: dlc.ID,
IndexerID: r1.IndexerId,
@@ -126,7 +126,7 @@ func (c *Client) DownloadMovie(m *ent.Media, link, name string, size int, indexe
TargetDir: m.TargetDir,
Status: history.StatusRunning,
Size: size,
Saved: torrent.Save(),
//Saved: torrent.Save(),
Link: link,
DownloadClientID: dlc.ID,
IndexerID: indexerID,

View File

@@ -376,7 +376,7 @@ func (c *Client) downloadMovieSingleEpisode(ep *ent.Episode, targetDir string) (
TargetDir: targetDir,
Status: history.StatusRunning,
Size: r1.Size,
Saved: torrent.Save(),
//Saved: torrent.Save(),
Link: r1.Link,
DownloadClientID: dlc.ID,
IndexerID: r1.IndexerId,