diff --git a/src/tribler/core/components/libtorrent/download_manager/download_manager.py b/src/tribler/core/components/libtorrent/download_manager/download_manager.py index ced69f75ed3..9d604d4e1f0 100644 --- a/src/tribler/core/components/libtorrent/download_manager/download_manager.py +++ b/src/tribler/core/components/libtorrent/download_manager/download_manager.py @@ -32,6 +32,7 @@ HTTPS_SCHEME, HTTP_SCHEME, MAGNET_SCHEME, + path_to_url, scheme_from_url, url_to_path, ) @@ -72,6 +73,7 @@ def __init__(self, notifier: Notifier, peer_mid: bytes, config: LibtorrentSettings = None, + gui_test_mode: bool = False, download_defaults: DownloadDefaultsSettings = None, bootstrap_infohash=None, socks_listen_ports: Optional[List[int]] = None, @@ -92,6 +94,7 @@ def __init__(self, self.notifier = notifier self.peer_mid = peer_mid self.config = config or LibtorrentSettings() + self.gui_test_mode = gui_test_mode self.bootstrap_infohash = bootstrap_infohash self.download_defaults = download_defaults or DownloadDefaultsSettings() self._libtorrent_port = None @@ -158,10 +161,23 @@ def initialize(self): self.set_download_states_callback(self.sesscb_states_callback) + def start(self): + self.register_task("start", self._start) + + async def _start(self): + await self.load_checkpoints() + + if self.gui_test_mode: + from tribler.core.tests.tools.common import TORRENT_WITH_DIRS # pylint: disable=import-outside-toplevel + uri = path_to_url(TORRENT_WITH_DIRS) + await self.start_download_from_uri(uri) + def notify_shutdown_state(self, state): self.notifier[notifications.tribler_shutdown_state](state) async def shutdown(self, timeout=30): + self.cancel_pending_task("start") + self.cancel_pending_task("download_states_lc") if self.downloads: self.notify_shutdown_state("Checkpointing Downloads...") await gather(*[download.stop() for download in self.downloads.values()], return_exceptions=True) @@ -782,9 +798,6 @@ def set_download_states_callback(self, user_callback, interval=1.0): self._logger.debug("Starting the download state callback with interval %f", interval) self.replace_task("download_states_lc", self._invoke_states_cb, user_callback, interval=interval) - def stop_download_states_callback(self): - return self.cancel_pending_task("download_states_lc") - async def _invoke_states_cb(self, callback): """ Invoke the download states callback with a list of the download states. @@ -825,9 +838,11 @@ def get_last_download_states(self): return self._last_states_list async def load_checkpoints(self): + self._logger.info("Load checkpoints...") for filename in self.get_checkpoint_dir().glob('*.conf'): self.load_checkpoint(filename) await sleep(.01) + self._logger.info("Checkpoints are loaded") def load_checkpoint(self, filename): try: diff --git a/src/tribler/core/components/libtorrent/libtorrent_component.py b/src/tribler/core/components/libtorrent/libtorrent_component.py index b2619c63a82..ff586d1eb0d 100644 --- a/src/tribler/core/components/libtorrent/libtorrent_component.py +++ b/src/tribler/core/components/libtorrent/libtorrent_component.py @@ -2,7 +2,6 @@ from tribler.core.components.key.key_component import KeyComponent from tribler.core.components.libtorrent.download_manager.download_manager import DownloadManager from tribler.core.components.socks_servers.socks_servers_component import SocksServersComponent -from tribler.core.utilities.rest_utils import path_to_url class LibtorrentComponent(Component): @@ -21,6 +20,7 @@ async def run(self): self.download_manager = DownloadManager( config=config.libtorrent, + gui_test_mode=config.gui_test_mode, state_dir=config.state_dir, notifier=self.session.notifier, peer_mid=key_component.primary_key.key_to_hash(), @@ -30,15 +30,10 @@ async def run(self): dummy_mode=config.gui_test_mode) self.download_manager.initialize() - await self.download_manager.load_checkpoints() - - if config.gui_test_mode: - from tribler.core.tests.tools.common import TORRENT_WITH_DIRS # pylint: disable=import-outside-toplevel - uri = path_to_url(TORRENT_WITH_DIRS) - await self.download_manager.start_download_from_uri(uri) + # load checkpoints in a background task to not delay initialization of dependent components (e.g. RESTComponent) + self.download_manager.start() async def shutdown(self): await super().shutdown() if self.download_manager: - self.download_manager.stop_download_states_callback() await self.download_manager.shutdown()