fix: download clients rule checking (#137)

* fix: download client rules exit

* feat: improve re-announce
This commit is contained in:
Ludvig Lundgren 2022-02-13 18:24:41 +01:00 committed by GitHub
parent b60e5f61c6
commit c3687b8fa5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 74 additions and 23 deletions

View file

@ -10,7 +10,7 @@ import (
"github.com/rs/zerolog/log" "github.com/rs/zerolog/log"
) )
const ReannounceMaxAttempts = 30 const ReannounceMaxAttempts = 50
const ReannounceInterval = 7000 const ReannounceInterval = 7000
func (s *service) qbittorrent(qbt *qbittorrent.Client, action domain.Action, hash string, torrentFile string) error { func (s *service) qbittorrent(qbt *qbittorrent.Client, action domain.Action, hash string, torrentFile string) error {
@ -92,7 +92,7 @@ func (s *service) qbittorrentCheckRulesCanDownload(action domain.Action) (bool,
// check for active downloads and other rules // check for active downloads and other rules
if client.Settings.Rules.Enabled && !action.IgnoreRules { if client.Settings.Rules.Enabled && !action.IgnoreRules {
activeDownloads, err := qbt.GetTorrentsFilter(qbittorrent.TorrentFilterDownloading) activeDownloads, err := qbt.GetTorrentsActiveDownloads()
if err != nil { if err != nil {
log.Error().Stack().Err(err).Msg("could not fetch downloading torrents") log.Error().Stack().Err(err).Msg("could not fetch downloading torrents")
return false, nil, err return false, nil, err
@ -119,6 +119,9 @@ func (s *service) qbittorrentCheckRulesCanDownload(action domain.Action) (bool,
} }
log.Debug().Msg("active downloads are slower than set limit, lets add it") log.Debug().Msg("active downloads are slower than set limit, lets add it")
} else {
log.Debug().Msg("max active downloads reached, skipping")
return false, nil, nil
} }
} }
} }
@ -132,10 +135,12 @@ func checkTrackerStatus(qb qbittorrent.Client, hash string) error {
attempts := 0 attempts := 0
// initial sleep to give tracker a head start // initial sleep to give tracker a head start
time.Sleep(2 * time.Second) time.Sleep(4 * time.Second)
for attempts < ReannounceMaxAttempts { for attempts < ReannounceMaxAttempts {
log.Debug().Msgf("qBittorrent - run re-announce %v attempt: %v", hash, attempts) if attempts > 0 {
log.Debug().Msgf("qBittorrent - run re-announce %v attempt: %v", hash, attempts)
}
trackers, err := qb.GetTorrentTrackers(hash) trackers, err := qb.GetTorrentTrackers(hash)
if err != nil { if err != nil {
@ -145,30 +150,34 @@ func checkTrackerStatus(qb qbittorrent.Client, hash string) error {
// check if status not working or something else // check if status not working or something else
working := findTrackerStatus(trackers, qbittorrent.TrackerStatusOK) working := findTrackerStatus(trackers, qbittorrent.TrackerStatusOK)
if working {
if !working { if attempts > 0 {
log.Trace().Msgf("qBittorrent - not working yet, lets re-announce %v attempt: %v", hash, attempts) log.Debug().Msgf("qBittorrent - re-announce for %v OK", hash)
err = qb.ReAnnounceTorrents([]string{hash})
if err != nil {
log.Error().Err(err).Msgf("qBittorrent - could not get re-announce torrent: %v", hash)
return err
} }
attempts++
// add delay for next run
time.Sleep(ReannounceInterval * time.Millisecond)
continue
} else {
log.Debug().Msgf("qBittorrent - re-announce for %v OK", hash)
announceOK = true announceOK = true
break
// if working lets return
return nil
} }
log.Trace().Msgf("qBittorrent - not working yet, lets re-announce %v attempt: %v", hash, attempts)
err = qb.ReAnnounceTorrents([]string{hash})
if err != nil {
log.Error().Err(err).Msgf("qBittorrent - could not get re-announce torrent: %v", hash)
return err
}
attempts++
// add delay for next run
time.Sleep(ReannounceInterval * time.Millisecond)
continue
} }
// add extra delay before delete // add extra delay before delete
// TODO add setting: delete on failure to reannounce
time.Sleep(30 * time.Second) time.Sleep(30 * time.Second)
if !announceOK { if !announceOK {

View file

@ -92,7 +92,8 @@ func (s *service) runAction(action domain.Action, release domain.Release) error
return err return err
} }
if !canDownload { if !canDownload {
rejections = []string{"deluge busy"} rejections = []string{"max active downloads reached, skipping"}
break
} }
if release.TorrentTmpFile == "" { if release.TorrentTmpFile == "" {
@ -120,7 +121,8 @@ func (s *service) runAction(action domain.Action, release domain.Release) error
return err return err
} }
if !canDownload { if !canDownload {
rejections = []string{"qBittorrent busy"} rejections = []string{"max active downloads reached, skipping"}
break
} }
if release.TorrentTmpFile == "" { if release.TorrentTmpFile == "" {

View file

@ -112,6 +112,46 @@ func (c *Client) GetTorrentsFilter(filter TorrentFilter) ([]Torrent, error) {
return torrents, nil return torrents, nil
} }
func (c *Client) GetTorrentsActiveDownloads() ([]Torrent, error) {
var filter = TorrentFilterDownloading
v := url.Values{}
v.Add("filter", string(filter))
params := v.Encode()
resp, err := c.get("torrents/info?"+params, nil)
if err != nil {
log.Error().Err(err).Msgf("get filtered torrents error: %v", filter)
return nil, err
}
defer resp.Body.Close()
body, readErr := ioutil.ReadAll(resp.Body)
if readErr != nil {
log.Error().Err(err).Msgf("get filtered torrents read error: %v", filter)
return nil, readErr
}
var torrents []Torrent
err = json.Unmarshal(body, &torrents)
if err != nil {
log.Error().Err(err).Msgf("get filtered torrents unmarshal error: %v", filter)
return nil, err
}
res := make([]Torrent, 0)
for _, torrent := range torrents {
// qbit counts paused torrents as downloading as well by default
// so only add torrents with state downloading, and not pausedDl, stalledDl etc
if torrent.State == TorrentStateDownloading {
res = append(res, torrent)
}
}
return res, nil
}
func (c *Client) GetTorrentsRaw() (string, error) { func (c *Client) GetTorrentsRaw() (string, error) {
resp, err := c.get("torrents/info", nil) resp, err := c.get("torrents/info", nil)
if err != nil { if err != nil {