[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
This commit is contained in:
Calum Lind 2018-09-24 15:49:33 +01:00
parent 23f1cfc926
commit 759a618f74
3 changed files with 38 additions and 35 deletions

View File

@ -406,7 +406,7 @@ class Core(component.Component):
return d return d
@export @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. """Download the metadata for the magnet uri without adding torrent to deluge session.
Args: Args:

View File

@ -15,6 +15,7 @@ import logging
import operator import operator
import os import os
import time import time
from tempfile import gettempdir
import six.moves.cPickle as pickle import six.moves.cPickle as pickle
from twisted.internet import defer, error, reactor, threads from twisted.internet import defer, error, reactor, threads
@ -303,7 +304,7 @@ class TorrentManager(component.Component):
else: else:
return torrent_info return torrent_info
def prefetch_metadata(self, magnet, timeout=60): def prefetch_metadata(self, magnet, timeout):
"""Download metadata for a magnet uri. """Download metadata for a magnet uri.
Args: Args:
@ -316,46 +317,48 @@ class TorrentManager(component.Component):
""" """
add_torrent_params = {} add_torrent_params = {}
# need a temp save_path add_torrent_params['save_path'] = gettempdir()
add_torrent_params['save_path'] = '/tmp'
add_torrent_params['url'] = magnet.strip().encode('utf8') 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'] = ((
add_torrent_params['flags'] = ((LT_DEFAULT_ADD_TORRENT_FLAGS | LT_DEFAULT_ADD_TORRENT_FLAGS
lt.add_torrent_params_flags_t.flag_duplicate_is_error | | 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_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_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() d = Deferred()
# Cancel the defer if timeout reached. # Cancel the defer if timeout reached.
defer_timeout = self.callLater(timeout, d.cancel) 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) self.prefetching_metadata[torrent_id] = (d, torrent_handle)
return d 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): def _build_torrent_options(self, options):
"""Load default options and update if needed.""" """Load default options and update if needed."""
_options = TorrentOptions() _options = TorrentOptions()

View File

@ -67,7 +67,7 @@ class TorrentmanagerTestCase(BaseTestCase):
return_value=t_info) return_value=t_info)
magnet = 'magnet:?xt=urn:btih:ab570cdd5a17ea1b61e970bb72047de141bce173' 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) self.tm.on_alert_metadata_received(mock_alert)
expected = ( expected = (
@ -105,8 +105,8 @@ class TorrentmanagerTestCase(BaseTestCase):
def test_prefetch_metadata_timeout(self): def test_prefetch_metadata_timeout(self):
magnet = 'magnet:?xt=urn:btih:ab570cdd5a17ea1b61e970bb72047de141bce173' magnet = 'magnet:?xt=urn:btih:ab570cdd5a17ea1b61e970bb72047de141bce173'
d = self.tm.prefetch_metadata(magnet) d = self.tm.prefetch_metadata(magnet, 30)
self.clock.advance(60) self.clock.advance(30)
expected = ('ab570cdd5a17ea1b61e970bb72047de141bce173', '') expected = ('ab570cdd5a17ea1b61e970bb72047de141bce173', '')
return d.addCallback(self.assertEqual, expected) return d.addCallback(self.assertEqual, expected)