From e7ab544f93dd054406d4795aa90c36fd721bf4b7 Mon Sep 17 00:00:00 2001 From: Dario Date: Sat, 31 Aug 2019 21:50:29 +0200 Subject: [PATCH 1/3] Reset dirty when changes to episodes are made --- medusa/tv/episode.py | 30 +++++++++++++++++++++--------- 1 file changed, 21 insertions(+), 9 deletions(-) diff --git a/medusa/tv/episode.py b/medusa/tv/episode.py index 74f16b424f..cb5b41d044 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 queue - 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,6 +1328,12 @@ 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}) + 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.""" @@ -1362,6 +1373,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() From 66772bc07bc41ad7070deb72260bdcef4b8a610b Mon Sep 17 00:00:00 2001 From: Dario Date: Sat, 31 Aug 2019 22:10:15 +0200 Subject: [PATCH 2/3] Reset dirty when exception occurs, minor logging changes --- medusa/tv/episode.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/medusa/tv/episode.py b/medusa/tv/episode.py index cb5b41d044..1eb6754ac1 100644 --- a/medusa/tv/episode.py +++ b/medusa/tv/episode.py @@ -1188,7 +1188,7 @@ 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.""" if not self.dirty: - log.debug('{id}: Not creating SQL queue - record is not dirty', + log.debug('{id}: Not creating SQL query - record is not dirty', {'id': self.series.series_id}) return @@ -1328,6 +1328,7 @@ 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 @@ -1340,6 +1341,11 @@ def save_to_db(self): 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, From 3c868b4881c52ab629acab4a71ab74a44e5e5dd2 Mon Sep 17 00:00:00 2001 From: Dario Date: Sat, 31 Aug 2019 22:31:10 +0200 Subject: [PATCH 3/3] Update CHANGELOG.md --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) 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))