fix(actions): reject if client is disabled (#1626)

* fix(actions): error on disabled client

* fix(actions): sql scan args

* refactor: download client cache for actions

* fix: tests client store

* fix: tests client store and int conversion

* fix: tests revert findbyid ctx timeout

* fix: tests row.err

* feat: add logging to download client cache
This commit is contained in:
ze0s 2024-08-27 19:45:06 +02:00 committed by GitHub
parent 77e1c2c305
commit 861f30c144
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
30 changed files with 928 additions and 680 deletions

View file

@ -7,7 +7,6 @@ import (
"context"
"encoding/base64"
"os"
"time"
"github.com/autobrr/autobrr/internal/domain"
"github.com/autobrr/autobrr/pkg/errors"
@ -20,20 +19,18 @@ func (s *service) deluge(ctx context.Context, action *domain.Action, release dom
var err error
// get client for action
client, err := s.clientSvc.FindByID(ctx, action.ClientID)
client, err := s.clientSvc.GetClient(ctx, action.ClientID)
if err != nil {
s.log.Error().Stack().Err(err).Msgf("error finding client: %d", action.ClientID)
return nil, err
return nil, errors.Wrap(err, "could not get client with id %d", action.ClientID)
}
if client == nil {
return nil, errors.New("could not find client by id: %d", action.ClientID)
if !client.Enabled {
return nil, errors.New("client %s %s not enabled", client.Type, client.Name)
}
var rejections []string
switch client.Type {
switch action.Client.Type {
case "DELUGE_V1":
rejections, err = s.delugeV1(ctx, client, action, release)
@ -90,27 +87,18 @@ func (s *service) delugeCheckRulesCanDownload(ctx context.Context, del deluge.De
}
func (s *service) delugeV1(ctx context.Context, client *domain.DownloadClient, action *domain.Action, release domain.Release) ([]string, error) {
settings := deluge.Settings{
Hostname: client.Host,
Port: uint(client.Port),
Login: client.Username,
Password: client.Password,
DebugServerResponses: true,
ReadWriteTimeout: time.Second * 30,
}
del := deluge.NewV1(settings)
downloadClient := client.Client.(*deluge.Client)
// perform connection to Deluge server
err := del.Connect(ctx)
err := downloadClient.Connect(ctx)
if err != nil {
return nil, errors.Wrap(err, "could not connect to client %s at %s", client.Name, client.Host)
}
defer del.Close()
defer downloadClient.Close()
// perform connection to Deluge server
rejections, err := s.delugeCheckRulesCanDownload(ctx, del, client, action)
rejections, err := s.delugeCheckRulesCanDownload(ctx, downloadClient, client, action)
if err != nil {
s.log.Error().Err(err).Msgf("error checking client rules: %s", action.Name)
return nil, err
@ -127,13 +115,13 @@ func (s *service) delugeV1(ctx context.Context, client *domain.DownloadClient, a
s.log.Trace().Msgf("action Deluge options: %+v", options)
torrentHash, err := del.AddTorrentMagnet(ctx, release.MagnetURI, &options)
torrentHash, err := downloadClient.AddTorrentMagnet(ctx, release.MagnetURI, &options)
if err != nil {
return nil, errors.Wrap(err, "could not add torrent magnet %s to client: %s", release.MagnetURI, client.Name)
}
if action.Label != "" {
labelPluginActive, err := del.LabelPlugin(ctx)
labelPluginActive, err := downloadClient.LabelPlugin(ctx)
if err != nil {
return nil, errors.Wrap(err, "could not load label plugin for client: %s", client.Name)
}
@ -176,13 +164,13 @@ func (s *service) delugeV1(ctx context.Context, client *domain.DownloadClient, a
s.log.Trace().Msgf("action Deluge options: %+v", options)
torrentHash, err := del.AddTorrentFile(ctx, release.TorrentTmpFile, encodedFile, &options)
torrentHash, err := downloadClient.AddTorrentFile(ctx, 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 := del.LabelPlugin(ctx)
labelPluginActive, err := downloadClient.LabelPlugin(ctx)
if err != nil {
return nil, errors.Wrap(err, "could not load label plugin for client: %s", client.Name)
}
@ -203,27 +191,18 @@ func (s *service) delugeV1(ctx context.Context, client *domain.DownloadClient, a
}
func (s *service) delugeV2(ctx context.Context, client *domain.DownloadClient, action *domain.Action, release domain.Release) ([]string, error) {
settings := deluge.Settings{
Hostname: client.Host,
Port: uint(client.Port),
Login: client.Username,
Password: client.Password,
DebugServerResponses: true,
ReadWriteTimeout: time.Second * 30,
}
del := deluge.NewV2(settings)
downloadClient := client.Client.(*deluge.ClientV2)
// perform connection to Deluge server
err := del.Connect(ctx)
err := downloadClient.Connect(ctx)
if err != nil {
return nil, errors.Wrap(err, "could not connect to client %s at %s", client.Name, client.Host)
}
defer del.Close()
defer downloadClient.Close()
// perform connection to Deluge server
rejections, err := s.delugeCheckRulesCanDownload(ctx, del, client, action)
rejections, err := s.delugeCheckRulesCanDownload(ctx, downloadClient, client, action)
if err != nil {
s.log.Error().Err(err).Msgf("error checking client rules: %s", action.Name)
return nil, err
@ -240,13 +219,13 @@ func (s *service) delugeV2(ctx context.Context, client *domain.DownloadClient, a
s.log.Trace().Msgf("action Deluge options: %+v", options)
torrentHash, err := del.AddTorrentMagnet(ctx, release.MagnetURI, &options)
torrentHash, err := downloadClient.AddTorrentMagnet(ctx, release.MagnetURI, &options)
if err != nil {
return nil, errors.Wrap(err, "could not add torrent magnet %s to client: %s", release.MagnetURI, client.Name)
}
if action.Label != "" {
labelPluginActive, err := del.LabelPlugin(ctx)
labelPluginActive, err := downloadClient.LabelPlugin(ctx)
if err != nil {
return nil, errors.Wrap(err, "could not load label plugin for client: %s", client.Name)
}
@ -290,13 +269,13 @@ func (s *service) delugeV2(ctx context.Context, client *domain.DownloadClient, a
s.log.Trace().Msgf("action Deluge options: %+v", options)
torrentHash, err := del.AddTorrentFile(ctx, release.TorrentTmpFile, encodedFile, &options)
torrentHash, err := downloadClient.AddTorrentFile(ctx, release.TorrentTmpFile, encodedFile, &options)
if err != nil {
return nil, errors.Wrap(err, "could not add torrent %s to client: %s", release.TorrentTmpFile, client.Name)
}
if action.Label != "" {
labelPluginActive, err := del.LabelPlugin(ctx)
labelPluginActive, err := downloadClient.LabelPlugin(ctx)
if err != nil {
return nil, errors.Wrap(err, "could not load label plugin for client: %s", client.Name)
}