Fix #1840 : Refactor last_seen_complete code

This commit is contained in:
Calum Lind 2013-04-29 22:31:50 +01:00
commit 1d34d5f6a5
2 changed files with 11 additions and 51 deletions

View file

@ -214,12 +214,6 @@ class Torrent(object):
else:
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
# re-pause it after its done if necessary
self.forcing_recheck = False
@ -634,16 +628,6 @@ class Torrent(object):
return host
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):
"""
Returns the status of the torrent based on the keys provided
@ -768,7 +752,7 @@ class Torrent(object):
"queue": self.handle.queue_position,
"ratio": self.get_ratio,
"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,
"pieces": self._get_pieces_info,
}
@ -1043,19 +1027,6 @@ class Torrent(object):
if not self.rpcserver.is_session_valid(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):
if not self.has_metadata:
return None

View file

@ -87,8 +87,8 @@ class TorrentState:
move_completed_path=None,
magnet=None,
time_added=-1,
last_seen_complete=0.0, # 0 is the default returned when the info
owner="", # does not exist on lt >= .16
last_seen_complete=0,
owner="",
shared=False
):
self.torrent_id = torrent_id
@ -149,7 +149,6 @@ class TorrentManager(component.Component):
# Create the torrents dict { torrent_id: Torrent }
self.torrents = {}
self.last_seen_complete_loop = None
self.queued_torrents = set()
# 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.start(900, False)
if self.last_seen_complete_loop:
self.last_seen_complete_loop.start(60)
def stop(self):
# Stop timers
if self.save_state_timer.running:
@ -242,9 +238,6 @@ class TorrentManager(component.Component):
if self.save_all_resume_data_timer.running:
self.save_all_resume_data_timer.stop()
if self.last_seen_complete_loop:
self.last_seen_complete_loop.stop()
# Save state on shutdown
self.save_state()
@ -690,16 +683,6 @@ class TorrentManager(component.Component):
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())
def save_state(self):
@ -711,10 +694,16 @@ class TorrentManager(component.Component):
if torrent.state == "Paused":
paused = True
torrent_status = torrent.get_status([
"total_uploaded",
"last_seen_complete"
], update=True
)
torrent_state = TorrentState(
torrent.torrent_id,
torrent.filename,
torrent.get_status(["total_uploaded"])["total_uploaded"],
torrent_status["total_uploaded"],
torrent.trackers,
torrent.options["compact_allocation"],
paused,
@ -736,7 +725,7 @@ class TorrentManager(component.Component):
torrent.options["move_completed_path"],
torrent.magnet,
torrent.time_added,
torrent.get_last_seen_complete(),
torrent_status["last_seen_complete"],
torrent.owner,
torrent.options["shared"]
)