Skip to content

Commit

Permalink
增加定时器,修复TR做种问题
Browse files Browse the repository at this point in the history
  • Loading branch information
wetor committed Mar 1, 2024
1 parent 18af3b9 commit 5bcbd7a
Show file tree
Hide file tree
Showing 13 changed files with 458 additions and 11 deletions.
2 changes: 1 addition & 1 deletion cmd/animego/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ func Main() {
CheckTimeSecond: config.Advanced.Client.CheckTimeSecond,
RetryConnectNum: config.Advanced.Client.RetryConnectNum,
}
clientSrv := wire.GetClient(config.Setting.Client.Client, clientOpts)
clientSrv := wire.GetClient(config.Setting.Client.Client, clientOpts, bolt)
clientSrv.Start()

// ===============================================================================================================
Expand Down
2 changes: 1 addition & 1 deletion configs/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ type Advanced struct {
} `yaml:"default" json:"default" attr:"解析季度默认值" comment:"使用tmdb解析季度失败时,同类型默认值按优先级执行。数值越大,优先级越高"`

Client struct {
SeedingTimeMinute int `yaml:"seeding_time_minute" json:"seeding_time_minute" attr:"做种时间" comment_key:"seeding_key"`
SeedingTimeMinute int `yaml:"seeding_time_minute" json:"seeding_time_minute" attr:"做种时间" comment:"0不做种,-1无限做种,其他值为做种具体分钟限制"`
ConnectTimeoutSecond int `yaml:"connect_timeout_second" json:"connect_timeout_second" attr:"连接超时时间"`
RetryConnectNum int `yaml:"retry_connect_num" json:"retry_connect_num" attr:"连接失败重试次数"`
CheckTimeSecond int `yaml:"check_time_second" json:"check_time_second" attr:"检查连接状态间隔时间"`
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ require (
github.com/caarlos0/env/v9 v9.0.0
github.com/gin-gonic/gin v1.9.1
github.com/go-python/gpython v0.2.0
github.com/google/uuid v1.5.0
github.com/google/wire v0.5.0
github.com/gorilla/websocket v1.5.0
github.com/hekmon/transmissionrpc/v3 v3.0.0
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,10 @@ github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeN
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/google/subcommands v1.0.1 h1:/eqq+otEXm5vhfBrbREPCSVQbvofip6kIz+mX5TUH7k=
github.com/google/subcommands v1.0.1/go.mod h1:ZjhPrFU+Olkh9WazFPsl27BQ4UPiG37m3yTrtFlrHVk=
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/google/uuid v1.5.0 h1:1p67kYwdtXjb0gL0BPiP1Av9wiZPo5A8z2cWkTZ+eyU=
github.com/google/uuid v1.5.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/google/wire v0.5.0 h1:I7ELFeVBr3yfPIcc8+MWvrjk+3VjbcSzoXm3JVa+jD8=
github.com/google/wire v0.5.0/go.mod h1:ngWDr9Qvq3yZA10YrxfyGELY/AFWGVpy9c1LTRi1EoU=
github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY=
Expand Down
4 changes: 2 additions & 2 deletions internal/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ var Set = wire.NewSet(
NewClient,
)

func NewClient(name string, opts *models.ClientOptions) api.Client {
func NewClient(name string, opts *models.ClientOptions, cache api.Cacher) api.Client {
var c api.Client
switch strings.ToLower(name) {
case "qbittorrent":
c = qbittorrent.NewQBittorrent(opts)
case "transmission":
c = transmission.NewTransmission(opts)
c = transmission.NewTransmission(opts, cache)
}
return c
}
5 changes: 5 additions & 0 deletions internal/client/transmission/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,8 @@ const (
IdleModeSingle // override the global settings, seeding until a certain idle time
IdleModeUnlimited // override the global settings, seeding regardless of activity
)

const (
TimerRetryCount = 3
TimerUpdateSecond = 5
)
41 changes: 40 additions & 1 deletion internal/client/transmission/transmission.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/wetor/AnimeGo/internal/constant"
"github.com/wetor/AnimeGo/internal/exceptions"
"github.com/wetor/AnimeGo/internal/models"
"github.com/wetor/AnimeGo/internal/pkg/timer"
"github.com/wetor/AnimeGo/pkg/log"
"github.com/wetor/AnimeGo/pkg/utils"
)
Expand All @@ -37,16 +38,25 @@ type Transmission struct {
client *transmissionrpc.Client
endpoint *url.URL

seedTimer *timer.Timer

*models.ClientOptions
}

func NewTransmission(opts *models.ClientOptions) *Transmission {
func NewTransmission(opts *models.ClientOptions, cache api.Cacher) *Transmission {
c := &Transmission{
retryChan: make(chan int, 1),
retryNum: 1,
connected: false,
ClientOptions: opts,
}
c.seedTimer = timer.NewTimer(&timer.Options{
Name: Name,
Cache: cache,
RetryCount: TimerRetryCount,
UpdateSecond: TimerUpdateSecond,
WG: opts.WG,
})
u, _ := url.Parse(c.Url)
c.endpoint, _ = url.Parse(fmt.Sprintf("%s://%s:%s@%s/transmission/rpc",
u.Scheme, c.Username, c.Password, u.Host))
Expand Down Expand Up @@ -182,6 +192,7 @@ func (c *Transmission) Start() {
}
}
}()
c.seedTimer.Start(c.Ctx)
}

func (c *Transmission) List(opt *models.ListOptions) ([]*models.TorrentItem, error) {
Expand All @@ -196,6 +207,34 @@ func (c *Transmission) List(opt *models.ListOptions) ([]*models.TorrentItem, err
items := make([]*models.TorrentItem, 0, len(torrents))

for _, torrent := range torrents {
// 下载完成在做种状态
if int(*torrent.PercentDone) == 1 && torrent.Status.String() == TorrentStatusSeed &&
!c.seedTimer.HasTask(*torrent.HashString) {
// 定时任务:达到做种时间,停止做种
_, err = c.seedTimer.AddTask(&timer.AddOptions{
Name: *torrent.HashString,
Duration: int64(c.SeedingTimeMinute * 60),
Func: func() error {
err := c.client.TorrentStopHashes(c.Ctx, []string{*torrent.HashString})
if err != nil {
return err
}
tStatus, err := c.client.TorrentGet(c.Ctx, []string{"status"}, []int64{*torrent.ID})
if err != nil {
return err
}
if len(tStatus) == 1 && tStatus[0].Status.String() != TorrentStatusStopped {
// 未暂停成功
return exceptions.ErrClient{Client: Name, Message: fmt.Sprintf("%s 暂停失败", *torrent.HashString)}
}
return nil
},
})
if err != nil {
log.Warnf("添加 %s 做种任务 %s 失败,忽略", Name, *torrent.HashString)
}
}

if len(opt.Category) > 0 && *torrent.Group != opt.Category {
continue
}
Expand Down
39 changes: 39 additions & 0 deletions internal/exceptions/timer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package exceptions

import "fmt"

type ErrTimer struct {
Message string
}

func (e ErrTimer) Error() string {
return e.Message
}

type ErrTimerExistTask struct {
Name string
Message string
}

func (e ErrTimerExistTask) Error() string {
if len(e.Message) == 0 {
return fmt.Sprintf("任务 %s 已存在", e.Name)
}
return fmt.Sprintf("任务 %s 已存在,%s", e.Name, e.Message)
}

func (e ErrTimerExistTask) Exist() bool {
return true
}

type ErrTimerRun struct {
Name string
Message string
}

func (e ErrTimerRun) Error() string {
if len(e.Message) == 0 {
return fmt.Sprintf("任务 %s 执行失败", e.Name)
}
return fmt.Sprintf("任务 %s 执行失败,%s", e.Name, e.Message)
}
47 changes: 47 additions & 0 deletions internal/pkg/timer/models.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package timer

import (
"sync"

"github.com/google/uuid"
"github.com/wetor/AnimeGo/internal/api"
)

const (
DefaultUpdateSecond = 1
DefaultRetryCount = 1
)

type TaskFunc func() error

type Options struct {
Name string

Cache api.Cacher
RetryCount int
UpdateSecond int

WG *sync.WaitGroup
}

func (o *Options) Default() {
if o.RetryCount == 0 {
o.RetryCount = DefaultRetryCount
}
if o.UpdateSecond == 0 {
o.UpdateSecond = DefaultUpdateSecond
}
}

type AddOptions struct {
Name string
Duration int64
Func TaskFunc
Loop bool
}

func (o *AddOptions) Default() {
if len(o.Name) == 0 {
o.Name = uuid.NewString()
}
}
Loading

0 comments on commit 5bcbd7a

Please sign in to comment.