feat(confg): reload on save and refactor logging (#275)

* feat(confg): reload on save

* refactor(logging): rework
This commit is contained in:
Ludvig Lundgren 2022-05-20 09:27:01 +02:00 committed by GitHub
parent 198528a474
commit 91b094f4f4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
56 changed files with 995 additions and 873 deletions

View file

@ -5,8 +5,6 @@ import (
"strconv"
"time"
"github.com/rs/zerolog/log"
"github.com/autobrr/autobrr/internal/domain"
"github.com/autobrr/autobrr/pkg/qbittorrent"
)
@ -15,7 +13,7 @@ const ReannounceMaxAttempts = 50
const ReannounceInterval = 7000
func (s *service) qbittorrent(qbt *qbittorrent.Client, action domain.Action, release domain.Release) error {
log.Debug().Msgf("action qBittorrent: %v", action.Name)
s.log.Debug().Msgf("action qBittorrent: %v", action.Name)
// macros handle args and replace vars
m := NewMacro(release)
@ -29,7 +27,7 @@ func (s *service) qbittorrent(qbt *qbittorrent.Client, action domain.Action, rel
// parse and replace values in argument string before continuing
actionArgs, err := m.Parse(action.SavePath)
if err != nil {
log.Error().Stack().Err(err).Msgf("could not parse macro: %v", action.SavePath)
s.log.Error().Stack().Err(err).Msgf("could not parse macro: %v", action.SavePath)
return err
}
@ -40,7 +38,7 @@ func (s *service) qbittorrent(qbt *qbittorrent.Client, action domain.Action, rel
// parse and replace values in argument string before continuing
categoryArgs, err := m.Parse(action.Category)
if err != nil {
log.Error().Stack().Err(err).Msgf("could not parse macro: %v", action.Category)
s.log.Error().Stack().Err(err).Msgf("could not parse macro: %v", action.Category)
return err
}
@ -50,7 +48,7 @@ func (s *service) qbittorrent(qbt *qbittorrent.Client, action domain.Action, rel
// parse and replace values in argument string before continuing
tagsArgs, err := m.Parse(action.Tags)
if err != nil {
log.Error().Stack().Err(err).Msgf("could not parse macro: %v", action.Tags)
s.log.Error().Stack().Err(err).Msgf("could not parse macro: %v", action.Tags)
return err
}
@ -69,34 +67,34 @@ func (s *service) qbittorrent(qbt *qbittorrent.Client, action domain.Action, rel
options["seedingTimeLimit"] = strconv.FormatInt(action.LimitSeedTime, 10)
}
log.Trace().Msgf("action qBittorrent options: %+v", options)
s.log.Trace().Msgf("action qBittorrent options: %+v", options)
err := qbt.AddTorrentFromFile(release.TorrentTmpFile, options)
if err != nil {
log.Error().Stack().Err(err).Msgf("could not add torrent %v to client: %v", release.TorrentTmpFile, qbt.Name)
s.log.Error().Stack().Err(err).Msgf("could not add torrent %v to client: %v", release.TorrentTmpFile, qbt.Name)
return err
}
if !action.Paused && !action.ReAnnounceSkip && release.TorrentHash != "" {
err = s.checkTrackerStatus(qbt, action, release.TorrentHash)
if err != nil {
log.Error().Stack().Err(err).Msgf("could not reannounce torrent: %v", release.TorrentHash)
s.log.Error().Stack().Err(err).Msgf("could not reannounce torrent: %v", release.TorrentHash)
return err
}
}
log.Info().Msgf("torrent with hash %v successfully added to client: '%v'", release.TorrentHash, qbt.Name)
s.log.Info().Msgf("torrent with hash %v successfully added to client: '%v'", release.TorrentHash, qbt.Name)
return nil
}
func (s *service) qbittorrentCheckRulesCanDownload(action domain.Action) (bool, *qbittorrent.Client, error) {
log.Trace().Msgf("action qBittorrent: %v check rules", action.Name)
s.log.Trace().Msgf("action qBittorrent: %v check rules", action.Name)
// get client for action
client, err := s.clientSvc.FindByID(context.TODO(), action.ClientID)
if err != nil {
log.Error().Stack().Err(err).Msgf("error finding client: %v", action.ClientID)
s.log.Error().Stack().Err(err).Msgf("error finding client: %v", action.ClientID)
return false, nil, err
}
@ -118,7 +116,7 @@ func (s *service) qbittorrentCheckRulesCanDownload(action domain.Action) (bool,
// save cookies?
err = qbt.Login()
if err != nil {
log.Error().Stack().Err(err).Msgf("error logging into client: %v", client.Host)
s.log.Error().Stack().Err(err).Msgf("error logging into client: %v", client.Host)
return false, nil, err
}
@ -126,7 +124,7 @@ func (s *service) qbittorrentCheckRulesCanDownload(action domain.Action) (bool,
if client.Settings.Rules.Enabled && !action.IgnoreRules {
activeDownloads, err := qbt.GetTorrentsActiveDownloads()
if err != nil {
log.Error().Stack().Err(err).Msg("could not fetch downloading torrents")
s.log.Error().Stack().Err(err).Msg("could not fetch downloading torrents")
return false, nil, err
}
@ -139,20 +137,20 @@ func (s *service) qbittorrentCheckRulesCanDownload(action domain.Action) (bool,
// check speeds of downloads
info, err := qbt.GetTransferInfo()
if err != nil {
log.Error().Err(err).Msg("could not get transfer info")
s.log.Error().Err(err).Msg("could not get transfer info")
return false, nil, err
}
// if current transfer speed is more than threshold return out and skip
// DlInfoSpeed is in bytes so lets convert to KB to match DownloadSpeedThreshold
if info.DlInfoSpeed/1024 >= client.Settings.Rules.DownloadSpeedThreshold {
log.Debug().Msg("max active downloads reached, skipping")
s.log.Debug().Msg("max active downloads reached, skipping")
return false, nil, nil
}
log.Debug().Msg("active downloads are slower than set limit, lets add it")
s.log.Debug().Msg("active downloads are slower than set limit, lets add it")
} else {
log.Debug().Msg("max active downloads reached, skipping")
s.log.Debug().Msg("max active downloads reached, skipping")
return false, nil, nil
}
}
@ -181,20 +179,20 @@ func (s *service) checkTrackerStatus(qb *qbittorrent.Client, action domain.Actio
}
for attempts < maxAttempts {
log.Debug().Msgf("qBittorrent - run re-announce %v attempt: %v", hash, attempts)
s.log.Debug().Msgf("qBittorrent - run re-announce %v attempt: %v", hash, attempts)
trackers, err := qb.GetTorrentTrackers(hash)
if err != nil {
log.Error().Err(err).Msgf("qBittorrent - could not get trackers for torrent: %v", hash)
s.log.Error().Err(err).Msgf("qBittorrent - could not get trackers for torrent: %v", hash)
return err
}
log.Trace().Msgf("qBittorrent - run re-announce %v attempt: %v trackers (%+v)", hash, attempts, trackers)
s.log.Trace().Msgf("qBittorrent - run re-announce %v attempt: %v trackers (%+v)", hash, attempts, trackers)
// check if status not working or something else
working := findTrackerStatus(trackers)
if working {
log.Debug().Msgf("qBittorrent - re-announce for %v OK", hash)
s.log.Debug().Msgf("qBittorrent - re-announce for %v OK", hash)
announceOK = true
@ -202,10 +200,10 @@ func (s *service) checkTrackerStatus(qb *qbittorrent.Client, action domain.Actio
return nil
}
log.Trace().Msgf("qBittorrent - not working yet, lets re-announce %v attempt: %v", hash, attempts)
s.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)
s.log.Error().Err(err).Msgf("qBittorrent - could not get re-announce torrent: %v", hash)
return err
}
@ -220,11 +218,11 @@ func (s *service) checkTrackerStatus(qb *qbittorrent.Client, action domain.Actio
// delete on failure to reannounce
if !announceOK && action.ReAnnounceDelete {
log.Debug().Msgf("qBittorrent - re-announce for %v took too long, deleting torrent", hash)
s.log.Debug().Msgf("qBittorrent - re-announce for %v took too long, deleting torrent", hash)
err := qb.DeleteTorrents([]string{hash}, false)
if err != nil {
log.Error().Stack().Err(err).Msgf("qBittorrent - could not delete torrent: %v", hash)
s.log.Error().Stack().Err(err).Msgf("qBittorrent - could not delete torrent: %v", hash)
return err
}
}