Skip to content

Commit

Permalink
Retry on ConnectionError (#740)
Browse files Browse the repository at this point in the history
Add Retries for connection to qbit
  • Loading branch information
NooNameR authored Feb 2, 2025
1 parent cae7461 commit a2add1d
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 9 deletions.
30 changes: 21 additions & 9 deletions modules/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -754,20 +754,32 @@ def _sort_share_limits(share_limits):
# Connect to Qbittorrent
self.qbt = None
if "qbt" in self.data:
logger.info("Connecting to Qbittorrent...")
self.qbt = Qbt(
self,
{
"host": self.util.check_for_attribute(self.data, "host", parent="qbt", throw=True),
"username": self.util.check_for_attribute(self.data, "user", parent="qbt", default_is_none=True),
"password": self.util.check_for_attribute(self.data, "pass", parent="qbt", default_is_none=True),
},
)
self.qbt = self.__connect()
else:
e = "Config Error: qbt attribute not found"
self.notify(e, "Config")
raise Failed(e)

def __retry_on_connect(exception):
return isinstance(exception.__cause__, ConnectionError)

@retry(
retry_on_exception=__retry_on_connect,
stop_max_attempt_number=5,
wait_exponential_multiplier=30000,
wait_exponential_max=120000,
)
def __connect(self):
logger.info("Connecting to Qbittorrent...")
return Qbt(
self,
{
"host": self.util.check_for_attribute(self.data, "host", parent="qbt", throw=True),
"username": self.util.check_for_attribute(self.data, "user", parent="qbt", default_is_none=True),
"password": self.util.check_for_attribute(self.data, "pass", parent="qbt", default_is_none=True),
},
)

# Empty old files from recycle bin or orphaned
def cleanup_dirs(self, location):
num_del = 0
Expand Down
4 changes: 4 additions & 0 deletions modules/qbittorrent.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from fnmatch import fnmatch
from functools import cache

from qbittorrentapi import APIConnectionError
from qbittorrentapi import Client
from qbittorrentapi import LoginFailed
from qbittorrentapi import NotFound404Error
Expand Down Expand Up @@ -77,6 +78,9 @@ def __init__(self, config, params):
ex = "Qbittorrent Error: Failed to login. Invalid username/password."
self.config.notify(ex, "Qbittorrent")
raise Failed(ex)
except APIConnectionError as exc:
self.config.notify(exc, "Qbittorrent")
raise Failed(exc) from ConnectionError(exc)
except Exception as exc:
self.config.notify(exc, "Qbittorrent")
raise Failed(exc)
Expand Down

0 comments on commit a2add1d

Please sign in to comment.