mirror of
https://git.deluge-torrent.org/deluge
synced 2025-09-18 15:51:57 +00:00
Fix #1840 : Refactor last_seen_complete code
This commit is contained in:
parent
64e1ab481b
commit
1d34d5f6a5
2 changed files with 11 additions and 51 deletions
|
@ -214,12 +214,6 @@ class Torrent(object):
|
||||||
else:
|
else:
|
||||||
self.owner = owner
|
self.owner = owner
|
||||||
|
|
||||||
# Keep track of last seen complete
|
|
||||||
if state:
|
|
||||||
self._last_seen_complete = state.last_seen_complete or 0.0
|
|
||||||
else:
|
|
||||||
self._last_seen_complete = 0.0
|
|
||||||
|
|
||||||
# Keep track if we're forcing a recheck of the torrent so that we can
|
# Keep track if we're forcing a recheck of the torrent so that we can
|
||||||
# re-pause it after its done if necessary
|
# re-pause it after its done if necessary
|
||||||
self.forcing_recheck = False
|
self.forcing_recheck = False
|
||||||
|
@ -634,16 +628,6 @@ class Torrent(object):
|
||||||
return host
|
return host
|
||||||
return ""
|
return ""
|
||||||
|
|
||||||
def get_last_seen_complete(self):
|
|
||||||
"""
|
|
||||||
Returns the time a torrent was last seen complete, ie, with all pieces
|
|
||||||
available.
|
|
||||||
"""
|
|
||||||
if lt.version_minor > 15:
|
|
||||||
return self.status.last_seen_complete
|
|
||||||
self.calculate_last_seen_complete()
|
|
||||||
return self._last_seen_complete
|
|
||||||
|
|
||||||
def get_status(self, keys, diff=False, update=False):
|
def get_status(self, keys, diff=False, update=False):
|
||||||
"""
|
"""
|
||||||
Returns the status of the torrent based on the keys provided
|
Returns the status of the torrent based on the keys provided
|
||||||
|
@ -768,7 +752,7 @@ class Torrent(object):
|
||||||
"queue": self.handle.queue_position,
|
"queue": self.handle.queue_position,
|
||||||
"ratio": self.get_ratio,
|
"ratio": self.get_ratio,
|
||||||
"tracker_host": self.get_tracker_host,
|
"tracker_host": self.get_tracker_host,
|
||||||
"last_seen_complete": self.get_last_seen_complete,
|
"last_seen_complete": lambda: self.status.last_seen_complete,
|
||||||
"name": self.get_name,
|
"name": self.get_name,
|
||||||
"pieces": self._get_pieces_info,
|
"pieces": self._get_pieces_info,
|
||||||
}
|
}
|
||||||
|
@ -1043,19 +1027,6 @@ class Torrent(object):
|
||||||
if not self.rpcserver.is_session_valid(key):
|
if not self.rpcserver.is_session_valid(key):
|
||||||
del self.prev_status[key]
|
del self.prev_status[key]
|
||||||
|
|
||||||
def calculate_last_seen_complete(self):
|
|
||||||
if self._last_seen_complete+60 > time.time():
|
|
||||||
# Simple caching. Only calculate every 1 min at minimum
|
|
||||||
return self._last_seen_complete
|
|
||||||
|
|
||||||
availability = self.handle.piece_availability()
|
|
||||||
if filter(lambda x: x<1, availability):
|
|
||||||
# Torrent does not have all the pieces
|
|
||||||
return
|
|
||||||
log.trace("Torrent %s has all the pieces. Setting last seen complete.",
|
|
||||||
self.torrent_id)
|
|
||||||
self._last_seen_complete = time.time()
|
|
||||||
|
|
||||||
def _get_pieces_info(self):
|
def _get_pieces_info(self):
|
||||||
if not self.has_metadata:
|
if not self.has_metadata:
|
||||||
return None
|
return None
|
||||||
|
|
|
@ -87,8 +87,8 @@ class TorrentState:
|
||||||
move_completed_path=None,
|
move_completed_path=None,
|
||||||
magnet=None,
|
magnet=None,
|
||||||
time_added=-1,
|
time_added=-1,
|
||||||
last_seen_complete=0.0, # 0 is the default returned when the info
|
last_seen_complete=0,
|
||||||
owner="", # does not exist on lt >= .16
|
owner="",
|
||||||
shared=False
|
shared=False
|
||||||
):
|
):
|
||||||
self.torrent_id = torrent_id
|
self.torrent_id = torrent_id
|
||||||
|
@ -149,7 +149,6 @@ class TorrentManager(component.Component):
|
||||||
|
|
||||||
# Create the torrents dict { torrent_id: Torrent }
|
# Create the torrents dict { torrent_id: Torrent }
|
||||||
self.torrents = {}
|
self.torrents = {}
|
||||||
self.last_seen_complete_loop = None
|
|
||||||
self.queued_torrents = set()
|
self.queued_torrents = set()
|
||||||
|
|
||||||
# This is a map of torrent_ids to Deferreds used to track needed resume data.
|
# This is a map of torrent_ids to Deferreds used to track needed resume data.
|
||||||
|
@ -228,9 +227,6 @@ class TorrentManager(component.Component):
|
||||||
self.save_all_resume_data_timer = LoopingCall(self.save_resume_data, self.torrents.keys())
|
self.save_all_resume_data_timer = LoopingCall(self.save_resume_data, self.torrents.keys())
|
||||||
self.save_all_resume_data_timer.start(900, False)
|
self.save_all_resume_data_timer.start(900, False)
|
||||||
|
|
||||||
if self.last_seen_complete_loop:
|
|
||||||
self.last_seen_complete_loop.start(60)
|
|
||||||
|
|
||||||
def stop(self):
|
def stop(self):
|
||||||
# Stop timers
|
# Stop timers
|
||||||
if self.save_state_timer.running:
|
if self.save_state_timer.running:
|
||||||
|
@ -242,9 +238,6 @@ class TorrentManager(component.Component):
|
||||||
if self.save_all_resume_data_timer.running:
|
if self.save_all_resume_data_timer.running:
|
||||||
self.save_all_resume_data_timer.stop()
|
self.save_all_resume_data_timer.stop()
|
||||||
|
|
||||||
if self.last_seen_complete_loop:
|
|
||||||
self.last_seen_complete_loop.stop()
|
|
||||||
|
|
||||||
# Save state on shutdown
|
# Save state on shutdown
|
||||||
self.save_state()
|
self.save_state()
|
||||||
|
|
||||||
|
@ -690,16 +683,6 @@ class TorrentManager(component.Component):
|
||||||
|
|
||||||
self.alerts.wait_on_handler = False
|
self.alerts.wait_on_handler = False
|
||||||
|
|
||||||
if lt.version_minor < 16:
|
|
||||||
log.debug("libtorrent version is lower than 0.16. Start looping "
|
|
||||||
"callback to calculate last_seen_complete info.")
|
|
||||||
def calculate_last_seen_complete():
|
|
||||||
for torrent in self.torrents.values():
|
|
||||||
torrent.calculate_last_seen_complete()
|
|
||||||
self.last_seen_complete_loop = LoopingCall(
|
|
||||||
calculate_last_seen_complete
|
|
||||||
)
|
|
||||||
|
|
||||||
component.get("EventManager").emit(SessionStartedEvent())
|
component.get("EventManager").emit(SessionStartedEvent())
|
||||||
|
|
||||||
def save_state(self):
|
def save_state(self):
|
||||||
|
@ -711,10 +694,16 @@ class TorrentManager(component.Component):
|
||||||
if torrent.state == "Paused":
|
if torrent.state == "Paused":
|
||||||
paused = True
|
paused = True
|
||||||
|
|
||||||
|
torrent_status = torrent.get_status([
|
||||||
|
"total_uploaded",
|
||||||
|
"last_seen_complete"
|
||||||
|
], update=True
|
||||||
|
)
|
||||||
|
|
||||||
torrent_state = TorrentState(
|
torrent_state = TorrentState(
|
||||||
torrent.torrent_id,
|
torrent.torrent_id,
|
||||||
torrent.filename,
|
torrent.filename,
|
||||||
torrent.get_status(["total_uploaded"])["total_uploaded"],
|
torrent_status["total_uploaded"],
|
||||||
torrent.trackers,
|
torrent.trackers,
|
||||||
torrent.options["compact_allocation"],
|
torrent.options["compact_allocation"],
|
||||||
paused,
|
paused,
|
||||||
|
@ -736,7 +725,7 @@ class TorrentManager(component.Component):
|
||||||
torrent.options["move_completed_path"],
|
torrent.options["move_completed_path"],
|
||||||
torrent.magnet,
|
torrent.magnet,
|
||||||
torrent.time_added,
|
torrent.time_added,
|
||||||
torrent.get_last_seen_complete(),
|
torrent_status["last_seen_complete"],
|
||||||
torrent.owner,
|
torrent.owner,
|
||||||
torrent.options["shared"]
|
torrent.options["shared"]
|
||||||
)
|
)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue