feat(actions): improve errors and logs (#340)

This commit is contained in:
Ludvig Lundgren 2022-07-07 16:28:14 +02:00 committed by GitHub
parent 31fbe013ff
commit 402596523d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
15 changed files with 315 additions and 418 deletions

View file

@ -3,16 +3,16 @@ package action
import ( import (
"context" "context"
"encoding/base64" "encoding/base64"
"errors"
"io/ioutil" "io/ioutil"
"time" "time"
"github.com/autobrr/autobrr/internal/domain" "github.com/autobrr/autobrr/internal/domain"
"github.com/autobrr/autobrr/pkg/errors"
delugeClient "github.com/gdm85/go-libdeluge" delugeClient "github.com/gdm85/go-libdeluge"
) )
func (s *service) deluge(action domain.Action, release domain.Release) error { func (s *service) deluge(action domain.Action, release domain.Release) ([]string, error) {
s.log.Debug().Msgf("action Deluge: %v", action.Name) s.log.Debug().Msgf("action Deluge: %v", action.Name)
var err error var err error
@ -21,84 +21,34 @@ func (s *service) deluge(action domain.Action, release domain.Release) error {
client, err := s.clientSvc.FindByID(context.TODO(), action.ClientID) client, err := s.clientSvc.FindByID(context.TODO(), action.ClientID)
if err != nil { if err != nil {
s.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 return nil, err
} }
if client == nil { if client == nil {
return errors.New("no client found") return nil, errors.New("could not find client by id: %v", action.ClientID)
} }
settings := delugeClient.Settings{ var rejections []string
Hostname: client.Host,
Port: uint(client.Port),
Login: client.Username,
Password: client.Password,
DebugServerResponses: true,
ReadWriteTimeout: time.Second * 20,
}
switch client.Type { switch client.Type {
case "DELUGE_V1": case "DELUGE_V1":
if err = s.delugeV1(client, settings, action, release); err != nil { rejections, err = s.delugeV1(client, action, release)
return err
}
case "DELUGE_V2": case "DELUGE_V2":
if err = s.delugeV2(client, settings, action, release); err != nil { rejections, err = s.delugeV2(client, action, release)
return err
}
} }
return nil return rejections, err
} }
func (s *service) delugeCheckRulesCanDownload(action domain.Action) (bool, error) { func (s *service) delugeCheckRulesCanDownload(deluge delugeClient.DelugeClient, client *domain.DownloadClient, action domain.Action) ([]string, error) {
s.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 {
s.log.Error().Stack().Err(err).Msgf("error finding client: %v ID %v", action.Name, action.ClientID)
return false, err
}
if client == nil {
return false, errors.New("no client found")
}
settings := delugeClient.Settings{
Hostname: client.Host,
Port: uint(client.Port),
Login: client.Username,
Password: client.Password,
DebugServerResponses: true,
ReadWriteTimeout: time.Second * 20,
}
var deluge delugeClient.DelugeClient
switch client.Type {
case "DELUGE_V1":
deluge = delugeClient.NewV1(settings)
case "DELUGE_V2":
deluge = delugeClient.NewV2(settings)
}
// perform connection to Deluge server
err = deluge.Connect()
if err != nil {
s.log.Error().Stack().Err(err).Msgf("error logging into client: %v %v", client.Name, client.Host)
return false, err
}
defer deluge.Close()
// 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 := deluge.TorrentsStatus(delugeClient.StateDownloading, nil) activeDownloads, err := deluge.TorrentsStatus(delugeClient.StateDownloading, nil)
if err != nil { if err != nil {
s.log.Error().Stack().Err(err).Msg("Deluge - could not fetch downloading torrents") return nil, errors.Wrap(err, "could not fetch downloading torrents")
return false, err
} }
// make sure it's not set to 0 by default // make sure it's not set to 0 by default
@ -107,7 +57,9 @@ func (s *service) delugeCheckRulesCanDownload(action domain.Action) (bool, error
// if max active downloads reached, check speed and if lower than threshold add anyways // if max active downloads reached, check speed and if lower than threshold add anyways
if len(activeDownloads) >= client.Settings.Rules.MaxActiveDownloads { if len(activeDownloads) >= client.Settings.Rules.MaxActiveDownloads {
s.log.Debug().Msg("max active downloads reached, skipping") s.log.Debug().Msg("max active downloads reached, skipping")
return false, nil
rejections := []string{"max active downloads reached, skipping"}
return rejections, nil
// // TODO handle ignore slow torrents // // TODO handle ignore slow torrents
//if client.Settings.Rules.IgnoreSlowTorrents { //if client.Settings.Rules.IgnoreSlowTorrents {
@ -131,132 +83,177 @@ func (s *service) delugeCheckRulesCanDownload(action domain.Action) (bool, error
} }
} }
return true, nil return nil, nil
} }
func (s *service) delugeV1(client *domain.DownloadClient, settings delugeClient.Settings, action domain.Action, release domain.Release) error { func (s *service) delugeV1(client *domain.DownloadClient, action domain.Action, release domain.Release) ([]string, error) {
settings := delugeClient.Settings{
Hostname: client.Host,
Port: uint(client.Port),
Login: client.Username,
Password: client.Password,
DebugServerResponses: true,
ReadWriteTimeout: time.Second * 20,
}
deluge := delugeClient.NewV1(settings) deluge := delugeClient.NewV1(settings)
// perform connection to Deluge server // perform connection to Deluge server
err := deluge.Connect() err := deluge.Connect()
if err != nil { if err != nil {
s.log.Error().Stack().Err(err).Msgf("error logging into client: %v %v", client.Name, client.Host) return nil, errors.Wrap(err, "could not connect to client %v at %v", client.Name, client.Host)
return err
} }
defer deluge.Close() defer deluge.Close()
// perform connection to Deluge server
rejections, err := s.delugeCheckRulesCanDownload(deluge, client, action)
if err != nil {
s.log.Error().Err(err).Msgf("error checking client rules: %v", action.Name)
return nil, err
}
if rejections != nil {
return rejections, nil
}
if release.TorrentTmpFile == "" {
err = release.DownloadTorrentFile()
if err != nil {
s.log.Error().Err(err).Msgf("could not download torrent file for release: %v", release.TorrentName)
return nil, err
}
}
t, err := ioutil.ReadFile(release.TorrentTmpFile) t, err := ioutil.ReadFile(release.TorrentTmpFile)
if err != nil { if err != nil {
s.log.Error().Stack().Err(err).Msgf("could not read torrent file: %v", release.TorrentTmpFile) return nil, errors.Wrap(err, "could not read torrent file: %v", release.TorrentTmpFile)
return err
} }
// encode file to base64 before sending to deluge // encode file to base64 before sending to deluge
encodedFile := base64.StdEncoding.EncodeToString(t) encodedFile := base64.StdEncoding.EncodeToString(t)
if encodedFile == "" { if encodedFile == "" {
s.log.Error().Stack().Err(err).Msgf("could not encode torrent file: %v", release.TorrentTmpFile) return nil, errors.Wrap(err, "could not encode torrent file: %v", release.TorrentTmpFile)
return err
} }
// set options
options := delugeClient.Options{}
// macros handle args and replace vars // macros handle args and replace vars
m := NewMacro(release) m := NewMacro(release)
if action.Paused { options, err := s.prepareDelugeOptions(action, m)
options.AddPaused = &action.Paused
}
if action.SavePath != "" {
// parse and replace values in argument string before continuing
savePathArgs, err := m.Parse(action.SavePath)
if err != nil { if err != nil {
s.log.Error().Stack().Err(err).Msgf("could not parse macro: %v", action.SavePath) return nil, errors.Wrap(err, "could not prepare options")
return err
}
options.DownloadLocation = &savePathArgs
}
if action.LimitDownloadSpeed > 0 {
maxDL := int(action.LimitDownloadSpeed)
options.MaxDownloadSpeed = &maxDL
}
if action.LimitUploadSpeed > 0 {
maxUL := int(action.LimitUploadSpeed)
options.MaxUploadSpeed = &maxUL
} }
s.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) torrentHash, err := deluge.AddTorrentFile(release.TorrentTmpFile, encodedFile, &options)
if err != nil { if err != nil {
s.log.Error().Stack().Err(err).Msgf("could not add torrent %v to client: %v", release.TorrentTmpFile, client.Name) return nil, errors.Wrap(err, "could not add torrent %v to client: %v", release.TorrentTmpFile, client.Name)
return err
} }
if action.Label != "" { if action.Label != "" {
p, err := deluge.LabelPlugin() labelPluginActive, err := deluge.LabelPlugin()
if err != nil { if err != nil {
s.log.Error().Stack().Err(err).Msgf("could not load label plugin: %v", client.Name) return nil, errors.Wrap(err, "could not load label plugin for client: %v", client.Name)
return err
} }
// parse and replace values in argument string before continuing // parse and replace values in argument string before continuing
labelArgs, err := m.Parse(action.Label) labelArgs, err := m.Parse(action.Label)
if err != nil { if err != nil {
s.log.Error().Stack().Err(err).Msgf("could not parse macro: %v", action.Label) return nil, errors.Wrap(err, "could not parse macro label: %v", action.Label)
return err
} }
if p != nil { if labelPluginActive != nil {
// TODO first check if label exists, if not, add it, otherwise set // TODO first check if label exists, if not, add it, otherwise set
err = p.SetTorrentLabel(torrentHash, labelArgs) err = labelPluginActive.SetTorrentLabel(torrentHash, labelArgs)
if err != nil { if err != nil {
s.log.Error().Stack().Err(err).Msgf("could not set label: %v on client: %v", action.Label, client.Name) return nil, errors.Wrap(err, "could not set label: %v on client: %v", action.Label, client.Name)
return err
} }
} }
} }
s.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 return nil, nil
} }
func (s *service) delugeV2(client *domain.DownloadClient, settings delugeClient.Settings, action domain.Action, release domain.Release) error { func (s *service) delugeV2(client *domain.DownloadClient, action domain.Action, release domain.Release) ([]string, error) {
settings := delugeClient.Settings{
Hostname: client.Host,
Port: uint(client.Port),
Login: client.Username,
Password: client.Password,
DebugServerResponses: true,
ReadWriteTimeout: time.Second * 20,
}
deluge := delugeClient.NewV2(settings) deluge := delugeClient.NewV2(settings)
// perform connection to Deluge server // perform connection to Deluge server
err := deluge.Connect() err := deluge.Connect()
if err != nil { if err != nil {
s.log.Error().Stack().Err(err).Msgf("error logging into client: %v %v", client.Name, client.Host) return nil, errors.Wrap(err, "could not connect to client %v at %v", client.Name, client.Host)
return err
} }
defer deluge.Close() defer deluge.Close()
t, err := ioutil.ReadFile(release.TorrentTmpFile) t, err := ioutil.ReadFile(release.TorrentTmpFile)
if err != nil { if err != nil {
s.log.Error().Stack().Err(err).Msgf("could not read torrent file: %v", release.TorrentTmpFile) return nil, errors.Wrap(err, "could not read torrent file: %v", release.TorrentTmpFile)
return err
} }
// encode file to base64 before sending to deluge // encode file to base64 before sending to deluge
encodedFile := base64.StdEncoding.EncodeToString(t) encodedFile := base64.StdEncoding.EncodeToString(t)
if encodedFile == "" { if encodedFile == "" {
s.log.Error().Stack().Err(err).Msgf("could not encode torrent file: %v", release.TorrentTmpFile) return nil, errors.Wrap(err, "could not encode torrent file: %v", release.TorrentTmpFile)
return err
} }
// set options
options := delugeClient.Options{}
// macros handle args and replace vars // macros handle args and replace vars
m := NewMacro(release) m := NewMacro(release)
// set options
options, err := s.prepareDelugeOptions(action, m)
if err != nil {
return nil, errors.Wrap(err, "could not prepare options")
}
s.log.Trace().Msgf("action Deluge options: %+v", options)
torrentHash, err := deluge.AddTorrentFile(release.TorrentTmpFile, encodedFile, &options)
if err != nil {
return nil, errors.Wrap(err, "could not add torrent %v to client: %v", release.TorrentTmpFile, client.Name)
}
if action.Label != "" {
labelPluginActive, err := deluge.LabelPlugin()
if err != nil {
return nil, errors.Wrap(err, "could not load label plugin for client: %v", client.Name)
}
// parse and replace values in argument string before continuing
labelArgs, err := m.Parse(action.Label)
if err != nil {
return nil, errors.Wrap(err, "could not parse macro label: %v", action.Label)
}
if labelPluginActive != nil {
// TODO first check if label exists, if not, add it, otherwise set
err = labelPluginActive.SetTorrentLabel(torrentHash, labelArgs)
if err != nil {
return nil, errors.Wrap(err, "could not set label: %v on client: %v", action.Label, client.Name)
}
}
}
s.log.Info().Msgf("torrent with hash %v successfully added to client: '%v'", torrentHash, client.Name)
return nil, nil
}
func (s *service) prepareDelugeOptions(action domain.Action, m Macro) (delugeClient.Options, error) {
// set options
options := delugeClient.Options{}
if action.Paused { if action.Paused {
options.AddPaused = &action.Paused options.AddPaused = &action.Paused
} }
@ -264,8 +261,7 @@ func (s *service) delugeV2(client *domain.DownloadClient, settings delugeClient.
// parse and replace values in argument string before continuing // parse and replace values in argument string before continuing
savePathArgs, err := m.Parse(action.SavePath) savePathArgs, err := m.Parse(action.SavePath)
if err != nil { if err != nil {
s.log.Error().Stack().Err(err).Msgf("could not parse macro: %v", action.SavePath) return options, errors.Wrap(err, "could not parse save path macro: %v", action.SavePath)
return err
} }
options.DownloadLocation = &savePathArgs options.DownloadLocation = &savePathArgs
@ -279,39 +275,5 @@ func (s *service) delugeV2(client *domain.DownloadClient, settings delugeClient.
options.MaxUploadSpeed = &maxUL options.MaxUploadSpeed = &maxUL
} }
s.log.Trace().Msgf("action Deluge options: %+v", options) return options, nil
torrentHash, err := deluge.AddTorrentFile(release.TorrentTmpFile, encodedFile, &options)
if err != nil {
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 {
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 {
s.log.Error().Stack().Err(err).Msgf("could not parse macro: %v", action.Label)
return err
}
if p != nil {
// TODO first check if label exists, if not, add it, otherwise set
err = p.SetTorrentLabel(torrentHash, labelArgs)
if err != nil {
s.log.Error().Stack().Err(err).Msgf("could not set label: %v on client: %v", action.Label, client.Name)
return err
}
}
}
s.log.Info().Msgf("torrent with hash %v successfully added to client: '%v'", torrentHash, client.Name)
return nil
} }

View file

@ -4,25 +4,30 @@ import (
"os/exec" "os/exec"
"time" "time"
"github.com/mattn/go-shellwords"
"github.com/autobrr/autobrr/internal/domain" "github.com/autobrr/autobrr/internal/domain"
"github.com/autobrr/autobrr/pkg/errors"
"github.com/mattn/go-shellwords"
) )
func (s *service) execCmd(release domain.Release, action domain.Action) { func (s *service) execCmd(action domain.Action, release domain.Release) error {
s.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)
if release.TorrentTmpFile == "" {
if err := release.DownloadTorrentFile(); err != nil {
return errors.Wrap(err, "error downloading torrent file for release: %v", release.TorrentName)
}
}
// check if program exists // check if program exists
cmd, err := exec.LookPath(action.ExecCmd) cmd, err := exec.LookPath(action.ExecCmd)
if err != nil { if err != nil {
s.log.Error().Stack().Err(err).Msgf("exec failed, could not find program: %v", action.ExecCmd) return errors.Wrap(err, "exec failed, could not find program: %v", action.ExecCmd)
return
} }
args, err := s.parseExecArgs(release, action.ExecArgs) args, err := s.parseExecArgs(release, action.ExecArgs)
if err != nil { if err != nil {
s.log.Error().Stack().Err(err).Msgf("parsing args failed: command: %v args: %v torrent: %v", cmd, action.ExecArgs, release.TorrentTmpFile) return errors.Wrap(err, "could not parse exec args: %v", action.ExecArgs)
return
} }
// we need to split on space into a string slice, so we can spread the args into exec // we need to split on space into a string slice, so we can spread the args into exec
@ -36,8 +41,7 @@ func (s *service) execCmd(release domain.Release, action domain.Action) {
output, err := command.CombinedOutput() output, err := command.CombinedOutput()
if err != nil { if err != nil {
// everything other than exit 0 is considered an error // everything other than exit 0 is considered an error
s.log.Error().Stack().Err(err).Msgf("command: %v args: %v failed, torrent: %v", cmd, args, release.TorrentTmpFile) return errors.Wrap(err, "error executing command: %v args: %v", cmd, args)
return
} }
s.log.Trace().Msgf("executed command: '%v'", string(output)) s.log.Trace().Msgf("executed command: '%v'", string(output))
@ -45,6 +49,8 @@ func (s *service) execCmd(release domain.Release, action domain.Action) {
duration := time.Since(start) duration := time.Since(start)
s.log.Info().Msgf("executed command: '%v', args: '%v' %v,%v, total time %v", cmd, args, release.TorrentName, release.Indexer, duration) s.log.Info().Msgf("executed command: '%v', args: '%v' %v,%v, total time %v", cmd, args, release.TorrentName, release.Indexer, duration)
return nil
} }
func (s *service) parseExecArgs(release domain.Release, execArgs string) ([]string, error) { func (s *service) parseExecArgs(release domain.Release, execArgs string) ([]string, error) {
@ -54,14 +60,14 @@ func (s *service) parseExecArgs(release domain.Release, execArgs string) ([]stri
// parse and replace values in argument string before continuing // parse and replace values in argument string before continuing
parsedArgs, err := m.Parse(execArgs) parsedArgs, err := m.Parse(execArgs)
if err != nil { if err != nil {
return nil, err return nil, errors.Wrap(err, "could not parse macro")
} }
p := shellwords.NewParser() p := shellwords.NewParser()
p.ParseBacktick = true p.ParseBacktick = true
args, err := p.Parse(parsedArgs) args, err := p.Parse(parsedArgs)
if err != nil { if err != nil {
return nil, err return nil, errors.Wrap(err, "could not parse into shell-words")
} }
return args, nil return args, nil

View file

@ -108,7 +108,7 @@ func Test_service_execCmd(t *testing.T) {
clientSvc: nil, clientSvc: nil,
bus: nil, bus: nil,
} }
s.execCmd(tt.args.release, tt.args.action) s.execCmd(tt.args.action, tt.args.release)
}) })
} }
} }

