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

@ -10,18 +10,17 @@ import (
"github.com/autobrr/autobrr/internal/domain"
delugeClient "github.com/gdm85/go-libdeluge"
"github.com/rs/zerolog/log"
)
func (s *service) deluge(action domain.Action, release domain.Release) error {
log.Debug().Msgf("action Deluge: %v", action.Name)
s.log.Debug().Msgf("action Deluge: %v", action.Name)
var err error
// 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 err
}
@ -40,12 +39,12 @@ func (s *service) deluge(action domain.Action, release domain.Release) error {
switch client.Type {
case "DELUGE_V1":
if err = delugeV1(client, settings, action, release); err != nil {
if err = s.delugeV1(client, settings, action, release); err != nil {
return err
}
case "DELUGE_V2":
if err = delugeV2(client, settings, action, release); err != nil {
if err = s.delugeV2(client, settings, action, release); err != nil {
return err
}
}
@ -54,12 +53,12 @@ func (s *service) deluge(action domain.Action, release domain.Release) error {
}
func (s *service) delugeCheckRulesCanDownload(action domain.Action) (bool, error) {
log.Trace().Msgf("action Deluge: %v check rules", action.Name)
s.log.Trace().Msgf("action Deluge: %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 ID %v", action.Name, action.ClientID)
s.log.Error().Stack().Err(err).Msgf("error finding client: %v ID %v", action.Name, action.ClientID)
return false, err
}
@ -88,7 +87,7 @@ func (s *service) delugeCheckRulesCanDownload(action domain.Action) (bool, error
// perform connection to Deluge server
err = deluge.Connect()
if err != nil {
log.Error().Stack().Err(err).Msgf("error logging into client: %v %v", client.Name, client.Host)
s.log.Error().Stack().Err(err).Msgf("error logging into client: %v %v", client.Name, client.Host)
return false, err
}
@ -98,7 +97,7 @@ func (s *service) delugeCheckRulesCanDownload(action domain.Action) (bool, error
if client.Settings.Rules.Enabled && !action.IgnoreRules {
activeDownloads, err := deluge.TorrentsStatus(delugeClient.StateDownloading, nil)
if err != nil {
log.Error().Stack().Err(err).Msg("Deluge - could not fetch downloading torrents")
s.log.Error().Stack().Err(err).Msg("Deluge - could not fetch downloading torrents")
return false, err
}
@ -107,7 +106,7 @@ func (s *service) delugeCheckRulesCanDownload(action domain.Action) (bool, error
// if max active downloads reached, check speed and if lower than threshold add anyways
if len(activeDownloads) >= client.Settings.Rules.MaxActiveDownloads {
log.Debug().Msg("max active downloads reached, skipping")
s.log.Debug().Msg("max active downloads reached, skipping")
return false, nil
// // TODO handle ignore slow torrents
@ -117,16 +116,16 @@ func (s *service) delugeCheckRulesCanDownload(action domain.Action) (bool, error
// // gives type conversion errors
// state, err := deluge.GetSessionStatus()
// if err != nil {
// log.Error().Err(err).Msg("could not get session state")
// s.log.Error().Err(err).Msg("could not get session state")
// return err
// }
//
// if int64(state.DownloadRate)*1024 >= client.Settings.Rules.DownloadSpeedThreshold {
// log.Trace().Msg("max active downloads reached, skip adding")
// s.log.Trace().Msg("max active downloads reached, skip adding")
// return nil
// }
//
// log.Trace().Msg("active downloads are slower than set limit, lets add it")
// s.log.Trace().Msg("active downloads are slower than set limit, lets add it")
//}
}
}
@ -135,14 +134,14 @@ func (s *service) delugeCheckRulesCanDownload(action domain.Action) (bool, error
return true, nil
}
func delugeV1(client *domain.DownloadClient, settings delugeClient.Settings, action domain.Action, release domain.Release) error {
func (s *service) delugeV1(client *domain.DownloadClient, settings delugeClient.Settings, action domain.Action, release domain.Release) error {
deluge := delugeClient.NewV1(settings)
// perform connection to Deluge server
err := deluge.Connect()
if err != nil {
log.Error().Stack().Err(err).Msgf("error logging into client: %v %v", client.Name, client.Host)
s.log.Error().Stack().Err(err).Msgf("error logging into client: %v %v", client.Name, client.Host)
return err
}
@ -150,14 +149,14 @@ func delugeV1(client *domain.DownloadClient, settings delugeClient.Settings, act
t, err := ioutil.ReadFile(release.TorrentTmpFile)
if err != nil {
log.Error().Stack().Err(err).Msgf("could not read torrent file: %v", release.TorrentTmpFile)
s.log.Error().Stack().Err(err).Msgf("could not read torrent file: %v", release.TorrentTmpFile)
return err
}
// encode file to base64 before sending to deluge
encodedFile := base64.StdEncoding.EncodeToString(t)
if encodedFile == "" {
log.Error().Stack().Err(err).Msgf("could not encode torrent file: %v", release.TorrentTmpFile)
s.log.Error().Stack().Err(err).Msgf("could not encode torrent file: %v", release.TorrentTmpFile)
return err
}
@ -174,7 +173,7 @@ func delugeV1(client *domain.DownloadClient, settings delugeClient.Settings, act
// parse and replace values in argument string before continuing
savePathArgs, 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
}
@ -189,25 +188,25 @@ func delugeV1(client *domain.DownloadClient, settings delugeClient.Settings, act
options.MaxUploadSpeed = &maxUL
}
log.Trace().Msgf("action Deluge options: %+v", options)
s.log.Trace().Msgf("action Deluge options: %+v", options)
torrentHash, err := deluge.AddTorrentFile(release.TorrentTmpFile, encodedFile, &options)
if err != nil {
log.Error().Stack().Err(err).Msgf("could not add torrent %v to client: %v", release.TorrentTmpFile, client.Name)
s.log.Error().Stack().Err(err).Msgf("could not add torrent %v to client: %v", release.TorrentTmpFile, client.Name)
return err
}
if action.Label != "" {
p, err := deluge.LabelPlugin()
if err != nil {
log.Error().Stack().Err(err).Msgf("could not load label plugin: %v", client.Name)
s.log.Error().Stack().Err(err).Msgf("could not load label plugin: %v", client.Name)
return err
}
// parse and replace values in argument string before continuing
labelArgs, err := m.Parse(action.Label)
if err != nil {
log.Error().Stack().Err(err).Msgf("could not parse macro: %v", action.Label)
s.log.Error().Stack().Err(err).Msgf("could not parse macro: %v", action.Label)
return err
}
@ -215,25 +214,25 @@ func delugeV1(client *domain.DownloadClient, settings delugeClient.Settings, act
// TODO first check if label exists, if not, add it, otherwise set
err = p.SetTorrentLabel(torrentHash, labelArgs)
if err != nil {
log.Error().Stack().Err(err).Msgf("could not set label: %v on client: %v", action.Label, client.Name)
s.log.Error().Stack().Err(err).Msgf("could not set label: %v on client: %v", action.Label, client.Name)
return err
}
}
}
log.Info().Msgf("torrent with hash %v successfully added to client: '%v'", torrentHash, client.Name)
s.log.Info().Msgf("torrent with hash %v successfully added to client: '%v'", torrentHash, client.Name)
return nil
}
func delugeV2(client *domain.DownloadClient, settings delugeClient.Settings, action domain.Action, release domain.Release) error {
func (s *service) delugeV2(client *domain.DownloadClient, settings delugeClient.Settings, action domain.Action, release domain.Release) error {
deluge := delugeClient.NewV2(settings)
// perform connection to Deluge server
err := deluge.Connect()
if err != nil {
log.Error().Stack().Err(err).Msgf("error logging into client: %v %v", client.Name, client.Host)
s.log.Error().Stack().Err(err).Msgf("error logging into client: %v %v", client.Name, client.Host)
return err
}
@ -241,14 +240,14 @@ func delugeV2(client *domain.DownloadClient, settings delugeClient.Settings, act
t, err := ioutil.ReadFile(release.TorrentTmpFile)
if err != nil {
log.Error().Stack().Err(err).Msgf("could not read torrent file: %v", release.TorrentTmpFile)
s.log.Error().Stack().Err(err).Msgf("could not read torrent file: %v", release.TorrentTmpFile)
return err
}
// encode file to base64 before sending to deluge
encodedFile := base64.StdEncoding.EncodeToString(t)
if encodedFile == "" {
log.Error().Stack().Err(err).Msgf("could not encode torrent file: %v", release.TorrentTmpFile)
s.log.Error().Stack().Err(err).Msgf("could not encode torrent file: %v", release.TorrentTmpFile)
return err
}
@ -265,7 +264,7 @@ func delugeV2(client *domain.DownloadClient, settings delugeClient.Settings, act
// parse and replace values in argument string before continuing
savePathArgs, 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
}
@ -280,25 +279,25 @@ func delugeV2(client *domain.DownloadClient, settings delugeClient.Settings, act
options.MaxUploadSpeed = &maxUL
}
log.Trace().Msgf("action Deluge options: %+v", options)
s.log.Trace().Msgf("action Deluge options: %+v", options)
torrentHash, err := deluge.AddTorrentFile(release.TorrentTmpFile, encodedFile, &options)
if err != nil {
log.Error().Stack().Err(err).Msgf("could not add torrent %v to client: %v", release.TorrentTmpFile, client.Name)
s.log.Error().Stack().Err(err).Msgf("could not add torrent %v to client: %v", release.TorrentTmpFile, client.Name)
return err
}
if action.Label != "" {
p, err := deluge.LabelPlugin()
if err != nil {
log.Error().Stack().Err(err).Msgf("could not load label plugin: %v", client.Name)
s.log.Error().Stack().Err(err).Msgf("could not load label plugin: %v", client.Name)
return err
}
// parse and replace values in argument string before continuing
labelArgs, err := m.Parse(action.Label)
if err != nil {
log.Error().Stack().Err(err).Msgf("could not parse macro: %v", action.Label)
s.log.Error().Stack().Err(err).Msgf("could not parse macro: %v", action.Label)
return err
}
@ -306,13 +305,13 @@ func delugeV2(client *domain.DownloadClient, settings delugeClient.Settings, act
// TODO first check if label exists, if not, add it, otherwise set
err = p.SetTorrentLabel(torrentHash, labelArgs)
if err != nil {
log.Error().Stack().Err(err).Msgf("could not set label: %v on client: %v", action.Label, client.Name)
s.log.Error().Stack().Err(err).Msgf("could not set label: %v on client: %v", action.Label, client.Name)
return err
}
}
}
log.Info().Msgf("torrent with hash %v successfully added to client: '%v'", torrentHash, client.Name)
s.log.Info().Msgf("torrent with hash %v successfully added to client: '%v'", torrentHash, client.Name)
return nil
}

View file

@ -6,17 +6,15 @@ import (
"time"
"github.com/autobrr/autobrr/internal/domain"
"github.com/rs/zerolog/log"
)
func (s *service) execCmd(release domain.Release, action domain.Action) {
log.Debug().Msgf("action exec: %v release: %v", action.Name, release.TorrentName)
s.log.Debug().Msgf("action exec: %v release: %v", action.Name, release.TorrentName)
// check if program exists
cmd, err := exec.LookPath(action.ExecCmd)
if err != nil {
log.Error().Stack().Err(err).Msgf("exec failed, could not find program: %v", action.ExecCmd)
s.log.Error().Stack().Err(err).Msgf("exec failed, could not find program: %v", action.ExecCmd)
return
}
@ -26,7 +24,7 @@ func (s *service) execCmd(release domain.Release, action domain.Action) {
// parse and replace values in argument string before continuing
parsedArgs, err := m.Parse(action.ExecArgs)
if err != nil {
log.Error().Stack().Err(err).Msgf("exec failed, could not parse arguments: %v", action.ExecCmd)
s.log.Error().Stack().Err(err).Msgf("exec failed, could not parse arguments: %v", action.ExecCmd)
return
}
@ -42,12 +40,12 @@ func (s *service) execCmd(release domain.Release, action domain.Action) {
output, err := command.CombinedOutput()
if err != nil {
// everything other than exit 0 is considered an error
log.Error().Stack().Err(err).Msgf("command: %v args: %v failed, torrent: %v", cmd, parsedArgs, release.TorrentTmpFile)
s.log.Error().Stack().Err(err).Msgf("command: %v args: %v failed, torrent: %v", cmd, parsedArgs, release.TorrentTmpFile)
}
log.Trace().Msgf("executed command: '%v'", string(output))
s.log.Trace().Msgf("executed command: '%v'", string(output))
duration := time.Since(start)
log.Info().Msgf("executed command: '%v', args: '%v' %v,%v, total time %v", cmd, parsedArgs, release.TorrentName, release.Indexer, duration)
s.log.Info().Msgf("executed command: '%v', args: '%v' %v,%v, total time %v", cmd, parsedArgs, release.TorrentName, release.Indexer, duration)
}

View file

@ -7,19 +7,17 @@ import (
"github.com/autobrr/autobrr/internal/domain"
"github.com/autobrr/autobrr/pkg/lidarr"
"github.com/rs/zerolog/log"
)
func (s *service) lidarr(release domain.Release, action domain.Action) ([]string, error) {
log.Trace().Msg("action LIDARR")
s.log.Trace().Msg("action LIDARR")
// TODO validate data
// get client for action
client, err := s.clientSvc.FindByID(context.TODO(), action.ClientID)
if err != nil {
log.Error().Err(err).Msgf("lidarr: error finding client: %v", action.ClientID)
s.log.Error().Err(err).Msgf("lidarr: error finding client: %v", action.ClientID)
return nil, err
}
@ -61,17 +59,17 @@ func (s *service) lidarr(release domain.Release, action domain.Action) ([]string
rejections, err := arr.Push(r)
if err != nil {
log.Error().Stack().Err(err).Msgf("lidarr: failed to push release: %v", r)
s.log.Error().Stack().Err(err).Msgf("lidarr: failed to push release: %v", r)
return nil, err
}
if rejections != nil {
log.Debug().Msgf("lidarr: release push rejected: %v, indexer %v to %v reasons: '%v'", r.Title, r.Indexer, client.Host, rejections)
s.log.Debug().Msgf("lidarr: release push rejected: %v, indexer %v to %v reasons: '%v'", r.Title, r.Indexer, client.Host, rejections)
return rejections, nil
}
log.Debug().Msgf("lidarr: successfully pushed release: %v, indexer %v to %v", r.Title, r.Indexer, client.Host)
s.log.Debug().Msgf("lidarr: successfully pushed release: %v, indexer %v to %v", r.Title, r.Indexer, client.Host)
return nil, nil
}

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
}
}

View file

@ -6,19 +6,17 @@ import (
"github.com/autobrr/autobrr/internal/domain"
"github.com/autobrr/autobrr/pkg/radarr"
"github.com/rs/zerolog/log"
)
func (s *service) radarr(release domain.Release, action domain.Action) ([]string, error) {
log.Trace().Msg("action RADARR")
s.log.Trace().Msg("action RADARR")
// TODO validate data
// get client for action
client, err := s.clientSvc.FindByID(context.TODO(), action.ClientID)
if err != nil {
log.Error().Err(err).Msgf("radarr: error finding client: %v", action.ClientID)
s.log.Error().Err(err).Msgf("radarr: error finding client: %v", action.ClientID)
return nil, err
}
@ -54,17 +52,17 @@ func (s *service) radarr(release domain.Release, action domain.Action) ([]string
rejections, err := arr.Push(r)
if err != nil {
log.Error().Stack().Err(err).Msgf("radarr: failed to push release: %v", r)
s.log.Error().Stack().Err(err).Msgf("radarr: failed to push release: %v", r)
return nil, err
}
if rejections != nil {
log.Debug().Msgf("radarr: release push rejected: %v, indexer %v to %v reasons: '%v'", r.Title, r.Indexer, client.Host, rejections)
s.log.Debug().Msgf("radarr: release push rejected: %v, indexer %v to %v reasons: '%v'", r.Title, r.Indexer, client.Host, rejections)
return rejections, nil
}
log.Debug().Msgf("radarr: successfully pushed release: %v, indexer %v to %v", r.Title, r.Indexer, client.Host)
s.log.Debug().Msgf("radarr: successfully pushed release: %v, indexer %v to %v", r.Title, r.Indexer, client.Host)
return nil, nil
}

View file

@ -9,8 +9,6 @@ import (
"path"
"time"
"github.com/rs/zerolog/log"
"github.com/autobrr/autobrr/internal/domain"
)
@ -22,11 +20,11 @@ func (s *service) RunActions(actions []domain.Action, release domain.Release) er
continue
}
log.Debug().Msgf("process action: %v for '%v'", action.Name, release.TorrentName)
s.log.Debug().Msgf("process action: %v for '%v'", action.Name, release.TorrentName)
err := s.runAction(action, release)
if err != nil {
log.Err(err).Stack().Msgf("process action failed: %v for '%v'", action.Name, release.TorrentName)
s.log.Err(err).Stack().Msgf("process action failed: %v for '%v'", action.Name, release.TorrentName)
s.bus.Publish("release:store-action-status", &domain.ReleaseActionStatus{
ReleaseID: release.ID,
@ -71,7 +69,7 @@ func (s *service) RunAction(action *domain.Action, release domain.Release) ([]st
case domain.ActionTypeExec:
if release.TorrentTmpFile == "" {
if err := release.DownloadTorrentFile(); err != nil {
log.Error().Stack().Err(err)
s.log.Error().Stack().Err(err)
break
}
}
@ -81,7 +79,7 @@ func (s *service) RunAction(action *domain.Action, release domain.Release) ([]st
case domain.ActionTypeWatchFolder:
if release.TorrentTmpFile == "" {
if err := release.DownloadTorrentFile(); err != nil {
log.Error().Stack().Err(err)
s.log.Error().Stack().Err(err)
break
}
}
@ -91,7 +89,7 @@ func (s *service) RunAction(action *domain.Action, release domain.Release) ([]st
case domain.ActionTypeWebhook:
if release.TorrentTmpFile == "" {
if err := release.DownloadTorrentFile(); err != nil {
log.Error().Stack().Err(err)
s.log.Error().Stack().Err(err)
break
}
}
@ -101,7 +99,7 @@ func (s *service) RunAction(action *domain.Action, release domain.Release) ([]st
case domain.ActionTypeDelugeV1, domain.ActionTypeDelugeV2:
canDownload, err := s.delugeCheckRulesCanDownload(*action)
if err != nil {
log.Error().Stack().Err(err).Msgf("error checking client rules: %v", action.Name)
s.log.Error().Stack().Err(err).Msgf("error checking client rules: %v", action.Name)
break
}
if !canDownload {
@ -111,21 +109,21 @@ func (s *service) RunAction(action *domain.Action, release domain.Release) ([]st
if release.TorrentTmpFile == "" {
if err := release.DownloadTorrentFile(); err != nil {
log.Error().Stack().Err(err)
s.log.Error().Stack().Err(err)
break
}
}
err = s.deluge(*action, release)
if err != nil {
log.Error().Stack().Err(err).Msg("error sending torrent to Deluge")
s.log.Error().Stack().Err(err).Msg("error sending torrent to Deluge")
break
}
case domain.ActionTypeQbittorrent:
canDownload, client, err := s.qbittorrentCheckRulesCanDownload(*action)
if err != nil {
log.Error().Stack().Err(err).Msgf("error checking client rules: %v", action.Name)
s.log.Error().Stack().Err(err).Msgf("error checking client rules: %v", action.Name)
break
}
if !canDownload {
@ -135,47 +133,47 @@ func (s *service) RunAction(action *domain.Action, release domain.Release) ([]st
if release.TorrentTmpFile == "" {
if err := release.DownloadTorrentFile(); err != nil {
log.Error().Stack().Err(err)
s.log.Error().Stack().Err(err)
break
}
}
err = s.qbittorrent(client, *action, release)
if err != nil {
log.Error().Stack().Err(err).Msg("error sending torrent to qBittorrent")
s.log.Error().Stack().Err(err).Msg("error sending torrent to qBittorrent")
break
}
case domain.ActionTypeRadarr:
rejections, err = s.radarr(release, *action)
if err != nil {
log.Error().Stack().Err(err).Msg("error sending torrent to radarr")
s.log.Error().Stack().Err(err).Msg("error sending torrent to radarr")
break
}
case domain.ActionTypeSonarr:
rejections, err = s.sonarr(release, *action)
if err != nil {
log.Error().Stack().Err(err).Msg("error sending torrent to sonarr")
s.log.Error().Stack().Err(err).Msg("error sending torrent to sonarr")
break
}
case domain.ActionTypeLidarr:
rejections, err = s.lidarr(release, *action)
if err != nil {
log.Error().Stack().Err(err).Msg("error sending torrent to lidarr")
s.log.Error().Stack().Err(err).Msg("error sending torrent to lidarr")
break
}
case domain.ActionTypeWhisparr:
rejections, err = s.whisparr(release, *action)
if err != nil {
log.Error().Stack().Err(err).Msg("error sending torrent to whisparr")
s.log.Error().Stack().Err(err).Msg("error sending torrent to whisparr")
break
}
default:
log.Warn().Msgf("unsupported action type: %v", action.Type)
s.log.Warn().Msgf("unsupported action type: %v", action.Type)
return rejections, err
}
@ -205,7 +203,7 @@ func (s *service) RunAction(action *domain.Action, release domain.Release) ([]st
}
if err != nil {
log.Err(err).Stack().Msgf("process action failed: %v for '%v'", action.Name, release.TorrentName)
s.log.Err(err).Stack().Msgf("process action failed: %v for '%v'", action.Name, release.TorrentName)
rlsActionStatus.Status = domain.ReleasePushStatusErr
rlsActionStatus.Rejections = []string{err.Error()}
@ -243,7 +241,7 @@ func (s *service) runAction(action domain.Action, release domain.Release) error
case domain.ActionTypeExec:
if release.TorrentTmpFile == "" {
if err := release.DownloadTorrentFile(); err != nil {
log.Error().Stack().Err(err)
s.log.Error().Stack().Err(err)
return err
}
}
@ -253,7 +251,7 @@ func (s *service) runAction(action domain.Action, release domain.Release) error
case domain.ActionTypeWatchFolder:
if release.TorrentTmpFile == "" {
if err := release.DownloadTorrentFile(); err != nil {
log.Error().Stack().Err(err)
s.log.Error().Stack().Err(err)
return err
}
}
@ -263,7 +261,7 @@ func (s *service) runAction(action domain.Action, release domain.Release) error
case domain.ActionTypeWebhook:
if release.TorrentTmpFile == "" {
if err := release.DownloadTorrentFile(); err != nil {
log.Error().Stack().Err(err)
s.log.Error().Stack().Err(err)
return err
}
}
@ -273,7 +271,7 @@ func (s *service) runAction(action domain.Action, release domain.Release) error
case domain.ActionTypeDelugeV1, domain.ActionTypeDelugeV2:
canDownload, err := s.delugeCheckRulesCanDownload(action)
if err != nil {
log.Error().Stack().Err(err).Msgf("error checking client rules: %v", action.Name)
s.log.Error().Stack().Err(err).Msgf("error checking client rules: %v", action.Name)
return err
}
if !canDownload {
@ -283,21 +281,21 @@ func (s *service) runAction(action domain.Action, release domain.Release) error
if release.TorrentTmpFile == "" {
if err := release.DownloadTorrentFile(); err != nil {
log.Error().Stack().Err(err)
s.log.Error().Stack().Err(err)
return err
}
}
err = s.deluge(action, release)
if err != nil {
log.Error().Stack().Err(err).Msg("error sending torrent to Deluge")
s.log.Error().Stack().Err(err).Msg("error sending torrent to Deluge")
return err
}
case domain.ActionTypeQbittorrent:
canDownload, client, err := s.qbittorrentCheckRulesCanDownload(action)
if err != nil {
log.Error().Stack().Err(err).Msgf("error checking client rules: %v", action.Name)
s.log.Error().Stack().Err(err).Msgf("error checking client rules: %v", action.Name)
return err
}
if !canDownload {
@ -307,47 +305,47 @@ func (s *service) runAction(action domain.Action, release domain.Release) error
if release.TorrentTmpFile == "" {
if err := release.DownloadTorrentFile(); err != nil {
log.Error().Stack().Err(err)
s.log.Error().Stack().Err(err)
return err
}
}
err = s.qbittorrent(client, action, release)
if err != nil {
log.Error().Stack().Err(err).Msg("error sending torrent to qBittorrent")
s.log.Error().Stack().Err(err).Msg("error sending torrent to qBittorrent")
return err
}
case domain.ActionTypeRadarr:
rejections, err = s.radarr(release, action)
if err != nil {
log.Error().Stack().Err(err).Msg("error sending torrent to radarr")
s.log.Error().Stack().Err(err).Msg("error sending torrent to radarr")
return err
}
case domain.ActionTypeSonarr:
rejections, err = s.sonarr(release, action)
if err != nil {
log.Error().Stack().Err(err).Msg("error sending torrent to sonarr")
s.log.Error().Stack().Err(err).Msg("error sending torrent to sonarr")
return err
}
case domain.ActionTypeLidarr:
rejections, err = s.lidarr(release, action)
if err != nil {
log.Error().Stack().Err(err).Msg("error sending torrent to lidarr")
s.log.Error().Stack().Err(err).Msg("error sending torrent to lidarr")
return err
}
case domain.ActionTypeWhisparr:
rejections, err = s.whisparr(release, action)
if err != nil {
log.Error().Stack().Err(err).Msg("error sending torrent to whisparr")
s.log.Error().Stack().Err(err).Msg("error sending torrent to whisparr")
return err
}
default:
log.Warn().Msgf("unsupported action: %v type: %v", action.Name, action.Type)
s.log.Warn().Msgf("unsupported action: %v type: %v", action.Name, action.Type)
return nil
}
@ -399,13 +397,13 @@ func (s *service) CheckCanDownload(actions []domain.Action) bool {
continue
}
log.Debug().Msgf("action-service: check can download action: %v", action.Name)
s.log.Debug().Msgf("action-service: check can download action: %v", action.Name)
switch action.Type {
case domain.ActionTypeDelugeV1, domain.ActionTypeDelugeV2:
canDownload, err := s.delugeCheckRulesCanDownload(action)
if err != nil {
log.Error().Stack().Err(err).Msgf("error checking client rules: %v", action.Name)
s.log.Error().Stack().Err(err).Msgf("error checking client rules: %v", action.Name)
continue
}
if !canDownload {
@ -417,7 +415,7 @@ func (s *service) CheckCanDownload(actions []domain.Action) bool {
case domain.ActionTypeQbittorrent:
canDownload, _, err := s.qbittorrentCheckRulesCanDownload(action)
if err != nil {
log.Error().Stack().Err(err).Msgf("error checking client rules: %v", action.Name)
s.log.Error().Stack().Err(err).Msgf("error checking client rules: %v", action.Name)
continue
}
if !canDownload {
@ -432,7 +430,7 @@ func (s *service) CheckCanDownload(actions []domain.Action) bool {
}
func (s *service) test(name string) {
log.Info().Msgf("action TEST: %v", name)
s.log.Info().Msgf("action TEST: %v", name)
}
func (s *service) watchFolder(action domain.Action, release domain.Release) {
@ -441,15 +439,15 @@ func (s *service) watchFolder(action domain.Action, release domain.Release) {
// parse and replace values in argument string before continuing
watchFolderArgs, err := m.Parse(action.WatchFolder)
if err != nil {
log.Error().Stack().Err(err).Msgf("could not parse macro: %v", action.WatchFolder)
s.log.Error().Stack().Err(err).Msgf("could not parse macro: %v", action.WatchFolder)
}
log.Trace().Msgf("action WATCH_FOLDER: %v file: %v", watchFolderArgs, release.TorrentTmpFile)
s.log.Trace().Msgf("action WATCH_FOLDER: %v file: %v", watchFolderArgs, release.TorrentTmpFile)
// Open original file
original, err := os.Open(release.TorrentTmpFile)
if err != nil {
log.Error().Stack().Err(err).Msgf("could not open temp file '%v'", release.TorrentTmpFile)
s.log.Error().Stack().Err(err).Msgf("could not open temp file '%v'", release.TorrentTmpFile)
return
}
defer original.Close()
@ -460,7 +458,7 @@ func (s *service) watchFolder(action domain.Action, release domain.Release) {
// Create new file
newFile, err := os.Create(fullFileName)
if err != nil {
log.Error().Stack().Err(err).Msgf("could not create new temp file '%v'", fullFileName)
s.log.Error().Stack().Err(err).Msgf("could not create new temp file '%v'", fullFileName)
return
}
defer newFile.Close()
@ -468,11 +466,11 @@ func (s *service) watchFolder(action domain.Action, release domain.Release) {
// Copy file
_, err = io.Copy(newFile, original)
if err != nil {
log.Error().Stack().Err(err).Msgf("could not copy file %v to watch folder", fullFileName)
s.log.Error().Stack().Err(err).Msgf("could not copy file %v to watch folder", fullFileName)
return
}
log.Info().Msgf("saved file to watch folder: %v", fullFileName)
s.log.Info().Msgf("saved file to watch folder: %v", fullFileName)
}
func (s *service) webhook(action domain.Action, release domain.Release) {
@ -481,12 +479,12 @@ func (s *service) webhook(action domain.Action, release domain.Release) {
// parse and replace values in argument string before continuing
dataArgs, err := m.Parse(action.WebhookData)
if err != nil {
log.Error().Stack().Err(err).Msgf("could not parse macro: %v", action.WebhookData)
s.log.Error().Stack().Err(err).Msgf("could not parse macro: %v", action.WebhookData)
return
}
log.Trace().Msgf("action WEBHOOK: '%v' file: %v", action.Name, release.TorrentName)
log.Trace().Msgf("webhook action '%v' - host: %v data: %v", action.Name, action.WebhookHost, action.WebhookData)
s.log.Trace().Msgf("action WEBHOOK: '%v' file: %v", action.Name, release.TorrentName)
s.log.Trace().Msgf("webhook action '%v' - host: %v data: %v", action.Name, action.WebhookHost, action.WebhookData)
t := &http.Transport{
TLSClientConfig: &tls.Config{
@ -498,7 +496,7 @@ func (s *service) webhook(action domain.Action, release domain.Release) {
req, err := http.NewRequest(http.MethodPost, action.WebhookHost, bytes.NewBufferString(dataArgs))
if err != nil {
log.Error().Err(err).Msgf("webhook client request error: %v", action.WebhookHost)
s.log.Error().Err(err).Msgf("webhook client request error: %v", action.WebhookHost)
return
}
@ -507,13 +505,13 @@ func (s *service) webhook(action domain.Action, release domain.Release) {
res, err := client.Do(req)
if err != nil {
log.Error().Err(err).Msgf("webhook client request error: %v", action.WebhookHost)
s.log.Error().Err(err).Msgf("webhook client request error: %v", action.WebhookHost)
return
}
defer res.Body.Close()
log.Info().Msgf("successfully ran webhook action: '%v' to: %v payload: %v", action.Name, action.WebhookHost, dataArgs)
s.log.Info().Msgf("successfully ran webhook action: '%v' to: %v payload: %v", action.Name, action.WebhookHost, dataArgs)
return
}

View file

@ -7,6 +7,7 @@ import (
"github.com/autobrr/autobrr/internal/domain"
"github.com/autobrr/autobrr/internal/download_client"
"github.com/autobrr/autobrr/internal/logger"
)
type Service interface {
@ -22,13 +23,19 @@ type Service interface {
}
type service struct {
log logger.Logger
repo domain.ActionRepo
clientSvc download_client.Service
bus EventBus.Bus
}
func NewService(repo domain.ActionRepo, clientSvc download_client.Service, bus EventBus.Bus) Service {
return &service{repo: repo, clientSvc: clientSvc, bus: bus}
func NewService(log logger.Logger, repo domain.ActionRepo, clientSvc download_client.Service, bus EventBus.Bus) Service {
return &service{
log: log,
repo: repo,
clientSvc: clientSvc,
bus: bus,
}
}
func (s *service) Store(ctx context.Context, action domain.Action) (*domain.Action, error) {

View file

@ -6,19 +6,17 @@ import (
"github.com/autobrr/autobrr/internal/domain"
"github.com/autobrr/autobrr/pkg/sonarr"
"github.com/rs/zerolog/log"
)
func (s *service) sonarr(release domain.Release, action domain.Action) ([]string, error) {
log.Trace().Msg("action SONARR")
s.log.Trace().Msg("action SONARR")
// TODO validate data
// get client for action
client, err := s.clientSvc.FindByID(context.TODO(), action.ClientID)
if err != nil {
log.Error().Err(err).Msgf("sonarr: error finding client: %v", action.ClientID)
s.log.Error().Err(err).Msgf("sonarr: error finding client: %v", action.ClientID)
return nil, err
}
@ -54,17 +52,17 @@ func (s *service) sonarr(release domain.Release, action domain.Action) ([]string
rejections, err := arr.Push(r)
if err != nil {
log.Error().Stack().Err(err).Msgf("sonarr: failed to push release: %v", r)
s.log.Error().Stack().Err(err).Msgf("sonarr: failed to push release: %v", r)
return nil, err
}
if rejections != nil {
log.Debug().Msgf("sonarr: release push rejected: %v, indexer %v to %v reasons: '%v'", r.Title, r.Indexer, client.Host, rejections)
s.log.Debug().Msgf("sonarr: release push rejected: %v, indexer %v to %v reasons: '%v'", r.Title, r.Indexer, client.Host, rejections)
return rejections, nil
}
log.Debug().Msgf("sonarr: successfully pushed release: %v, indexer %v to %v", r.Title, r.Indexer, client.Host)
s.log.Debug().Msgf("sonarr: successfully pushed release: %v, indexer %v to %v", r.Title, r.Indexer, client.Host)
return nil, nil
}

View file

@ -6,19 +6,17 @@ import (
"github.com/autobrr/autobrr/internal/domain"
"github.com/autobrr/autobrr/pkg/whisparr"
"github.com/rs/zerolog/log"
)
func (s *service) whisparr(release domain.Release, action domain.Action) ([]string, error) {
log.Trace().Msg("action WHISPARR")
s.log.Trace().Msg("action WHISPARR")
// TODO validate data
// get client for action
client, err := s.clientSvc.FindByID(context.TODO(), action.ClientID)
if err != nil {
log.Error().Err(err).Msgf("whisparr: error finding client: %v", action.ClientID)
s.log.Error().Err(err).Msgf("whisparr: error finding client: %v", action.ClientID)
return nil, err
}
@ -54,17 +52,17 @@ func (s *service) whisparr(release domain.Release, action domain.Action) ([]stri
rejections, err := arr.Push(r)
if err != nil {
log.Error().Stack().Err(err).Msgf("whisparr: failed to push release: %v", r)
s.log.Error().Stack().Err(err).Msgf("whisparr: failed to push release: %v", r)
return nil, err
}
if rejections != nil {
log.Debug().Msgf("whisparr: release push rejected: %v, indexer %v to %v reasons: '%v'", r.Title, r.Indexer, client.Host, rejections)
s.log.Debug().Msgf("whisparr: release push rejected: %v, indexer %v to %v reasons: '%v'", r.Title, r.Indexer, client.Host, rejections)
return rejections, nil
}
log.Debug().Msgf("whisparr: successfully pushed release: %v, indexer %v to %v", r.Title, r.Indexer, client.Host)
s.log.Debug().Msgf("whisparr: successfully pushed release: %v, indexer %v to %v", r.Title, r.Indexer, client.Host)
return nil, nil
}