From 60c764ac338a83f5fe835e880bf2bac25b33d4da Mon Sep 17 00:00:00 2001 From: Rato Date: Tue, 13 Jun 2017 06:52:20 +0200 Subject: [PATCH] [Core] Save torrent state only if state has changed --- deluge/core/torrentmanager.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/deluge/core/torrentmanager.py b/deluge/core/torrentmanager.py index d143ff0a8..2c86d5e7f 100644 --- a/deluge/core/torrentmanager.py +++ b/deluge/core/torrentmanager.py @@ -81,6 +81,12 @@ class TorrentState: # pylint: disable=old-style-class continue setattr(self, key, value) + def __eq__(self, other): + return isinstance(other, TorrentState) and self.__dict__ == other.__dict__ + + def __ne__(self, other): + return not self == other + class TorrentManagerState: # pylint: disable=old-style-class """TorrentManagerState holds a list of TorrentState objects. @@ -92,6 +98,12 @@ class TorrentManagerState: # pylint: disable=old-style-class def __init__(self): self.torrents = [] + def __eq__(self, other): + return isinstance(other, TorrentManagerState) and self.torrents == other.torrents + + def __ne__(self, other): + return not self == other + class TorrentManager(component.Component): """TorrentManager contains a list of torrents in the current libtorrent session. @@ -138,6 +150,9 @@ class TorrentManager(component.Component): self.status_dict = {} self.last_state_update_alert_ts = 0 + # Keep the previous saved state + self.prev_saved_state = None + # Register set functions set_config_keys = ['max_connections_per_torrent', 'max_upload_slots_per_torrent', 'max_upload_speed_per_torrent', 'max_download_speed_per_torrent'] @@ -617,6 +632,10 @@ class TorrentManager(component.Component): log.debug('Skipping saving state with no torrents loaded') return + # If the state hasn't changed, no need to save it + if self.prev_saved_state == state: + return + filename = 'torrents.state' filepath = os.path.join(self.state_dir, filename) filepath_bak = filepath + '.bak' @@ -645,6 +664,7 @@ class TorrentManager(component.Component): try: log.debug('Saving %s to: %s', filename, filepath) os.rename(filepath_tmp, filepath) + self.prev_saved_state = state except OSError as ex: log.error('Failed to set new state file %s: %s', filepath, ex) if os.path.isfile(filepath_bak):