mirror of
https://github.com/simon-ding/polaris.git
synced 2026-06-09 11:39:46 +08:00
feat: task reloading
This commit is contained in:
9
db/db.go
9
db/db.go
@@ -328,6 +328,15 @@ func (c *Client) GetHistories() ent.Histories {
|
|||||||
return h
|
return h
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *Client) GetRunningHistories() ent.Histories {
|
||||||
|
h, err := c.ent.History.Query().Where(history.Completed(false)).All(context.TODO())
|
||||||
|
if err != nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return h
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
func (c *Client) GetHistory(id int) *ent.History {
|
func (c *Client) GetHistory(id int) *ent.History {
|
||||||
return c.ent.History.Query().Where(history.ID(id)).FirstX(context.TODO())
|
return c.ent.History.Query().Where(history.ID(id)).FirstX(context.TODO())
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,34 +2,40 @@ package transmission
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"encoding/json"
|
||||||
"net/url"
|
"net/url"
|
||||||
"polaris/log"
|
"polaris/log"
|
||||||
"strconv"
|
|
||||||
|
|
||||||
"github.com/hekmon/transmissionrpc/v3"
|
"github.com/hekmon/transmissionrpc/v3"
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
)
|
)
|
||||||
|
|
||||||
func NewClient(url1, user, password string) (*Client, error) {
|
func NewClient(c Config) (*Client, error) {
|
||||||
u, err := url.Parse(url1)
|
u, err := url.Parse(c.URL)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, errors.Wrap(err, "parse url")
|
return nil, errors.Wrap(err, "parse url")
|
||||||
}
|
}
|
||||||
if user != "" {
|
if c.User != "" {
|
||||||
log.Info("transmission login with user: ", user)
|
log.Info("transmission login with user: ", c.User)
|
||||||
u.User = url.UserPassword(user, password)
|
u.User = url.UserPassword(c.User, c.Password)
|
||||||
}
|
}
|
||||||
u.Path = "/transmission/rpc"
|
u.Path = "/transmission/rpc"
|
||||||
|
|
||||||
tbt, err := transmissionrpc.New(u, nil)
|
tbt, err := transmissionrpc.New(u, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, errors.Wrap(err, "connect transmission")
|
return nil, errors.Wrap(err, "connect transmission")
|
||||||
}
|
}
|
||||||
return &Client{c: tbt}, nil
|
return &Client{c: tbt, cfg: c}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type Config struct {
|
||||||
|
URL string `json:"url"`
|
||||||
|
User string `json:"user"`
|
||||||
|
Password string `json:"password"`
|
||||||
|
}
|
||||||
type Client struct {
|
type Client struct {
|
||||||
c *transmissionrpc.Client
|
c *transmissionrpc.Client
|
||||||
|
cfg Config
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Client) Download(magnet, dir string) (*Torrent, error) {
|
func (c *Client) Download(magnet, dir string) (*Torrent, error) {
|
||||||
@@ -40,19 +46,30 @@ func (c *Client) Download(magnet, dir string) (*Torrent, error) {
|
|||||||
log.Infof("get torrent info: %+v", t)
|
log.Infof("get torrent info: %+v", t)
|
||||||
|
|
||||||
return &Torrent{
|
return &Torrent{
|
||||||
id: *t.ID,
|
ID: *t.ID,
|
||||||
c: c.c,
|
c: c.c,
|
||||||
|
Config: c.cfg,
|
||||||
}, err
|
}, err
|
||||||
}
|
}
|
||||||
|
|
||||||
type Torrent struct {
|
type Torrent struct {
|
||||||
//t *transmissionrpc.Torrent
|
//t *transmissionrpc.Torrent
|
||||||
c *transmissionrpc.Client
|
c *transmissionrpc.Client
|
||||||
id int64
|
ID int64 `json: "id"`
|
||||||
|
Config
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *Torrent) reloadClient() error {
|
||||||
|
c, err := NewClient(t.Config)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
t.c = c.c
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *Torrent) getTorrent() transmissionrpc.Torrent {
|
func (t *Torrent) getTorrent() transmissionrpc.Torrent {
|
||||||
r, err := t.c.TorrentGetAllFor(context.TODO(), []int64{t.id})
|
r, err := t.c.TorrentGetAllFor(context.TODO(), []int64{t.ID})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Errorf("get torrent info for error: %v", err)
|
log.Errorf("get torrent info for error: %v", err)
|
||||||
}
|
}
|
||||||
@@ -60,7 +77,7 @@ func (t *Torrent) getTorrent() transmissionrpc.Torrent {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (t *Torrent) Exists() bool {
|
func (t *Torrent) Exists() bool {
|
||||||
r, err := t.c.TorrentGetAllFor(context.TODO(), []int64{t.id})
|
r, err := t.c.TorrentGetAllFor(context.TODO(), []int64{t.ID})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Errorf("get torrent info for error: %v", err)
|
log.Errorf("get torrent info for error: %v", err)
|
||||||
}
|
}
|
||||||
@@ -82,21 +99,36 @@ func (t *Torrent) Progress() int {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (t *Torrent) Stop() error {
|
func (t *Torrent) Stop() error {
|
||||||
return t.c.TorrentStopIDs(context.TODO(), []int64{t.id})
|
return t.c.TorrentStopIDs(context.TODO(), []int64{t.ID})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *Torrent) Start() error {
|
func (t *Torrent) Start() error {
|
||||||
return t.c.TorrentStartIDs(context.TODO(), []int64{t.id})
|
return t.c.TorrentStartIDs(context.TODO(), []int64{t.ID})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *Torrent) Remove() error {
|
func (t *Torrent) Remove() error {
|
||||||
return t.c.TorrentRemove(context.TODO(), transmissionrpc.TorrentRemovePayload{
|
return t.c.TorrentRemove(context.TODO(), transmissionrpc.TorrentRemovePayload{
|
||||||
IDs: []int64{t.id},
|
IDs: []int64{t.ID},
|
||||||
DeleteLocalData: true,
|
DeleteLocalData: true,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *Torrent) Save() string {
|
func (t *Torrent) Save() string {
|
||||||
return strconv.Itoa(int(t.id))
|
|
||||||
|
d, _ := json.Marshal(*t)
|
||||||
|
return string(d)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func ReloadTorrent(s string) (*Torrent, error) {
|
||||||
|
var torrent = Torrent{}
|
||||||
|
err := json.Unmarshal([]byte(s), &torrent)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
err = torrent.reloadClient()
|
||||||
|
if err != nil {
|
||||||
|
return nil, errors.Wrap(err, "reload client")
|
||||||
|
}
|
||||||
|
return &torrent, nil
|
||||||
|
}
|
||||||
@@ -78,7 +78,11 @@ type searchAndDownloadIn struct {
|
|||||||
|
|
||||||
func (s *Server) searchAndDownload(seriesId, seasonNum, episodeNum int) (*string, error) {
|
func (s *Server) searchAndDownload(seriesId, seasonNum, episodeNum int) (*string, error) {
|
||||||
tr := s.db.GetTransmission()
|
tr := s.db.GetTransmission()
|
||||||
trc, err := transmission.NewClient(tr.URL, tr.User, tr.Password)
|
trc, err := transmission.NewClient(transmission.Config{
|
||||||
|
URL: tr.URL,
|
||||||
|
User: tr.User,
|
||||||
|
Password: tr.Password,
|
||||||
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, errors.Wrap(err, "connect transmission")
|
return nil, errors.Wrap(err, "connect transmission")
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ import (
|
|||||||
"polaris/log"
|
"polaris/log"
|
||||||
"polaris/pkg"
|
"polaris/pkg"
|
||||||
"polaris/pkg/tmdb"
|
"polaris/pkg/tmdb"
|
||||||
|
"polaris/pkg/transmission"
|
||||||
"polaris/ui"
|
"polaris/ui"
|
||||||
|
|
||||||
"github.com/gin-contrib/static"
|
"github.com/gin-contrib/static"
|
||||||
@@ -35,6 +36,7 @@ type Server struct {
|
|||||||
|
|
||||||
func (s *Server) Serve() error {
|
func (s *Server) Serve() error {
|
||||||
s.scheduler()
|
s.scheduler()
|
||||||
|
s.reloadTasks()
|
||||||
//st, _ := fs.Sub(ui.Web, "build/web")
|
//st, _ := fs.Sub(ui.Web, "build/web")
|
||||||
s.r.Use(static.Serve("/", static.EmbedFolder(ui.Web, "build/web")))
|
s.r.Use(static.Serve("/", static.EmbedFolder(ui.Web, "build/web")))
|
||||||
|
|
||||||
@@ -99,3 +101,20 @@ func (s *Server) MustTMDB() *tmdb.Client {
|
|||||||
}
|
}
|
||||||
return t
|
return t
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
func (s *Server) reloadTasks() {
|
||||||
|
runningTasks := s.db.GetRunningHistories()
|
||||||
|
if len(runningTasks) == 0 {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
for _, t := range runningTasks {
|
||||||
|
log.Infof("reloading task: %s", t.SourceTitle)
|
||||||
|
torrent, err := transmission.ReloadTorrent(t.Saved)
|
||||||
|
if err != nil {
|
||||||
|
log.Errorf("relaod task %s failed: %v", t.SourceTitle, err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
s.tasks[t.ID] = torrent
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user