[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:
parent
23f1cfc926
commit
759a618f74
|
@ -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:
|
||||
|
|
|
@ -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()
|
||||
|
|
|
@ -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)
|
||||
|
||||
|
|
Loading…
Reference in New Issue