Merge pull request #777 from croneter/fix-profile-switch

Fix PKC synching playstate to wrong user on profile switch. Be aware that Kodi profile switches are error-prone
This commit is contained in:
croneter 2019-03-17 17:59:20 +01:00 committed by GitHub
commit a8a229967c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 44 additions and 22 deletions

View file

@ -258,6 +258,7 @@ class FullSync(common.fullsync_mixin):
for section in (x for x in sections.SECTIONS for section in (x for x in sections.SECTIONS
if x.section_type == kind[1]): if x.section_type == kind[1]):
if self.isCanceled(): if self.isCanceled():
LOG.debug('Need to exit now')
return return
if not section.sync_to_kodi: if not section.sync_to_kodi:
LOG.info('User chose to not sync section %s', section) LOG.info('User chose to not sync section %s', section)
@ -283,6 +284,8 @@ class FullSync(common.fullsync_mixin):
self.section_success = False self.section_success = False
else: else:
queue.put(element) queue.put(element)
except Exception:
utils.ERROR(notify=True)
finally: finally:
queue.put(None) queue.put(None)
@ -410,7 +413,7 @@ class FullSync(common.fullsync_mixin):
def _run(self): def _run(self):
self.current_sync = timing.plex_now() self.current_sync = timing.plex_now()
# Get latest Plex libraries and build playlist and video node files # Get latest Plex libraries and build playlist and video node files
if not sections.sync_from_pms(self): if self.isCanceled() or not sections.sync_from_pms(self):
return return
self.successful = True self.successful = True
try: try:
@ -422,10 +425,7 @@ class FullSync(common.fullsync_mixin):
# Actual syncing - do only new items first # Actual syncing - do only new items first
LOG.info('Running full_library_sync with repair=%s', LOG.info('Running full_library_sync with repair=%s',
self.repair) self.repair)
if not self.full_library_sync(): if self.isCanceled() or not self.full_library_sync():
self.successful = False
return
if self.isCanceled():
self.successful = False self.successful = False
return return
if common.PLAYLIST_SYNC_ENABLED and not playlists.full_sync(): if common.PLAYLIST_SYNC_ENABLED and not playlists.full_sync():
@ -437,6 +437,7 @@ class FullSync(common.fullsync_mixin):
self.dialog.close() self.dialog.close()
if self.threader: if self.threader:
self.threader.shutdown() self.threader.shutdown()
self.threader = None
if not self.successful and not self.isCanceled(): if not self.successful and not self.isCanceled():
# "ERROR in library sync" # "ERROR in library sync"
utils.dialog('notification', utils.dialog('notification',

View file

@ -289,7 +289,9 @@ class SubscriptionMgr(object):
# To avoid RuntimeError, don't use self.lastplayers # To avoid RuntimeError, don't use self.lastplayers
for playerid in (0, 1, 2): for playerid in (0, 1, 2):
self.last_params['state'] = 'stopped' self.last_params['state'] = 'stopped'
self._send_pms_notification(playerid, self.last_params) self._send_pms_notification(playerid,
self.last_params,
timeout=0.0001)
def _plex_stream_index(self, playerid, stream_type): def _plex_stream_index(self, playerid, stream_type):
""" """
@ -399,7 +401,11 @@ class SubscriptionMgr(object):
self.last_params = params self.last_params = params
return params return params
def _send_pms_notification(self, playerid, params): def _send_pms_notification(self, playerid, params, timeout=None):
"""
Pass a really low timeout in seconds if shutting down Kodi and we don't
need the PMS' response
"""
serv = self._server_by_host(self.server) serv = self._server_by_host(self.server)
playqueue = PQ.PLAYQUEUES[playerid] playqueue = PQ.PLAYQUEUES[playerid]
xargs = params_pms() xargs = params_pms()
@ -416,7 +422,8 @@ class SubscriptionMgr(object):
DU().downloadUrl(url, DU().downloadUrl(url,
authenticate=False, authenticate=False,
parameters=xargs, parameters=xargs,
headerOverride=HEADERS_PMS) headerOverride=HEADERS_PMS,
timeout=timeout)
LOG.debug("Sent server notification with parameters: %s to %s", LOG.debug("Sent server notification with parameters: %s to %s",
xargs, url) xargs, url)

View file

@ -451,12 +451,15 @@ class Service(object):
self.choose_plex_libraries() self.choose_plex_libraries()
elif plex_command == 'RESET-PKC': elif plex_command == 'RESET-PKC':
utils.reset() utils.reset()
elif plex_command == 'EXIT-PKC':
LOG.info('Received command from another instance to quit')
app.APP.stop_pkc = True
if task: if task:
backgroundthread.BGThreader.addTasksToFront([task]) backgroundthread.BGThreader.addTasksToFront([task])
continue continue
if app.APP.suspend: if app.APP.suspend:
app.APP.monitor.waitForAbort(0.1) xbmc.sleep(100)
continue continue
# Before proceeding, need to make sure: # Before proceeding, need to make sure:
@ -495,7 +498,7 @@ class Service(object):
if utils.settings('enable_alexa') == 'true': if utils.settings('enable_alexa') == 'true':
self.alexa.start() self.alexa.start()
app.APP.monitor.waitForAbort(0.1) xbmc.sleep(100)
# EXITING PKC # EXITING PKC
# Tell all threads to terminate (e.g. several lib sync threads) # Tell all threads to terminate (e.g. several lib sync threads)
@ -506,26 +509,37 @@ class Service(object):
library_sync.clear_window_vars() library_sync.clear_window_vars()
# Will block until threads have quit # Will block until threads have quit
app.APP.stop_threads() app.APP.stop_threads()
utils.window('plex_service_started', clear=True)
LOG.info("======== STOP %s ========", v.ADDON_NAME)
def start(): def start():
# Safety net - Kody starts PKC twice upon first installation! # Safety net - Kody starts PKC twice upon first installation!
if utils.window('plex_service_started') == 'true': if utils.window('plex_service_started') == 'true':
EXIT = True LOG.info('Another service.py instance is already running - shutting '
else: 'it down now')
# Telling the other Python instance of PKC to shut down now
i = 0
while utils.window('plexkodiconnect.command'):
xbmc.sleep(20)
i += 1
if i > 300:
LOG.error('Could not tell other PKC instance to shut down')
return
utils.window('plexkodiconnect.command', value='EXIT-PKC')
# Telling successful - now wait for actual shut-down
i = 0
while utils.window('plex_service_started'):
xbmc.sleep(20)
i += 1
if i > 300:
LOG.error('Could not shut down other PKC instance')
return
utils.window('plex_service_started', value='true') utils.window('plex_service_started', value='true')
EXIT = False
# Delay option
DELAY = int(utils.settings('startupDelay')) DELAY = int(utils.settings('startupDelay'))
LOG.info("Delaying Plex startup by: %s sec...", DELAY) LOG.info("Delaying Plex startup by: %s sec...", DELAY)
if EXIT: if DELAY and xbmc.Monitor().waitForAbort(DELAY):
LOG.error('PKC service.py already started - exiting this instance')
elif DELAY and xbmc.Monitor().waitForAbort(DELAY):
# Start the service # Start the service
LOG.info("Abort requested while waiting. PKC not started.") LOG.info("Abort requested while waiting. PKC not started.")
else: else:
Service().ServiceEntryPoint() Service().ServiceEntryPoint()
utils.window('plex_service_started', clear=True)
LOG.info("======== STOP PlexKodiConnect service ========")