Skip to content

Commit

Permalink
Fix start of watch folder
Browse files Browse the repository at this point in the history
  • Loading branch information
drew2a committed Apr 13, 2022
1 parent b7d1689 commit 0dc490c
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 12 deletions.
23 changes: 18 additions & 5 deletions src/tribler/core/components/watch_folder/watch_folder.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,24 @@
from tribler.core import notifications
from tribler.core.components.libtorrent.download_manager.download_manager import DownloadManager
from tribler.core.components.libtorrent.torrentdef import TorrentDef
from tribler.core.components.watch_folder.settings import WatchFolderSettings
from tribler.core.utilities import path_util
from tribler.core.utilities.notifier import Notifier

WATCH_FOLDER_CHECK_INTERVAL = 10


class WatchFolder(TaskManager):

def __init__(self, watch_folder_path, download_manager: DownloadManager, notifier: Notifier):
def __init__(self, state_dir: Path, settings: WatchFolderSettings, download_manager: DownloadManager,
notifier: Notifier):
super().__init__()
self.watch_folder = watch_folder_path
self.state_dir = state_dir
self.settings = settings
self.download_manager = download_manager
self.notifier = notifier

self._logger = logging.getLogger(self.__class__.__name__)
self._logger.info(f'Initialised with {settings}')

def start(self):
self.register_task("check watch folder", self.check_watch_folder, interval=WATCH_FOLDER_CHECK_INTERVAL)
Expand All @@ -45,11 +48,20 @@ def cleanup_torrent_file(self, root, name):
self.notifier[notifications.watch_folder_corrupt_file](name)

def check_watch_folder(self):
if not self.watch_folder.is_dir():
self._logger.debug(f'Checking watch folder...')

if not self.settings.enabled or not self.state_dir:
self._logger.debug(f'Cancelled. Enabled: {self.settings.enabled}. State dir: {self.state_dir}.')
return

directory = self.settings.get_path_as_absolute('directory', self.state_dir)
if not directory.is_dir():
self._logger.debug(f'Cancelled. Is not directory: {directory}.')
return

# Make sure that we pass a str to os.walk
watch_dir = str(self.watch_folder)
watch_dir = str(directory)
self._logger.debug(f'Watch dir: {watch_dir}')

for root, _, files in os.walk(watch_dir):
root = path_util.Path(root)
Expand All @@ -71,3 +83,4 @@ def check_watch_folder(self):
if not self.download_manager.download_exists(infohash):
self._logger.info("Starting download from torrent file %s", name)
self.download_manager.start_download(torrent_file=root / name)
self._logger.debug(f'Checking watch folder completed.')
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@ class WatchFolderComponent(Component):

async def run(self):
await super().run()
config = self.session.config
notifier = self.session.notifier
libtorrent_component = await self.require_component(LibtorrentComponent)

watch_folder_path = config.watch_folder.get_path_as_absolute('directory', config.state_dir)
watch_folder = WatchFolder(watch_folder_path=watch_folder_path,
download_manager=libtorrent_component.download_manager,
notifier=notifier)
watch_folder = WatchFolder(
state_dir=self.session.config.state_dir,
settings=self.session.config.watch_folder,
download_manager=libtorrent_component.download_manager,
notifier=notifier
)
watch_folder.start()
self.watch_folder = watch_folder

Expand Down
3 changes: 1 addition & 2 deletions src/tribler/core/start_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,7 @@ def components_gen(config: TriblerConfig):
yield TunnelsComponent()
if config.ipv8.enabled:
yield PayoutComponent()
if config.watch_folder.enabled:
yield WatchFolderComponent()
yield WatchFolderComponent()
if config.general.version_checker_enabled:
yield VersionCheckComponent()
if config.chant.enabled and config.chant.manager_enabled and config.libtorrent.enabled:
Expand Down

0 comments on commit 0dc490c

Please sign in to comment.