View file

@ -6,10 +6,11 @@ import (
"time" "time"
"github.com/autobrr/autobrr/internal/domain" "github.com/autobrr/autobrr/internal/domain"
"github.com/autobrr/autobrr/pkg/errors"
"github.com/autobrr/autobrr/pkg/lidarr" "github.com/autobrr/autobrr/pkg/lidarr"
) )
func (s *service) lidarr(release domain.Release, action domain.Action) ([]string, error) { func (s *service) lidarr(action domain.Action, release domain.Release) ([]string, error) {
s.log.Trace().Msg("action LIDARR") s.log.Trace().Msg("action LIDARR")
// TODO validate data // TODO validate data
@ -23,7 +24,7 @@ func (s *service) lidarr(release domain.Release, action domain.Action) ([]string
// return early if no client found // return early if no client found
if client == nil { if client == nil {
return nil, err return nil, errors.New("could not find client by id: %v", action.ClientID)
} }
// initial config // initial config

View file

@ -7,6 +7,7 @@ import (
"time" "time"
"github.com/autobrr/autobrr/internal/domain" "github.com/autobrr/autobrr/internal/domain"
"github.com/autobrr/autobrr/pkg/errors"
) )
type Macro struct { type Macro struct {
@ -65,13 +66,13 @@ func (m Macro) Parse(text string) (string, error) {
// setup template // setup template
tmpl, err := template.New("macro").Parse(text) tmpl, err := template.New("macro").Parse(text)
if err != nil { if err != nil {
return "", err return "", errors.Wrap(err, "could parse macro template")
} }
var tpl bytes.Buffer var tpl bytes.Buffer
err = tmpl.Execute(&tpl, m) err = tmpl.Execute(&tpl, m)
if err != nil { if err != nil {
return "", err return "", errors.Wrap(err, "could not parse macro")
} }
return tpl.String(), nil return tpl.String(), nil

View file

@ -6,6 +6,7 @@ import (
"time" "time"
"github.com/autobrr/autobrr/internal/domain" "github.com/autobrr/autobrr/internal/domain"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
) )

View file

@ -7,101 +7,28 @@ import (
"time" "time"
"github.com/autobrr/autobrr/internal/domain" "github.com/autobrr/autobrr/internal/domain"
"github.com/autobrr/autobrr/pkg/errors"
"github.com/autobrr/autobrr/pkg/qbittorrent" "github.com/autobrr/autobrr/pkg/qbittorrent"
) )
const ReannounceMaxAttempts = 50 const ReannounceMaxAttempts = 50
const ReannounceInterval = 7000 const ReannounceInterval = 7000
func (s *service) qbittorrent(qbt *qbittorrent.Client, action domain.Action, release domain.Release) error { func (s *service) qbittorrent(action domain.Action, release domain.Release) ([]string, error) {
s.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)
options := map[string]string{}
if action.Paused {
options["paused"] = "true"
}
if action.SavePath != "" {
// parse and replace values in argument string before continuing
actionArgs, err := m.Parse(action.SavePath)
if err != nil {
s.log.Error().Stack().Err(err).Msgf("could not parse macro: %v", action.SavePath)
return err
}
options["savepath"] = actionArgs
options["autoTMM"] = "false"
}
if action.Category != "" {
// parse and replace values in argument string before continuing
categoryArgs, err := m.Parse(action.Category)
if err != nil {
s.log.Error().Stack().Err(err).Msgf("could not parse macro: %v", action.Category)
return err
}
options["category"] = categoryArgs
}
if action.Tags != "" {
// parse and replace values in argument string before continuing
tagsArgs, err := m.Parse(action.Tags)
if err != nil {
s.log.Error().Stack().Err(err).Msgf("could not parse macro: %v", action.Tags)
return err
}
options["tags"] = tagsArgs
}
if action.LimitUploadSpeed > 0 {
options["upLimit"] = strconv.FormatInt(action.LimitUploadSpeed*1000, 10)
}
if action.LimitDownloadSpeed > 0 {
options["dlLimit"] = strconv.FormatInt(action.LimitDownloadSpeed*1000, 10)
}
if action.LimitRatio > 0 {
options["ratioLimit"] = strconv.FormatFloat(action.LimitRatio, 'r', 2, 64)
}
if action.LimitSeedTime > 0 {
options["seedingTimeLimit"] = strconv.FormatInt(action.LimitSeedTime, 10)
}
s.log.Trace().Msgf("action qBittorrent options: %+v", options)
err := qbt.AddTorrentFromFile(release.TorrentTmpFile, options)
if err != nil {
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 != "" {
if err := s.reannounceTorrent(qbt, action, release.TorrentHash); err != nil {
s.log.Error().Stack().Err(err).Msgf("could not reannounce torrent: %v", release.TorrentHash)
return err
}
}
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) {
s.log.Trace().Msgf("action qBittorrent: %v check rules", action.Name)
// get client for action // get client for action
client, err := s.clientSvc.FindByID(context.TODO(), action.ClientID) client, err := s.clientSvc.FindByID(context.TODO(), action.ClientID)
if err != nil { if err != nil {
s.log.Error().Stack().Err(err).Msgf("error finding client: %v", action.ClientID) return nil, errors.Wrap(err, "error finding client: %v", action.ClientID)
return false, nil, err
} }
if client == nil { if client == nil {
return false, nil, err return nil, errors.New("could not find client by id: %v", action.ClientID)
} }
qbt, exists := s.qbitClients[qbitKey{client.ID, client.Name}]
if !exists {
qbtSettings := qbittorrent.Settings{ qbtSettings := qbittorrent.Settings{
Hostname: client.Host, Hostname: client.Host,
Port: uint(client.Port), Port: uint(client.Port),
@ -119,21 +46,120 @@ func (s *service) qbittorrentCheckRulesCanDownload(action domain.Action) (bool,
qbtSettings.Basic.Password = client.Settings.Basic.Password qbtSettings.Basic.Password = client.Settings.Basic.Password
} }
qbt := qbittorrent.NewClient(qbtSettings) qbt = qbittorrent.NewClient(qbtSettings)
qbt.Name = client.Name qbt.Name = client.Name
// save cookies?
err = qbt.Login() s.qbitClients[qbitKey{client.ID, client.Name}] = qbt
if err != nil {
s.log.Error().Stack().Err(err).Msgf("error logging into client: %v", client.Host) if err = qbt.Login(); err != nil {
return false, nil, err return nil, errors.Wrap(err, "could not log into client: %v at %v", client.Name, client.Host)
} }
}
if qbt == nil {
return nil, errors.New("qbit client does not exist")
}
rejections, err := s.qbittorrentCheckRulesCanDownload(action, client, qbt)
if err != nil {
return nil, errors.Wrap(err, "error checking client rules: %v", action.Name)
}
if rejections != nil {
return rejections, nil
}
if release.TorrentTmpFile == "" {
err = release.DownloadTorrentFile()
if err != nil {
return nil, errors.Wrap(err, "error downloading torrent file for release: %v", release.TorrentName)
}
}
// macros handle args and replace vars
m := NewMacro(release)
options, err := s.prepareQbitOptions(action, m)
if err != nil {
return nil, errors.Wrap(err, "could not prepare options")
}
s.log.Trace().Msgf("action qBittorrent options: %+v", options)
if err = qbt.AddTorrentFromFile(release.TorrentTmpFile, options); err != nil {
return nil, errors.Wrap(err, "could not add torrent %v to client: %v", release.TorrentTmpFile, qbt.Name)
}
if !action.Paused && !action.ReAnnounceSkip && release.TorrentHash != "" {
if err := s.reannounceTorrent(qbt, action, release.TorrentHash); err != nil {
return nil, errors.Wrap(err, "could not reannounce torrent: %v", release.TorrentHash)
}
}
s.log.Info().Msgf("torrent with hash %v successfully added to client: '%v'", release.TorrentHash, qbt.Name)
return nil, nil
}
func (s *service) prepareQbitOptions(action domain.Action, m Macro) (map[string]string, error) {
options := map[string]string{}
if action.Paused {
options["paused"] = "true"
}
if action.SavePath != "" {
// parse and replace values in argument string before continuing
actionArgs, err := m.Parse(action.SavePath)
if err != nil {
return nil, errors.Wrap(err, "could not parse savepath macro: %v", action.SavePath)
}
options["savepath"] = actionArgs
options["autoTMM"] = "false"
}
if action.Category != "" {
// parse and replace values in argument string before continuing
categoryArgs, err := m.Parse(action.Category)
if err != nil {
return nil, errors.Wrap(err, "could not parse category macro: %v", action.Category)
}
options["category"] = categoryArgs
}
if action.Tags != "" {
// parse and replace values in argument string before continuing
tagsArgs, err := m.Parse(action.Tags)
if err != nil {
return nil, errors.Wrap(err, "could not parse tags macro: %v", action.Tags)
}
options["tags"] = tagsArgs
}
if action.LimitUploadSpeed > 0 {
options["upLimit"] = strconv.FormatInt(action.LimitUploadSpeed*1000, 10)
}
if action.LimitDownloadSpeed > 0 {
options["dlLimit"] = strconv.FormatInt(action.LimitDownloadSpeed*1000, 10)
}
if action.LimitRatio > 0 {
options["ratioLimit"] = strconv.FormatFloat(action.LimitRatio, 'r', 2, 64)
}
if action.LimitSeedTime > 0 {
options["seedingTimeLimit"] = strconv.FormatInt(action.LimitSeedTime, 10)
}
return options, nil
}
func (s *service) qbittorrentCheckRulesCanDownload(action domain.Action, client *domain.DownloadClient, qbt *qbittorrent.Client) ([]string, error) {
s.log.Trace().Msgf("action qBittorrent: %v check rules", action.Name)
// 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.GetTorrentsActiveDownloads() activeDownloads, err := qbt.GetTorrentsActiveDownloads()
if err != nil { if err != nil {
s.log.Error().Stack().Err(err).Msg("could not fetch downloading torrents") return nil, errors.Wrap(err, "could not fetch active downloads")
return false, nil, err
} }
// make sure it's not set to 0 by default // make sure it's not set to 0 by default
@ -145,27 +171,30 @@ func (s *service) qbittorrentCheckRulesCanDownload(action domain.Action) (bool,
// check speeds of downloads // check speeds of downloads
info, err := qbt.GetTransferInfo() info, err := qbt.GetTransferInfo()
if err != nil { if err != nil {
s.log.Error().Err(err).Msg("could not get transfer info") return nil, errors.Wrap(err, "could not get transfer info")
return false, nil, err
} }
// if current transfer speed is more than threshold return out and skip // if current transfer speed is more than threshold return out and skip
// DlInfoSpeed is in bytes so lets convert to KB to match DownloadSpeedThreshold // DlInfoSpeed is in bytes so lets convert to KB to match DownloadSpeedThreshold
if info.DlInfoSpeed/1024 >= client.Settings.Rules.DownloadSpeedThreshold { if info.DlInfoSpeed/1024 >= client.Settings.Rules.DownloadSpeedThreshold {
s.log.Debug().Msg("max active downloads reached, skipping") s.log.Debug().Msg("max active downloads reached, skipping")
return false, nil, nil
rejections := []string{"max active downloads reached, skipping"}
return rejections, nil
} }
s.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 { } else {
s.log.Debug().Msg("max active downloads reached, skipping") s.log.Debug().Msg("max active downloads reached, skipping")
return false, nil, nil
rejections := []string{"max active downloads reached, skipping"}
return rejections, nil
} }
} }
} }
} }
return true, qbt, nil return nil, nil
} }
func (s *service) reannounceTorrent(qb *qbittorrent.Client, action domain.Action, hash string) error { func (s *service) reannounceTorrent(qb *qbittorrent.Client, action domain.Action, hash string) error {
@ -190,8 +219,7 @@ func (s *service) reannounceTorrent(qb *qbittorrent.Client, action domain.Action
trackers, err := qb.GetTorrentTrackers(hash) trackers, err := qb.GetTorrentTrackers(hash)
if err != nil { if err != nil {
s.log.Error().Err(err).Msgf("qBittorrent - could not get trackers for torrent: %v", hash) return errors.Wrap(err, "could not get trackers for torrent with hash: %v", hash)
return err
} }
if trackers == nil { if trackers == nil {
@ -215,8 +243,7 @@ func (s *service) reannounceTorrent(qb *qbittorrent.Client, action domain.Action
s.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}) err = qb.ReAnnounceTorrents([]string{hash})
if err != nil { if err != nil {
s.log.Error().Err(err).Msgf("qBittorrent - could not get re-announce torrent: %v", hash) return errors.Wrap(err, "could not re-announce torrent with hash: %v", hash)
return err
} }
attempts++ attempts++
@ -228,8 +255,7 @@ func (s *service) reannounceTorrent(qb *qbittorrent.Client, action domain.Action
err := qb.DeleteTorrents([]string{hash}, false) err := qb.DeleteTorrents([]string{hash}, false)
if err != nil { if err != nil {
s.log.Error().Stack().Err(err).Msgf("qBittorrent - could not delete torrent: %v", hash) return errors.Wrap(err, "could not delete torrent with hash: %v", hash)
return err
} }
} }

View file

@ -5,10 +5,11 @@ import (
"time" "time"
"github.com/autobrr/autobrr/internal/domain" "github.com/autobrr/autobrr/internal/domain"
"github.com/autobrr/autobrr/pkg/errors"
"github.com/autobrr/autobrr/pkg/radarr" "github.com/autobrr/autobrr/pkg/radarr"
) )
func (s *service) radarr(release domain.Release, action domain.Action) ([]string, error) { func (s *service) radarr(action domain.Action, release domain.Release) ([]string, error) {
s.log.Trace().Msg("action RADARR") s.log.Trace().Msg("action RADARR")
// TODO validate data // TODO validate data
@ -16,13 +17,12 @@ func (s *service) radarr(release domain.Release, action domain.Action) ([]string
// get client for action // get client for action
client, err := s.clientSvc.FindByID(context.TODO(), action.ClientID) client, err := s.clientSvc.FindByID(context.TODO(), action.ClientID)
if err != nil { if err != nil {
s.log.Error().Err(err).Msgf("radarr: error finding client: %v", action.ClientID) return nil, errors.Wrap(err, "error finding client: %v", action.ClientID)
return nil, err
} }
// return early if no client found // return early if no client found
if client == nil { if client == nil {
return nil, err return nil, errors.New("could not find client by id: %v", action.ClientID)
} }
// initial config // initial config
@ -53,8 +53,7 @@ func (s *service) radarr(release domain.Release, action domain.Action) ([]string
rejections, err := arr.Push(r) rejections, err := arr.Push(r)
if err != nil { if err != nil {
s.log.Error().Stack().Err(err).Msgf("radarr: failed to push release: %v", r) return nil, errors.Wrap(err, "radarr failed to push release: %v", r)
return nil, err
} }
if rejections != nil { if rejections != nil {

View file

@ -10,122 +10,46 @@ import (
"time" "time"
"github.com/autobrr/autobrr/internal/domain" "github.com/autobrr/autobrr/internal/domain"
"github.com/autobrr/autobrr/pkg/errors"
) )
func (s *service) RunAction(action *domain.Action, release domain.Release) ([]string, error) { func (s *service) RunAction(action *domain.Action, release domain.Release) ([]string, error) {
var err error var (
var rejections []string err error
rejections []string
)
switch action.Type { switch action.Type {
case domain.ActionTypeTest: case domain.ActionTypeTest:
s.test(action.Name) s.test(action.Name)
case domain.ActionTypeExec: case domain.ActionTypeExec:
if release.TorrentTmpFile == "" { err = s.execCmd(*action, release)
if err := release.DownloadTorrentFile(); err != nil {
s.log.Error().Stack().Err(err)
break
}
}
s.execCmd(release, *action)
case domain.ActionTypeWatchFolder: case domain.ActionTypeWatchFolder:
if release.TorrentTmpFile == "" { err = s.watchFolder(*action, release)
if err := release.DownloadTorrentFile(); err != nil {
s.log.Error().Stack().Err(err)
break
}
}
s.watchFolder(*action, release)
case domain.ActionTypeWebhook: case domain.ActionTypeWebhook:
if release.TorrentTmpFile == "" { err = s.webhook(*action, release)
if err := release.DownloadTorrentFile(); err != nil {
s.log.Error().Stack().Err(err)
break
}
}
s.webhook(*action, release)
case domain.ActionTypeDelugeV1, domain.ActionTypeDelugeV2: case domain.ActionTypeDelugeV1, domain.ActionTypeDelugeV2:
canDownload, err := s.delugeCheckRulesCanDownload(*action) rejections, err = s.deluge(*action, release)
if err != nil {
s.log.Error().Stack().Err(err).Msgf("error checking client rules: %v", action.Name)
break
}
if !canDownload {
rejections = []string{"max active downloads reached, skipping"}
break
}
if release.TorrentTmpFile == "" {
if err := release.DownloadTorrentFile(); err != nil {
s.log.Error().Stack().Err(err)
break
}
}
err = s.deluge(*action, release)
if err != nil {
s.log.Error().Stack().Err(err).Msg("error sending torrent to Deluge")
break
}
case domain.ActionTypeQbittorrent: case domain.ActionTypeQbittorrent:
canDownload, client, err := s.qbittorrentCheckRulesCanDownload(*action) rejections, err = s.qbittorrent(*action, release)
if err != nil {
s.log.Error().Stack().Err(err).Msgf("error checking client rules: %v", action.Name)
break
}
if !canDownload {
rejections = []string{"max active downloads reached, skipping"}
break
}
if release.TorrentTmpFile == "" {
if err := release.DownloadTorrentFile(); err != nil {
s.log.Error().Stack().Err(err)
break
}
}
err = s.qbittorrent(client, *action, release)
if err != nil {
s.log.Error().Stack().Err(err).Msg("error sending torrent to qBittorrent")
break
}
case domain.ActionTypeRadarr: case domain.ActionTypeRadarr:
rejections, err = s.radarr(release, *action) rejections, err = s.radarr(*action, release)
if err != nil {
s.log.Error().Stack().Err(err).Msg("error sending torrent to radarr")
break
}
case domain.ActionTypeSonarr: case domain.ActionTypeSonarr:
rejections, err = s.sonarr(release, *action) rejections, err = s.sonarr(*action, release)
if err != nil {
s.log.Error().Stack().Err(err).Msg("error sending torrent to sonarr")
break
}
case domain.ActionTypeLidarr: case domain.ActionTypeLidarr:
rejections, err = s.lidarr(release, *action) rejections, err = s.lidarr(*action, release)
if err != nil {
s.log.Error().Stack().Err(err).Msg("error sending torrent to lidarr")
break
}
case domain.ActionTypeWhisparr: case domain.ActionTypeWhisparr:
rejections, err = s.whisparr(release, *action) rejections, err = s.whisparr(*action, release)
if err != nil {
s.log.Error().Stack().Err(err).Msg("error sending torrent to whisparr")
break
}
default: default:
s.log.Warn().Msgf("unsupported action type: %v", action.Type) s.log.Warn().Msgf("unsupported action type: %v", action.Type)
@ -161,7 +85,7 @@ func (s *service) RunAction(action *domain.Action, release domain.Release) ([]st
} }
if err != nil { if err != nil {
s.log.Err(err).Stack().Msgf("process action failed: %v for '%v'", action.Name, release.TorrentName) s.log.Error().Err(err).Msgf("process action failed: %v for '%v'", action.Name, release.TorrentName)
rlsActionStatus.Status = domain.ReleasePushStatusErr rlsActionStatus.Status = domain.ReleasePushStatusErr
rlsActionStatus.Rejections = []string{err.Error()} rlsActionStatus.Rejections = []string{err.Error()}
@ -189,56 +113,23 @@ func (s *service) RunAction(action *domain.Action, release domain.Release) ([]st
return rejections, err return rejections, err
} }
func (s *service) CheckCanDownload(actions []domain.Action) bool {
for _, action := range actions {
if !action.Enabled {
// only run active actions
continue
}
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 {
s.log.Error().Stack().Err(err).Msgf("error checking client rules: %v", action.Name)
continue
}
if !canDownload {
continue
}
return true
case domain.ActionTypeQbittorrent:
canDownload, _, err := s.qbittorrentCheckRulesCanDownload(action)
if err != nil {
s.log.Error().Stack().Err(err).Msgf("error checking client rules: %v", action.Name)
continue
}
if !canDownload {
continue
}
return true
}
}
return false
}
func (s *service) test(name string) { func (s *service) test(name string) {
s.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) { func (s *service) watchFolder(action domain.Action, release domain.Release) error {
if release.TorrentTmpFile == "" {
if err := release.DownloadTorrentFile(); err != nil {
return errors.Wrap(err, "watch folder: could not download torrent file for release: %v", release.TorrentName)
}
}
m := NewMacro(release) m := NewMacro(release)
// parse and replace values in argument string before continuing // parse and replace values in argument string before continuing
watchFolderArgs, err := m.Parse(action.WatchFolder) watchFolderArgs, err := m.Parse(action.WatchFolder)
if err != nil { if err != nil {
s.log.Error().Stack().Err(err).Msgf("could not parse macro: %v", action.WatchFolder) return errors.Wrap(err, "could not parse watch folder macro: %v", action.WatchFolder)
} }
s.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)
@ -246,8 +137,7 @@ func (s *service) watchFolder(action domain.Action, release domain.Release) {
// Open original file // Open original file
original, err := os.Open(release.TorrentTmpFile) original, err := os.Open(release.TorrentTmpFile)
if err != nil { if err != nil {
s.log.Error().Stack().Err(err).Msgf("could not open temp file '%v'", release.TorrentTmpFile) return errors.Wrap(err, "could not open temp file: %v", release.TorrentTmpFile)
return
} }
defer original.Close() defer original.Close()
@ -257,29 +147,34 @@ func (s *service) watchFolder(action domain.Action, release domain.Release) {
// Create new file // Create new file
newFile, err := os.Create(fullFileName) newFile, err := os.Create(fullFileName)
if err != nil { if err != nil {
s.log.Error().Stack().Err(err).Msgf("could not create new temp file '%v'", fullFileName) return errors.Wrap(err, "could not create new file %v", fullFileName)
return
} }
defer newFile.Close() defer newFile.Close()
// Copy file // Copy file
_, err = io.Copy(newFile, original) _, err = io.Copy(newFile, original)
if err != nil { if err != nil {
s.log.Error().Stack().Err(err).Msgf("could not copy file %v to watch folder", fullFileName) return errors.Wrap(err, "could not copy file %v to watch folder", fullFileName)
return
} }
s.log.Info().Msgf("saved file to watch folder: %v", fullFileName) s.log.Info().Msgf("saved file to watch folder: %v", fullFileName)
return nil
} }
func (s *service) webhook(action domain.Action, release domain.Release) { func (s *service) webhook(action domain.Action, release domain.Release) error {
if release.TorrentTmpFile == "" {
if err := release.DownloadTorrentFile(); err != nil {
return errors.Wrap(err, "webhook: could not download torrent file for release: %v", release.TorrentName)
}
}
m := NewMacro(release) m := NewMacro(release)
// parse and replace values in argument string before continuing // parse and replace values in argument string before continuing
dataArgs, err := m.Parse(action.WebhookData) dataArgs, err := m.Parse(action.WebhookData)
if err != nil { if err != nil {
s.log.Error().Stack().Err(err).Msgf("could not parse macro: %v", action.WebhookData) return errors.Wrap(err, "could not parse webhook data macro: %v", action.WebhookData)
return
} }
s.log.Trace().Msgf("action WEBHOOK: '%v' file: %v", action.Name, release.TorrentName) s.log.Trace().Msgf("action WEBHOOK: '%v' file: %v", action.Name, release.TorrentName)
@ -295,8 +190,7 @@ func (s *service) webhook(action domain.Action, release domain.Release) {
req, err := http.NewRequest(http.MethodPost, action.WebhookHost, bytes.NewBufferString(dataArgs)) req, err := http.NewRequest(http.MethodPost, action.WebhookHost, bytes.NewBufferString(dataArgs))
if err != nil { if err != nil {
s.log.Error().Err(err).Msgf("webhook client request error: %v", action.WebhookHost) return errors.Wrap(err, "could not build request for webhook")
return
} }
req.Header.Set("Content-Type", "application/json") req.Header.Set("Content-Type", "application/json")
@ -304,13 +198,12 @@ func (s *service) webhook(action domain.Action, release domain.Release) {
res, err := client.Do(req) res, err := client.Do(req)
if err != nil { if err != nil {
s.log.Error().Err(err).Msgf("webhook client request error: %v", action.WebhookHost) return errors.Wrap(err, "could not make request for webhook")
return
} }
defer res.Body.Close() defer res.Body.Close()
s.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 return nil
} }

View file

@ -7,6 +7,7 @@ import (
"github.com/autobrr/autobrr/internal/domain" "github.com/autobrr/autobrr/internal/domain"
"github.com/autobrr/autobrr/internal/download_client" "github.com/autobrr/autobrr/internal/download_client"
"github.com/autobrr/autobrr/internal/logger" "github.com/autobrr/autobrr/internal/logger"
"github.com/autobrr/autobrr/pkg/qbittorrent"
"github.com/asaskevich/EventBus" "github.com/asaskevich/EventBus"
"github.com/dcarbone/zadapters/zstdlog" "github.com/dcarbone/zadapters/zstdlog"
@ -21,7 +22,11 @@ type Service interface {
ToggleEnabled(actionID int) error ToggleEnabled(actionID int) error
RunAction(action *domain.Action, release domain.Release) ([]string, error) RunAction(action *domain.Action, release domain.Release) ([]string, error)
CheckCanDownload(actions []domain.Action) bool }
type qbitKey struct {
I int // type
N string // name
} }
type service struct { type service struct {
@ -30,6 +35,8 @@ type service struct {
repo domain.ActionRepo repo domain.ActionRepo
clientSvc download_client.Service clientSvc download_client.Service
bus EventBus.Bus bus EventBus.Bus
qbitClients map[qbitKey]*qbittorrent.Client
} }
func NewService(log logger.Logger, repo domain.ActionRepo, clientSvc download_client.Service, bus EventBus.Bus) Service { func NewService(log logger.Logger, repo domain.ActionRepo, clientSvc download_client.Service, bus EventBus.Bus) Service {
@ -38,6 +45,7 @@ func NewService(log logger.Logger, repo domain.ActionRepo, clientSvc download_cl
repo: repo, repo: repo,
clientSvc: clientSvc, clientSvc: clientSvc,
bus: bus, bus: bus,
qbitClients: map[qbitKey]*qbittorrent.Client{},
} }
s.subLogger = zstdlog.NewStdLoggerWithLevel(s.log.With().Logger(), zerolog.TraceLevel) s.subLogger = zstdlog.NewStdLoggerWithLevel(s.log.With().Logger(), zerolog.TraceLevel)

View file

@ -9,7 +9,7 @@ import (
"github.com/autobrr/autobrr/pkg/sonarr" "github.com/autobrr/autobrr/pkg/sonarr"
) )
func (s *service) sonarr(release domain.Release, action domain.Action) ([]string, error) { func (s *service) sonarr(action domain.Action, release domain.Release) ([]string, error) {
s.log.Trace().Msg("action SONARR") s.log.Trace().Msg("action SONARR")
// TODO validate data // TODO validate data
@ -17,7 +17,6 @@ func (s *service) sonarr(release domain.Release, action domain.Action) ([]string
// get client for action // get client for action
client, err := s.clientSvc.FindByID(context.TODO(), action.ClientID) client, err := s.clientSvc.FindByID(context.TODO(), action.ClientID)
if err != nil { if err != nil {
s.log.Error().Err(err).Msgf("sonarr: error finding client: %v", action.ClientID)
return nil, errors.Wrap(err, "sonarr could not find client: %v", action.ClientID) return nil, errors.Wrap(err, "sonarr could not find client: %v", action.ClientID)
} }
@ -54,8 +53,7 @@ func (s *service) sonarr(release domain.Release, action domain.Action) ([]string
rejections, err := arr.Push(r) rejections, err := arr.Push(r)
if err != nil { if err != nil {
s.log.Error().Stack().Err(err).Msgf("sonarr: failed to push release: %v", r) return nil, errors.Wrap(err, "sonarr: failed to push release: %v", r)
return nil, err
} }
if rejections != nil { if rejections != nil {

View file

@ -5,10 +5,11 @@ import (
"time" "time"
"github.com/autobrr/autobrr/internal/domain" "github.com/autobrr/autobrr/internal/domain"
"github.com/autobrr/autobrr/pkg/errors"
"github.com/autobrr/autobrr/pkg/whisparr" "github.com/autobrr/autobrr/pkg/whisparr"
) )
func (s *service) whisparr(release domain.Release, action domain.Action) ([]string, error) { func (s *service) whisparr(action domain.Action, release domain.Release) ([]string, error) {
s.log.Trace().Msg("action WHISPARR") s.log.Trace().Msg("action WHISPARR")
// TODO validate data // TODO validate data
@ -16,13 +17,12 @@ func (s *service) whisparr(release domain.Release, action domain.Action) ([]stri
// get client for action // get client for action
client, err := s.clientSvc.FindByID(context.TODO(), action.ClientID) client, err := s.clientSvc.FindByID(context.TODO(), action.ClientID)
if err != nil { if err != nil {
s.log.Error().Err(err).Msgf("whisparr: error finding client: %v", action.ClientID) return nil, errors.Wrap(err, "sonarr could not find client: %v", action.ClientID)
return nil, err
} }
// return early if no client found // return early if no client found
if client == nil { if client == nil {
return nil, err return nil, errors.New("could not find client by id: %v", action.ClientID)
} }
// initial config // initial config
@ -53,8 +53,7 @@ func (s *service) whisparr(release domain.Release, action domain.Action) ([]stri
rejections, err := arr.Push(r) rejections, err := arr.Push(r)
if err != nil { if err != nil {
s.log.Error().Stack().Err(err).Msgf("whisparr: failed to push release: %v", r) return nil, errors.Wrap(err, "whisparr: failed to push release: %v", r)
return nil, err
} }
if rejections != nil { if rejections != nil {

View file

@ -318,17 +318,17 @@ func (r *Release) DownloadTorrentFile() error {
// Write the body to file // Write the body to file
_, err = io.Copy(tmpFile, resp.Body) _, err = io.Copy(tmpFile, resp.Body)
if err != nil { if err != nil {
return errors.Wrap(err, fmt.Sprintf("error writing downloaded file: %v", tmpFile.Name())) return errors.Wrap(err, "error writing downloaded file: %v", tmpFile.Name())
} }
meta, err := metainfo.LoadFromFile(tmpFile.Name()) meta, err := metainfo.LoadFromFile(tmpFile.Name())
if err != nil { if err != nil {
return errors.Wrap(err, fmt.Sprintf("metainfo could not load file contents: %v", tmpFile.Name())) return errors.Wrap(err, "metainfo could not load file contents: %v", tmpFile.Name())
} }
torrentMetaInfo, err := meta.UnmarshalInfo() torrentMetaInfo, err := meta.UnmarshalInfo()
if err != nil { if err != nil {
return errors.Wrap(err, fmt.Sprintf("metainfo could not unmarshal info from torrent: %v", tmpFile.Name())) return errors.Wrap(err, "metainfo could not unmarshal info from torrent: %v", tmpFile.Name())
} }
r.TorrentTmpFile = tmpFile.Name() r.TorrentTmpFile = tmpFile.Name()
@ -337,8 +337,6 @@ func (r *Release) DownloadTorrentFile() error {
// remove file if fail // remove file if fail
//log.Debug().Msgf("successfully downloaded file: %v", tmpFile.Name())
return nil return nil
} }

View file

@ -57,11 +57,16 @@ func (s *service) testQbittorrentConnection(client domain.DownloadClient) error
} }
qbt := qbittorrent.NewClient(qbtSettings) qbt := qbittorrent.NewClient(qbtSettings)
err := qbt.Login()
if err != nil { if err := qbt.Login(); err != nil {
return errors.Wrap(err, "error logging into client: %v", client.Host) return errors.Wrap(err, "error logging into client: %v", client.Host)
} }
_, err := qbt.GetTorrents()
if err != nil {
return errors.Wrap(err, "error getting torrents: %v", client.Host)
}
s.log.Debug().Msgf("test client connection for qBittorrent: success") s.log.Debug().Msgf("test client connection for qBittorrent: success")
return nil return nil

View file

@ -113,7 +113,7 @@ func (s *service) Test(client domain.DownloadClient) error {
// test // test
if err := s.testConnection(client); err != nil { if err := s.testConnection(client); err != nil {
s.log.Err(err).Msg("client connection test error") s.log.Error().Err(err).Msg("client connection test error")
return err return err
} }