Clean-up some of the code in the torrentmanager alert handlers to

prevent uncaught exceptions during shutdown
This commit is contained in:
Andrew Resch 2009-04-20 02:28:06 +00:00
parent 22c9fffad8
commit b3ee54fcdc
1 changed files with 69 additions and 66 deletions

View File

@ -624,9 +624,11 @@ class TorrentManager(component.Component):
## Alert handlers ## ## Alert handlers ##
def on_alert_torrent_finished(self, alert): def on_alert_torrent_finished(self, alert):
log.debug("on_alert_torrent_finished") log.debug("on_alert_torrent_finished")
# Get the torrent_id try:
torrent = self.torrents[str(alert.handle.info_hash())]
except:
return
torrent_id = str(alert.handle.info_hash()) torrent_id = str(alert.handle.info_hash())
torrent = self.torrents[torrent_id]
log.debug("%s is finished..", torrent_id) log.debug("%s is finished..", torrent_id)
# Move completed download to completed folder if needed # Move completed download to completed folder if needed
if not torrent.is_finished: if not torrent.is_finished:
@ -646,13 +648,16 @@ class TorrentManager(component.Component):
def on_alert_torrent_paused(self, alert): def on_alert_torrent_paused(self, alert):
log.debug("on_alert_torrent_paused") log.debug("on_alert_torrent_paused")
# Get the torrent_id try:
torrent = self.torrents[str(alert.handle.info_hash())]
except:
return
torrent_id = str(alert.handle.info_hash()) torrent_id = str(alert.handle.info_hash())
# Set the torrent state # Set the torrent state
old_state = self.torrents[torrent_id].state old_state = torrent.state
self.torrents[torrent_id].update_state() torrent.update_state()
if self.torrents[torrent_id].state != old_state: if torrent.state != old_state:
component.get("EventManager").emit(TorrentStateChangedEvent(torrent_id, self.torrents[torrent_id].state)) component.get("EventManager").emit(TorrentStateChangedEvent(torrent_id, torrent.state))
# Write the fastresume file # Write the fastresume file
self.torrents[torrent_id].save_resume_data() self.torrents[torrent_id].save_resume_data()
@ -662,91 +667,75 @@ class TorrentManager(component.Component):
def on_alert_torrent_checked(self, alert): def on_alert_torrent_checked(self, alert):
log.debug("on_alert_torrent_checked") log.debug("on_alert_torrent_checked")
# Get the torrent_id try:
torrent_id = str(alert.handle.info_hash()) torrent = self.torrents[str(alert.handle.info_hash())]
except:
return
# Set the torrent state # Set the torrent state
self.torrents[torrent_id].update_state() torrent.update_state()
def on_alert_tracker_reply(self, alert): def on_alert_tracker_reply(self, alert):
log.debug("on_alert_tracker_reply: %s", alert.message()) log.debug("on_alert_tracker_reply: %s", alert.message())
if not alert.handle.is_valid(): try:
# Handle is no longer valid, probably a removed torrent torrent = self.torrents[str(alert.handle.info_hash())]
except:
return return
# Get the torrent_id
torrent_id = str(alert.handle.info_hash())
# Set the tracker status for the torrent # Set the tracker status for the torrent
try:
if alert.message() != "Got peers from DHT": if alert.message() != "Got peers from DHT":
self.torrents[torrent_id].set_tracker_status(_("Announce OK")) torrent.set_tracker_status(_("Announce OK"))
except KeyError:
log.debug("torrent_id doesn't exist.")
# Check to see if we got any peer information from the tracker # Check to see if we got any peer information from the tracker
if alert.handle.status().num_complete == -1 or \ if alert.handle.status().num_complete == -1 or \
alert.handle.status().num_incomplete == -1: alert.handle.status().num_incomplete == -1:
# We didn't get peer information, so lets send a scrape request # We didn't get peer information, so lets send a scrape request
self.torrents[torrent_id].scrape_tracker() torrent.scrape_tracker()
def on_alert_tracker_announce(self, alert): def on_alert_tracker_announce(self, alert):
log.debug("on_alert_tracker_announce") log.debug("on_alert_tracker_announce")
# Get the torrent_id
try: try:
torrent_id = str(alert.handle.info_hash()) torrent = self.torrents[str(alert.handle.info_hash())]
except RuntimeError: except:
log.debug("Invalid torrent handle.")
return return
# Set the tracker status for the torrent # Set the tracker status for the torrent
try: torrent.set_tracker_status(_("Announce Sent"))
self.torrents[torrent_id].set_tracker_status(_("Announce Sent"))
except KeyError:
log.debug("torrent_id doesn't exist.")
def on_alert_tracker_warning(self, alert): def on_alert_tracker_warning(self, alert):
log.debug("on_alert_tracker_warning") log.debug("on_alert_tracker_warning")
# Get the torrent_id
try: try:
torrent_id = str(alert.handle.info_hash()) torrent = self.torrents[str(alert.handle.info_hash())]
except RuntimeError: except:
log.debug("Invalid torrent handle.")
return return
tracker_status = '%s: %s' % (_("Warning"), str(alert.message())) tracker_status = '%s: %s' % (_("Warning"), str(alert.message()))
# Set the tracker status for the torrent # Set the tracker status for the torrent
try: torrent.set_tracker_status(tracker_status)
self.torrents[torrent_id].set_tracker_status(tracker_status)
except KeyError:
log.debug("torrent_id doesn't exist.")
def on_alert_tracker_error(self, alert): def on_alert_tracker_error(self, alert):
log.debug("on_alert_tracker_error") log.debug("on_alert_tracker_error")
try: try:
torrent_id = str(alert.handle.info_hash()) torrent = self.torrents[str(alert.handle.info_hash())]
except RuntimeError: except:
log.debug("Invalid torrent handle.")
return return
torrent = self.torrents[torrent_id]
tracker_status = "%s: %s" % (_("Error"), alert.msg) tracker_status = "%s: %s" % (_("Error"), alert.msg)
try:
torrent.set_tracker_status(tracker_status) torrent.set_tracker_status(tracker_status)
except KeyError:
log.debug("torrent_id doesn't exist.")
def on_alert_storage_moved(self, alert): def on_alert_storage_moved(self, alert):
log.debug("on_alert_storage_moved") log.debug("on_alert_storage_moved")
log.debug("save_path: %s", alert.handle.save_path())
# Get the torrent_id
torrent_id = str(alert.handle.info_hash())
try: try:
log.debug("save_path2: %s", self.torrents[torrent_id].handle.save_path()) torrent = self.torrents[str(alert.handle.info_hash())]
self.torrents[torrent_id].set_save_path(alert.handle.save_path()) except:
except KeyError: return
log.debug("torrent_id doesn't exist.") torrent.set_save_path(alert.handle.save_path())
def on_alert_torrent_resumed(self, alert): def on_alert_torrent_resumed(self, alert):
log.debug("on_alert_torrent_resumed") log.debug("on_alert_torrent_resumed")
try:
torrent = self.torrents[str(alert.handle.info_hash())]
except:
return
torrent_id = str(alert.handle.info_hash()) torrent_id = str(alert.handle.info_hash())
torrent = self.torrents[torrent_id]
torrent.is_finished = torrent.handle.is_seed() torrent.is_finished = torrent.handle.is_seed()
old_state = torrent.state old_state = torrent.state
torrent.update_state() torrent.update_state()
@ -758,32 +747,40 @@ class TorrentManager(component.Component):
def on_alert_state_changed(self, alert): def on_alert_state_changed(self, alert):
log.debug("on_alert_state_changed") log.debug("on_alert_state_changed")
try: try:
torrent_id = str(alert.handle.info_hash()) torrent = self.torrents[str(alert.handle.info_hash())]
except RuntimeError: except:
return return
if torrent_id in self.torrents: old_state = torrent.state
old_state = self.torrents[torrent_id].state torrent.update_state()
self.torrents[torrent_id].update_state()
# Only emit a state changed event if the state has actually changed # Only emit a state changed event if the state has actually changed
if self.torrents[torrent_id].state != old_state: if torrent.state != old_state:
component.get("EventManager").emit(TorrentStateChangedEvent(torrent_id, self.torrents[torrent_id].state)) component.get("EventManager").emit(TorrentStateChangedEvent(torrent_id, torrent.state))
def on_alert_save_resume_data(self, alert): def on_alert_save_resume_data(self, alert):
log.debug("on_alert_save_resume_data") log.debug("on_alert_save_resume_data")
try:
torrent = self.torrents[str(alert.handle.info_hash())] torrent = self.torrents[str(alert.handle.info_hash())]
except:
return
torrent.write_resume_data(alert.resume_data) torrent.write_resume_data(alert.resume_data)
def on_alert_save_resume_data_failed(self, alert): def on_alert_save_resume_data_failed(self, alert):
log.debug("on_alert_save_resume_data_failed: %s", alert.message()) log.debug("on_alert_save_resume_data_failed: %s", alert.message())
try:
torrent = self.torrents[str(alert.handle.info_hash())] torrent = self.torrents[str(alert.handle.info_hash())]
except:
return
torrent.waiting_on_resume_data = False torrent.waiting_on_resume_data = False
def on_alert_file_renamed(self, alert): def on_alert_file_renamed(self, alert):
log.debug("on_alert_file_renamed") log.debug("on_alert_file_renamed")
log.debug("index: %s name: %s", alert.index, alert.name) log.debug("index: %s name: %s", alert.index, alert.name)
try:
torrent = self.torrents[str(alert.handle.info_hash())]
except:
return
torrent_id = str(alert.handle.info_hash()) torrent_id = str(alert.handle.info_hash())
torrent = self.torrents[torrent_id]
torrent.files[alert.index]["path"] = alert.name torrent.files[alert.index]["path"] = alert.name
# We need to see if this file index is in a waiting_on_folder list # We need to see if this file index is in a waiting_on_folder list
@ -806,10 +803,16 @@ class TorrentManager(component.Component):
def on_alert_metadata_received(self, alert): def on_alert_metadata_received(self, alert):
log.debug("on_alert_metadata_received") log.debug("on_alert_metadata_received")
try:
torrent = self.torrents[str(alert.handle.info_hash())] torrent = self.torrents[str(alert.handle.info_hash())]
except:
return
torrent.write_torrentfile() torrent.write_torrentfile()
def on_alert_file_error(self, alert): def on_alert_file_error(self, alert):
log.debug("on_alert_file_error: %s", alert.message()) log.debug("on_alert_file_error: %s", alert.message())
try:
torrent = self.torrents[str(alert.handle.info_hash())] torrent = self.torrents[str(alert.handle.info_hash())]
except:
return
torrent.update_state() torrent.update_state()