From 97d829779f5ffec280a598d2e9b85e245a9cabe0 Mon Sep 17 00:00:00 2001 From: croneter Date: Mon, 10 Dec 2018 19:51:17 +0100 Subject: [PATCH] Fix OperationalError: enforce Kodi restart with clean DB once - Fixes #570 --- resources/lib/initialsetup.py | 20 +++++++++++++------- resources/lib/service_entry.py | 5 ++++- resources/lib/utils.py | 21 ++++++++++----------- resources/settings.xml | 1 + 4 files changed, 28 insertions(+), 19 deletions(-) diff --git a/resources/lib/initialsetup.py b/resources/lib/initialsetup.py index c5b9c693..de1639f1 100644 --- a/resources/lib/initialsetup.py +++ b/resources/lib/initialsetup.py @@ -511,6 +511,10 @@ class InitialSetup(object): LOG.info("Using PMS %s with machineIdentifier %s", app.CONN.server, app.CONN.machine_identifier) self.save_pms_settings(app.CONN.server, self.pms_token) + if utils.settings('kodi_db_has_been_wiped_clean') == 'false': + # If the user chose to go to the PKC settings on the first run + # Will trigger a reboot + utils.wipe_database() if reboot is True: utils.reboot_kodi() return @@ -531,6 +535,10 @@ class InitialSetup(object): # User already answered the installation questions if utils.settings('InstallQuestionsAnswered') == 'true': LOG.info('Installation questions already answered') + if utils.settings('kodi_db_has_been_wiped_clean') == 'false': + # If the user chose to go to the PKC settings on the first run + # Will trigger a reboot + utils.wipe_database() if reboot is True: utils.reboot_kodi() # Reload relevant settings @@ -594,20 +602,18 @@ class InitialSetup(object): # Make sure that we only ask these questions upon first installation utils.settings('InstallQuestionsAnswered', value='true') - # New installation - make sure we start with a clean slate - utils.wipe_database() - if goto_settings is False: # Open Settings page now? You will need to restart! goto_settings = utils.yesno_dialog(utils.lang(29999), utils.lang(39017)) if goto_settings: - app.APP.suspend = True + app.APP.stop_pkc = True executebuiltin( 'Addon.OpenSettings(plugin.video.plexkodiconnect)') - elif reboot is True: - utils.reboot_kodi() - # Reload relevant settings + # New installation - make sure we start with a clean slate + # Will trigger a reboot, usually + utils.wipe_database() + # Reload relevant settings if that is not the case app.CONN.load() app.ACCOUNT.load() app.SYNC.load() diff --git a/resources/lib/service_entry.py b/resources/lib/service_entry.py index e58642b0..5c1338f4 100644 --- a/resources/lib/service_entry.py +++ b/resources/lib/service_entry.py @@ -94,6 +94,9 @@ class Service(): self.connection_check_running = False self.auth_running = False + def isCanceled(self): + return xbmc.abortRequested or app.APP.stop_pkc + def on_connection_check(self, result): """ Call this method after PF.check_connection() @@ -361,7 +364,7 @@ class Service(): self.playqueue = playqueue.PlayqueueMonitor() # Main PKC program loop - while not xbmc.abortRequested: + while not self.isCanceled(): # Check for Kodi profile change if utils.window('plex_kodiProfile') != v.KODI_PROFILE: # Profile change happened, terminate this thread and others diff --git a/resources/lib/utils.py b/resources/lib/utils.py index 8e95dacf..e7348f6b 100644 --- a/resources/lib/utils.py +++ b/resources/lib/utils.py @@ -403,16 +403,11 @@ def kodi_sql(media_type=None): else: db_path = v.DB_VIDEO_PATH conn = connect(db_path, timeout=5.0) - try: - conn.execute('PRAGMA journal_mode=WAL;') - except OperationalError: - LOG.warn('Issue with sqlite WAL mode - force-rebooting Kodi') - settings('lastfullsync', value='0') - reboot_kodi() + conn.execute('PRAGMA journal_mode=WAL;') conn.execute('PRAGMA cache_size = -8000;') conn.execute('PRAGMA synchronous=NORMAL;') - # Use transactions conn.execute('BEGIN') + # Use transactions return conn @@ -485,7 +480,11 @@ def wipe_database(): settings('SyncInstallRunDone', value="false") settings('lastfullsync', value="0") LOG.info('Wiping done') - init_dbs() + if settings('kodi_db_has_been_wiped_clean') != 'true': + # Root cause is sqlite WAL mode - Kodi might still have DB access open + LOG.warn('Need to restart Kodi before filling Kodi DB again') + settings('kodi_db_has_been_wiped_clean', value='true') + reboot_kodi() def init_dbs(): @@ -526,15 +525,15 @@ def reset(ask_user=True): return xbmc.sleep(1000) - # Wipe everything - wipe_database() - # Reset all PlexKodiConnect Addon settings? (this is usually NOT # recommended and unnecessary!) if ask_user and yesno_dialog(lang(29999), lang(39603)): # Delete the settings LOG.info("Deleting: settings.xml") path_ops.remove("%ssettings.xml" % v.ADDON_PROFILE) + + # Wipe everything + wipe_database() reboot_kodi() diff --git a/resources/settings.xml b/resources/settings.xml index 5feec070..e7febd0e 100644 --- a/resources/settings.xml +++ b/resources/settings.xml @@ -84,6 +84,7 @@ +