From 759a618f7434379c817f8f42b9a97d7e443587da Mon Sep 17 00:00:00 2001 From: Calum Lind Date: Mon, 24 Sep 2018 15:49:33 +0100 Subject: [PATCH] [Core] Tweaks to prefetch metadata method - Disable the magnet from being auto_managed so it starts immediately. - Reduce the default timeout to 30secs. - Use the generic tempfile dir. - Move callback method to be class method --- deluge/core/core.py | 2 +- deluge/core/torrentmanager.py | 65 +++++++++++++++-------------- deluge/tests/test_torrentmanager.py | 6 +-- 3 files changed, 38 insertions(+), 35 deletions(-) diff --git a/deluge/core/core.py b/deluge/core/core.py index 7125a8093..15024258e 100644 --- a/deluge/core/core.py +++ b/deluge/core/core.py @@ -406,7 +406,7 @@ class Core(component.Component): return d @export - def prefetch_magnet_metadata(self, magnet, timeout=60): + def prefetch_magnet_metadata(self, magnet, timeout=30): """Download the metadata for the magnet uri without adding torrent to deluge session. Args: diff --git a/deluge/core/torrentmanager.py b/deluge/core/torrentmanager.py index 2ab5c66e9..cb5d1bb3f 100644 --- a/deluge/core/torrentmanager.py +++ b/deluge/core/torrentmanager.py @@ -15,6 +15,7 @@ import logging import operator import os import time +from tempfile import gettempdir import six.moves.cPickle as pickle from twisted.internet import defer, error, reactor, threads @@ -303,7 +304,7 @@ class TorrentManager(component.Component): else: return torrent_info - def prefetch_metadata(self, magnet, timeout=60): + def prefetch_metadata(self, magnet, timeout): """Download metadata for a magnet uri. Args: @@ -316,46 +317,48 @@ class TorrentManager(component.Component): """ add_torrent_params = {} - # need a temp save_path - add_torrent_params['save_path'] = '/tmp' + add_torrent_params['save_path'] = gettempdir() add_torrent_params['url'] = magnet.strip().encode('utf8') - # do we need to make it not auto_managed to force start. what about queue? - add_torrent_params['flags'] = ((LT_DEFAULT_ADD_TORRENT_FLAGS | - lt.add_torrent_params_flags_t.flag_duplicate_is_error | - lt.add_torrent_params_flags_t.flag_upload_mode)) + add_torrent_params['flags'] = (( + LT_DEFAULT_ADD_TORRENT_FLAGS + | lt.add_torrent_params_flags_t.flag_duplicate_is_error + | lt.add_torrent_params_flags_t.flag_upload_mode + ) + ^ lt.add_torrent_params_flags_t.flag_auto_managed + ^ lt.add_torrent_params_flags_t.flag_paused + ) torrent_handle = self.session.add_torrent(add_torrent_params) - torrent_id = str(torrent_handle.info_hash()) - - def on_metadata(torrent_info, torrent_id, defer_timeout): - # Cancel reactor.callLater. - try: - defer_timeout.cancel() - except error.AlreadyCalled: - pass - - log.debug('remove magnet from session') - try: - torrent_handle = self.prefetching_metadata.pop(torrent_id)[1] - except KeyError: - pass - else: - self.session.remove_torrent(torrent_handle, 1) - - metadata = '' - if isinstance(torrent_info, lt.torrent_info): - log.debug('metadata received') - metadata = torrent_info.metadata() - - return torrent_id, metadata d = Deferred() # Cancel the defer if timeout reached. defer_timeout = self.callLater(timeout, d.cancel) - d.addBoth(on_metadata, torrent_id, defer_timeout) + d.addBoth(self.on_prefetch_metadata, torrent_id, defer_timeout) self.prefetching_metadata[torrent_id] = (d, torrent_handle) return d + def on_prefetch_metadata(self, torrent_info, torrent_id, defer_timeout): + # Cancel reactor.callLater. + try: + defer_timeout.cancel() + except error.AlreadyCalled: + pass + + log.debug('remove magnet from session') + try: + torrent_handle = self.prefetching_metadata.pop(torrent_id)[1] + except KeyError: + pass + else: + self.session.remove_torrent(torrent_handle, 1) + + metadata = '' + if isinstance(torrent_info, lt.torrent_info): + log.debug('metadata received') + metadata = torrent_info.metadata() + + return torrent_id, metadata + def _build_torrent_options(self, options): """Load default options and update if needed.""" _options = TorrentOptions() diff --git a/deluge/tests/test_torrentmanager.py b/deluge/tests/test_torrentmanager.py index cc1010379..711f778c2 100644 --- a/deluge/tests/test_torrentmanager.py +++ b/deluge/tests/test_torrentmanager.py @@ -67,7 +67,7 @@ class TorrentmanagerTestCase(BaseTestCase): return_value=t_info) magnet = 'magnet:?xt=urn:btih:ab570cdd5a17ea1b61e970bb72047de141bce173' - d = self.tm.prefetch_metadata(magnet) + d = self.tm.prefetch_metadata(magnet, 30) self.tm.on_alert_metadata_received(mock_alert) expected = ( @@ -105,8 +105,8 @@ class TorrentmanagerTestCase(BaseTestCase): def test_prefetch_metadata_timeout(self): magnet = 'magnet:?xt=urn:btih:ab570cdd5a17ea1b61e970bb72047de141bce173' - d = self.tm.prefetch_metadata(magnet) - self.clock.advance(60) + d = self.tm.prefetch_metadata(magnet, 30) + self.clock.advance(30) expected = ('ab570cdd5a17ea1b61e970bb72047de141bce173', '') return d.addCallback(self.assertEqual, expected)