Skip to content

Commit

Permalink
Only attempt to connect automatically when explicitly configured to d…
Browse files Browse the repository at this point in the history
…o so
  • Loading branch information
rmartin16 committed Aug 26, 2021
1 parent 60c23e3 commit e34440a
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 18 deletions.
41 changes: 25 additions & 16 deletions qbittorrentui/daemon.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import threading
from copy import deepcopy
from time import time
from abc import ABC, abstractmethod

from qbittorrentui._vendored.attrdict import AttrDict
from qbittorrentui.config import config
Expand Down Expand Up @@ -185,7 +186,7 @@ def __init__(self):
self.connection_failure_reported = False


class Daemon(threading.Thread):
class Daemon(threading.Thread, ABC):
"""
Base class for background daemons to send and receive data/commands with server.
Expand All @@ -195,24 +196,29 @@ class Daemon(threading.Thread):
def __init__(self, torrent_client: Connector):
super(Daemon, self).__init__()
self.setDaemon(daemonic=True)
self.setName(self.__class__.__name__)
self.stop_request = threading.Event()
self.wake_up = threading.Event()
self.reset = threading.Event()

self._loop_interval = int(config.get("DAEMON_LOOP_INTERVAL"))
self._loop_success = False
self._daemon_name = self.__class__.__name__

self.client = torrent_client

reset_daemons.connect(receiver=self.reset_signal)

def reset_signal(self, *a):
self.reset.set()

@abstractmethod
def reset_daemon(self):
pass

@abstractmethod
def _one_loop(self):
pass

def reset_signal(self, *a):
self.reset.set()

def stop(self, *a):
self.stop_request.set()
self.set_wake_up(*a)
Expand Down Expand Up @@ -240,22 +246,19 @@ def run(self):
logger.info(
"Daemon %s could not connect to server", self.__class__.__name__
)
connection_to_server_status.send(f"{self._daemon_name}", success=False)
connection_to_server_status.send(f"{self.name}", success=False)
except Exception:
logger.info("Daemon %s crashed", self._daemon_name, exc_info=True)
logger.info("Daemon %s crashed", self.name, exc_info=True)
finally:
if self._loop_success:
connection_to_server_status.send(self._daemon_name, success=True)
connection_to_server_status.send(self.name, success=True)
self._loop_success = False
# wait for next loop
poll_time = time() - start_time
if poll_time < self._loop_interval:
self.wake_up.wait(self._loop_interval - poll_time)

logger.info("Daemon %s exiting", self._daemon_name)

def _one_loop(self):
pass
logger.info("Daemon %s exiting", self.name)


class SyncMainData(Daemon):
Expand Down Expand Up @@ -290,7 +293,7 @@ def _one_loop(self):
self._rid = 0

def reset_daemon(self):
logger.info("%s is resetting", self._daemon_name)
logger.info("%s is resetting", self.name)
self._rid = 0
while not self.maindata_q.empty():
self.maindata_q.get()
Expand Down Expand Up @@ -332,7 +335,7 @@ def _one_loop(self):
self._loop_success = True

def reset_daemon(self):
logger.info("%s is resetting", self._daemon_name)
logger.info("%s is resetting", self.name)
self._rid = {}
self._torrent_hashes = []
self._torrent_stores = {}
Expand Down Expand Up @@ -483,6 +486,9 @@ def _one_loop(self):

self._loop_success = True

def reset_daemon(self):
logger.info("%s is resetting", self.name)

def get_server_preferences(self):
self._server_preferences_lock.acquire()
prefs = deepcopy(self._server_preferences)
Expand Down Expand Up @@ -546,8 +552,11 @@ def _one_loop(self):
# request server sync if commands were issued
if ran_commands:
self._loop_success = True
update_torrent_list_now.send("%s daemon" % self._daemon_name)
update_torrent_window_now.send("%s daemon" % self._daemon_name)
update_torrent_list_now.send("%s daemon" % self.name)
update_torrent_window_now.send("%s daemon" % self.name)

def reset_daemon(self):
logger.info("%s is resetting", self.name)

def run_command(self, sender: str, command_func: str, command_args: dict):
self._command_q.put(dict(func=command_func, func_args=command_args))
Expand Down
9 changes: 8 additions & 1 deletion qbittorrentui/windows/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,15 @@ def __init__(self, main, error_message: str = "", support_auto_connect=False):
self.attempt_auto_connect = False
for section in config.keys():
if section != "DEFAULT":
# if CONNECT_AUTOMATICALLY is set to anything other than
# 0 or FALSE/false/False, automatically connecting is enabled
settings_auto_connect = config.get(
section=section, option="CONNECT_AUTOMATICALLY"
)
is_auto_connect = bool(
config.get(section=section, option="CONNECT_AUTOMATICALLY")
settings_auto_connect
and not settings_auto_connect.upper() == "FALSE"
and not settings_auto_connect == "0"
)
if (
support_auto_connect
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

setup(
name="qbittorrentui",
version="0.3.0",
version="0.3.1",
packages=find_packages(exclude=["*.tests", "*.tests.*", "tests.*", "tests"]),
package_data={"": ["default.ini"]},
include_package_data=True,
Expand Down

0 comments on commit e34440a

Please sign in to comment.