Skip to content

Commit

Permalink
Load libtorrent checkpoints in a background task to not delay initial…
Browse files Browse the repository at this point in the history
…ization of REST API
  • Loading branch information
kozlovsky committed Sep 7, 2022
1 parent cdbb9e8 commit 640d0fa
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
HTTPS_SCHEME,
HTTP_SCHEME,
MAGNET_SCHEME,
path_to_url,
scheme_from_url,
url_to_path,
)
Expand Down Expand Up @@ -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,
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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:
Expand Down
11 changes: 3 additions & 8 deletions src/tribler/core/components/libtorrent/libtorrent_component.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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(),
Expand All @@ -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()

0 comments on commit 640d0fa

Please sign in to comment.