[#1126] [#2322] Emit FinishedEvent after moving storage complete

Also changed the Execute and Extractor plugins to process the updated
FinishedEvent functionality.
This commit is contained in:
Calum Lind 2014-07-12 21:54:45 +01:00
commit fed5221503
3 changed files with 38 additions and 23 deletions

View file

@ -148,6 +148,9 @@ class TorrentManager(component.Component):
# self.num_resume_data used to save resume_data in bulk # self.num_resume_data used to save resume_data in bulk
self.num_resume_data = 0 self.num_resume_data = 0
# Keep track of torrents finished but moving storage
self.waiting_on_finish_moving = []
# Keeps track of resume data that needs to be saved to disk # Keeps track of resume data that needs to be saved to disk
self.resume_data = {} self.resume_data = {}
@ -181,6 +184,8 @@ class TorrentManager(component.Component):
self.on_alert_tracker_error) self.on_alert_tracker_error)
self.alerts.register_handler("storage_moved_alert", self.alerts.register_handler("storage_moved_alert",
self.on_alert_storage_moved) self.on_alert_storage_moved)
self.alerts.register_handler("storage_moved_failed_alert",
self.on_alert_storage_moved_failed)
self.alerts.register_handler("torrent_resumed_alert", self.alerts.register_handler("torrent_resumed_alert",
self.on_alert_torrent_resumed) self.on_alert_torrent_resumed)
self.alerts.register_handler("state_changed_alert", self.alerts.register_handler("state_changed_alert",
@ -895,17 +900,14 @@ class TorrentManager(component.Component):
# that the torrent wasn't downloaded, but just added. # that the torrent wasn't downloaded, but just added.
total_download = torrent.get_status(["total_payload_download"])["total_payload_download"] total_download = torrent.get_status(["total_payload_download"])["total_payload_download"]
# Move completed download to completed folder if needed
if not torrent.is_finished and total_download:
move_path = None
if torrent.options["move_completed"]:
move_path = torrent.options["move_completed_path"]
if torrent.options["download_location"] != move_path:
torrent.move_storage(move_path)
torrent.update_state() torrent.update_state()
if not torrent.is_finished and total_download: if not torrent.is_finished and total_download:
# Move completed download to completed folder if needed
if torrent.options["move_completed"] and \
torrent.options["download_location"] != torrent.options["move_completed_path"]:
self.waiting_on_finish_moving.append(torrent_id)
torrent.move_storage(torrent.options["move_completed_path"])
else:
torrent.is_finished = True torrent.is_finished = True
component.get("EventManager").emit(TorrentFinishedEvent(torrent_id)) component.get("EventManager").emit(TorrentFinishedEvent(torrent_id))
else: else:
@ -1014,12 +1016,32 @@ class TorrentManager(component.Component):
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")
try: try:
torrent = self.torrents[str(alert.handle.info_hash())] torrent_id = str(alert.handle.info_hash())
except: torrent = self.torrents[torrent_id]
except (RuntimeError, KeyError):
return return
torrent.set_save_path(os.path.normpath(alert.handle.save_path())) torrent.set_save_path(os.path.normpath(alert.handle.save_path()))
torrent.set_move_completed(False) torrent.set_move_completed(False)
if torrent in self.waiting_on_finish_moving:
self.waiting_on_finish_moving.remove(torrent_id)
torrent.is_finished = True
component.get("EventManager").emit(TorrentFinishedEvent(torrent_id))
def on_alert_storage_moved_failed(self, alert):
"""Alert handler for libtorrent storage_moved_failed_alert"""
log.debug("on_alert_storage_moved_failed: %s", alert.message())
try:
torrent_id = str(alert.handle.info_hash())
torrent = self.torrents[torrent_id]
except (RuntimeError, KeyError):
return
if torrent in self.waiting_on_finish_moving:
self.waiting_on_finish_moving.remove(torrent_id)
torrent.is_finished = True
component.get("EventManager").emit(TorrentFinishedEvent(torrent_id))
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: try:

View file

@ -97,13 +97,11 @@ class Core(CorePluginBase):
def execute_commands(self, torrent_id, event): def execute_commands(self, torrent_id, event):
torrent = component.get("TorrentManager").torrents[torrent_id] torrent = component.get("TorrentManager").torrents[torrent_id]
info = torrent.get_status(["name", "save_path", "move_on_completed", "move_on_completed_path"]) info = torrent.get_status(["name", "save_path"])
# Grab the torrent name and save path # Grab the torrent name and save path
torrent_name = info["name"] torrent_name = info["name"]
if event == "complete": if event == "added" and not self.torrent_manager.session_started:
save_path = info["move_on_completed_path"] if info ["move_on_completed"] else info["save_path"]
elif event == "added" and not self.torrent_manager.session_started:
return return
else: else:
save_path = info["save_path"] save_path = info["save_path"]

View file

@ -111,7 +111,6 @@ class Core(CorePluginBase):
if not self.config["extract_path"]: if not self.config["extract_path"]:
self.config["extract_path"] = deluge.configmanager.ConfigManager("core.conf")["download_location"] self.config["extract_path"] = deluge.configmanager.ConfigManager("core.conf")["download_location"]
component.get("EventManager").register_event_handler("TorrentFinishedEvent", self._on_torrent_finished) component.get("EventManager").register_event_handler("TorrentFinishedEvent", self._on_torrent_finished)
def disable(self): def disable(self):
component.get("EventManager").deregister_event_handler("TorrentFinishedEvent", self._on_torrent_finished) component.get("EventManager").deregister_event_handler("TorrentFinishedEvent", self._on_torrent_finished)
@ -123,11 +122,7 @@ class Core(CorePluginBase):
This is called when a torrent finishes and checks if any files to extract. This is called when a torrent finishes and checks if any files to extract.
""" """
tid = component.get("TorrentManager").torrents[torrent_id] tid = component.get("TorrentManager").torrents[torrent_id]
tid_status = tid.get_status(["save_path", "move_completed", "name"]) tid_status = tid.get_status(["save_path", "name"])
if tid_status["move_completed"]:
log.warning("EXTRACTOR: Cannot extract torrents with 'Move Completed' enabled")
return
files = tid.get_files() files = tid.get_files()
for f in files: for f in files: