mirror of
https://github.com/idanoo/autobrr
synced 2025-07-23 16:59:12 +00:00
Fix: Performance issues and sqlite locking (#74)
* fix: performance issues and sqlite locking * fix: dashboard release stats was reversed * refactor: open and migrate db * chore: cleanup
This commit is contained in:
parent
d8c37dde2f
commit
f466657ed4
25 changed files with 362 additions and 658 deletions
|
@ -1,6 +1,7 @@
|
|||
package action
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/base64"
|
||||
"errors"
|
||||
"io/ioutil"
|
||||
|
@ -18,7 +19,7 @@ func (s *service) deluge(action domain.Action, torrentFile string) error {
|
|||
var err error
|
||||
|
||||
// get client for action
|
||||
client, err := s.clientSvc.FindByID(action.ClientID)
|
||||
client, err := s.clientSvc.FindByID(context.TODO(), action.ClientID)
|
||||
if err != nil {
|
||||
log.Error().Stack().Err(err).Msgf("error finding client: %v", action.ClientID)
|
||||
return err
|
||||
|
@ -56,7 +57,7 @@ func (s *service) delugeCheckRulesCanDownload(action domain.Action) (bool, error
|
|||
log.Trace().Msgf("action Deluge: %v check rules", action.Name)
|
||||
|
||||
// get client for action
|
||||
client, err := s.clientSvc.FindByID(action.ClientID)
|
||||
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)
|
||||
return false, err
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
package action
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"github.com/autobrr/autobrr/internal/domain"
|
||||
|
@ -15,7 +16,7 @@ func (s *service) lidarr(release domain.Release, action domain.Action) error {
|
|||
// TODO validate data
|
||||
|
||||
// get client for action
|
||||
client, err := s.clientSvc.FindByID(action.ClientID)
|
||||
client, err := s.clientSvc.FindByID(context.TODO(), action.ClientID)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msgf("error finding client: %v", action.ClientID)
|
||||
return err
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
package action
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
|
@ -12,36 +13,9 @@ import (
|
|||
const REANNOUNCE_MAX_ATTEMPTS = 30
|
||||
const REANNOUNCE_INTERVAL = 7000
|
||||
|
||||
func (s *service) qbittorrent(action domain.Action, hash string, torrentFile string) error {
|
||||
func (s *service) qbittorrent(qbt *qbittorrent.Client, action domain.Action, hash string, torrentFile string) error {
|
||||
log.Debug().Msgf("action qBittorrent: %v", action.Name)
|
||||
|
||||
// get client for action
|
||||
client, err := s.clientSvc.FindByID(action.ClientID)
|
||||
if err != nil {
|
||||
log.Error().Stack().Err(err).Msgf("error finding client: %v ID %v", action.Name, action.ClientID)
|
||||
return err
|
||||
}
|
||||
|
||||
if client == nil {
|
||||
return err
|
||||
}
|
||||
|
||||
qbtSettings := qbittorrent.Settings{
|
||||
Hostname: client.Host,
|
||||
Port: uint(client.Port),
|
||||
Username: client.Username,
|
||||
Password: client.Password,
|
||||
SSL: client.SSL,
|
||||
}
|
||||
|
||||
qbt := qbittorrent.NewClient(qbtSettings)
|
||||
// save cookies?
|
||||
err = qbt.Login()
|
||||
if err != nil {
|
||||
log.Error().Stack().Err(err).Msgf("error logging into client: %v %v", client.Name, client.Host)
|
||||
return err
|
||||
}
|
||||
|
||||
options := map[string]string{}
|
||||
|
||||
if action.Paused {
|
||||
|
@ -66,9 +40,9 @@ func (s *service) qbittorrent(action domain.Action, hash string, torrentFile str
|
|||
|
||||
log.Trace().Msgf("action qBittorrent options: %+v", options)
|
||||
|
||||
err = qbt.AddTorrentFromFile(torrentFile, options)
|
||||
err := qbt.AddTorrentFromFile(torrentFile, options)
|
||||
if err != nil {
|
||||
log.Error().Stack().Err(err).Msgf("could not add torrent %v to client: %v", torrentFile, client.Name)
|
||||
log.Error().Stack().Err(err).Msgf("could not add torrent %v to client: %v", torrentFile, qbt.Name)
|
||||
return err
|
||||
}
|
||||
|
||||
|
@ -80,23 +54,23 @@ func (s *service) qbittorrent(action domain.Action, hash string, torrentFile str
|
|||
}
|
||||
}
|
||||
|
||||
log.Info().Msgf("torrent with hash %v successfully added to client: '%v'", hash, client.Name)
|
||||
log.Info().Msgf("torrent with hash %v successfully added to client: '%v'", hash, qbt.Name)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *service) qbittorrentCheckRulesCanDownload(action domain.Action) (bool, error) {
|
||||
func (s *service) qbittorrentCheckRulesCanDownload(action domain.Action) (bool, *qbittorrent.Client, error) {
|
||||
log.Trace().Msgf("action qBittorrent: %v check rules", action.Name)
|
||||
|
||||
// get client for action
|
||||
client, err := s.clientSvc.FindByID(action.ClientID)
|
||||
client, err := s.clientSvc.FindByID(context.TODO(), action.ClientID)
|
||||
if err != nil {
|
||||
log.Error().Stack().Err(err).Msgf("error finding client: %v", action.ClientID)
|
||||
return false, err
|
||||
return false, nil, err
|
||||
}
|
||||
|
||||
if client == nil {
|
||||
return false, err
|
||||
return false, nil, err
|
||||
}
|
||||
|
||||
qbtSettings := qbittorrent.Settings{
|
||||
|
@ -108,11 +82,12 @@ func (s *service) qbittorrentCheckRulesCanDownload(action domain.Action) (bool,
|
|||
}
|
||||
|
||||
qbt := qbittorrent.NewClient(qbtSettings)
|
||||
qbt.Name = client.Name
|
||||
// save cookies?
|
||||
err = qbt.Login()
|
||||
if err != nil {
|
||||
log.Error().Stack().Err(err).Msgf("error logging into client: %v", client.Host)
|
||||
return false, err
|
||||
return false, nil, err
|
||||
}
|
||||
|
||||
// check for active downloads and other rules
|
||||
|
@ -120,7 +95,7 @@ func (s *service) qbittorrentCheckRulesCanDownload(action domain.Action) (bool,
|
|||
activeDownloads, err := qbt.GetTorrentsFilter(qbittorrent.TorrentFilterDownloading)
|
||||
if err != nil {
|
||||
log.Error().Stack().Err(err).Msg("could not fetch downloading torrents")
|
||||
return false, err
|
||||
return false, nil, err
|
||||
}
|
||||
|
||||
// make sure it's not set to 0 by default
|
||||
|
@ -133,14 +108,14 @@ func (s *service) qbittorrentCheckRulesCanDownload(action domain.Action) (bool,
|
|||
info, err := qbt.GetTransferInfo()
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msg("could not get transfer info")
|
||||
return false, err
|
||||
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")
|
||||
return false, nil
|
||||
return false, nil, nil
|
||||
}
|
||||
|
||||
log.Debug().Msg("active downloads are slower than set limit, lets add it")
|
||||
|
@ -149,7 +124,7 @@ func (s *service) qbittorrentCheckRulesCanDownload(action domain.Action) (bool,
|
|||
}
|
||||
}
|
||||
|
||||
return true, nil
|
||||
return true, qbt, nil
|
||||
}
|
||||
|
||||
func checkTrackerStatus(qb qbittorrent.Client, hash string) error {
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
package action
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"github.com/autobrr/autobrr/internal/domain"
|
||||
|
@ -15,7 +16,7 @@ func (s *service) radarr(release domain.Release, action domain.Action) error {
|
|||
// TODO validate data
|
||||
|
||||
// get client for action
|
||||
client, err := s.clientSvc.FindByID(action.ClientID)
|
||||
client, err := s.clientSvc.FindByID(context.TODO(), action.ClientID)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msgf("error finding client: %v", action.ClientID)
|
||||
return err
|
||||
|
|
|
@ -102,7 +102,7 @@ func (s *service) RunActions(actions []domain.Action, release domain.Release) er
|
|||
}(action, tmpFile)
|
||||
|
||||
case domain.ActionTypeQbittorrent:
|
||||
canDownload, err := s.qbittorrentCheckRulesCanDownload(action)
|
||||
canDownload, client, err := s.qbittorrentCheckRulesCanDownload(action)
|
||||
if err != nil {
|
||||
log.Error().Stack().Err(err).Msgf("error checking client rules: %v", action.Name)
|
||||
continue
|
||||
|
@ -131,7 +131,7 @@ func (s *service) RunActions(actions []domain.Action, release domain.Release) er
|
|||
}
|
||||
|
||||
go func(action domain.Action, hash string, tmpFile string) {
|
||||
err = s.qbittorrent(action, hash, tmpFile)
|
||||
err = s.qbittorrent(client, action, hash, tmpFile)
|
||||
if err != nil {
|
||||
log.Error().Stack().Err(err).Msg("error sending torrent to qBittorrent")
|
||||
}
|
||||
|
@ -206,7 +206,7 @@ func (s *service) CheckCanDownload(actions []domain.Action) bool {
|
|||
return true
|
||||
|
||||
case domain.ActionTypeQbittorrent:
|
||||
canDownload, err := s.qbittorrentCheckRulesCanDownload(action)
|
||||
canDownload, _, err := s.qbittorrentCheckRulesCanDownload(action)
|
||||
if err != nil {
|
||||
log.Error().Stack().Err(err).Msgf("error checking client rules: %v", action.Name)
|
||||
continue
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
package action
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"github.com/autobrr/autobrr/internal/domain"
|
||||
|
@ -15,7 +16,7 @@ func (s *service) sonarr(release domain.Release, action domain.Action) error {
|
|||
// TODO validate data
|
||||
|
||||
// get client for action
|
||||
client, err := s.clientSvc.FindByID(action.ClientID)
|
||||
client, err := s.clientSvc.FindByID(context.TODO(), action.ClientID)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msgf("error finding client: %v", action.ClientID)
|
||||
return err
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue