From 9335bb5347e1919c7d975f525e662c746a06adc3 Mon Sep 17 00:00:00 2001 From: Dario Date: Sat, 31 Aug 2019 23:04:53 +0200 Subject: [PATCH] Reset dirty flag when changes to episodes are made (#7102) * Reset dirty when changes to episodes are made * Reset dirty when exception occurs, minor logging changes * Update CHANGELOG.md --- CHANGELOG.md | 1 + medusa/tv/episode.py | 36 +++++++++++++++++++++++++++--------- 2 files changed, 28 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 877876bb1c..27ab5c00fa 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ - Improved API v2 exception reporting on Python 2 ([#6931](https://github.com/pymedusa/Medusa/pull/6931)) - Added support for qbittorrent api v2. Required from qbittorrent version > 3.2.0. ([#7040](https://github.com/pymedusa/Medusa/pull/7040)) - Show Header: Improved visibility of local and global configured required and ignored words. ([#7085](https://github.com/pymedusa/Medusa/pull/7085)) +- Reduced frequency of file system access when not strictly required ([#7102](https://github.com/pymedusa/Medusa/pull/7102)) #### Fixes - Fixed hdtorrent provider parse the publishing date with the day first ([#6847](https://github.com/pymedusa/Medusa/pull/6847)) diff --git a/medusa/tv/episode.py b/medusa/tv/episode.py index 74f16b424f..1eb6754ac1 100644 --- a/medusa/tv/episode.py +++ b/medusa/tv/episode.py @@ -609,6 +609,7 @@ def load_from_db(self, season, episode): """ if self.loaded: return True + main_db_con = db.DBConnection() sql_results = main_db_con.select( 'SELECT ' @@ -653,7 +654,7 @@ def load_from_db(self, season, episode): # don't overwrite my location if sql_results[0]['location']: - self.location = os.path.normpath(sql_results[0]['location']) + self._location = os.path.normpath(sql_results[0]['location']) if sql_results[0]['file_size']: self.file_size = int(sql_results[0]['file_size']) else: @@ -961,6 +962,8 @@ def load_from_indexer(self, season=None, episode=None, tvapi=None, cached_season ) self.status = UNSET + self.save_to_db() + def __load_from_nfo(self, location): if not self.series.is_location_valid(): @@ -1044,6 +1047,8 @@ def __load_from_nfo(self, location): self.hastbn = bool(os.path.isfile(replace_extension(nfo_file, 'tbn'))) + self.save_to_db() + def __str__(self): """Represent a string. @@ -1182,12 +1187,12 @@ def delete_episode(self): def get_sql(self): """Create SQL queue for this episode if any of its data has been changed since the last save.""" - try: - if not self.dirty: - log.debug('{id}: Not creating SQL queue - record is not dirty', - {'id': self.series.series_id}) - return + if not self.dirty: + log.debug('{id}: Not creating SQL query - record is not dirty', + {'id': self.series.series_id}) + return + try: main_db_con = db.DBConnection() rows = main_db_con.select( 'SELECT ' @@ -1210,7 +1215,7 @@ def get_sql(self): # use a custom update method to get the data into the DB for existing records. # Multi or added subtitle or removed subtitles if app.SUBTITLES_MULTI or not rows[0]['subtitles'] or not self.subtitles: - return [ + sql_query = [ 'UPDATE ' ' tv_episodes ' 'SET ' @@ -1248,7 +1253,7 @@ def get_sql(self): else: # Don't update the subtitle language when the srt file doesn't contain the # alpha2 code, keep value from subliminal - return [ + sql_query = [ 'UPDATE ' ' tv_episodes ' 'SET ' @@ -1284,7 +1289,7 @@ def get_sql(self): self.version, self.release_group, self.manually_searched, self.watched, ep_id]] else: # use a custom insert method to get the data into the DB. - return [ + sql_query = [ 'INSERT OR IGNORE INTO ' ' tv_episodes ' ' (episode_id, ' @@ -1323,12 +1328,24 @@ def get_sql(self): except Exception as error: log.error('{id}: Error while updating database: {error_msg!r}', {'id': self.series.series_id, 'error_msg': error}) + self.reset_dirty() + return + + self.loaded = False + self.reset_dirty() + + return sql_query def save_to_db(self): """Save this episode to the database if any of its data has been changed since the last save.""" if not self.dirty: return + log.debug('{id}: Saving episode to database: {show} {ep}', + {'id': self.series.series_id, + 'show': self.series.name, + 'ep': episode_num(self.season, self.episode)}) + new_value_dict = { 'indexerid': self.indexerid, 'name': self.name, @@ -1362,6 +1379,7 @@ def save_to_db(self): # use a custom update/insert method to get the data into the DB main_db_con = db.DBConnection() main_db_con.upsert('tv_episodes', new_value_dict, control_value_dict) + self.loaded = False self.reset_dirty()