Fix library sync

Adjust lock, re-add screensaver deactivated during sync, prep compare sync, stop library updates from being processed before startup sync is completed
This commit is contained in:
angelblue05 2018-10-03 00:12:30 -05:00
parent b8ea16ed46
commit 8d838d257e
4 changed files with 246 additions and 153 deletions

View file

@ -276,14 +276,14 @@ class Service(xbmc.Monitor):
self.connect.setup_manual_server()
elif method == 'UserDataChanged' and self.library_thread:
if data.get('ServerId'):
if data.get('ServerId') or not window('emby_startup.bool'):
return
LOG.info("[ UserDataChanged ] %s", data)
self.library_thread.userdata(data['UserDataList'])
elif method == 'LibraryChanged' and self.library_thread:
if data.get('ServerId'):
if data.get('ServerId') or not window('emby_startup.bool'):
return
LOG.info("[ LibraryChanged ] %s", data)
@ -415,11 +415,9 @@ class Service(xbmc.Monitor):
properties = [ # TODO: review
"emby_state", "emby_serverStatus",
"emby_syncRunning", "emby_dbCheck",
"emby_currUser", "emby_dbScan",
"emby_initialScan",
"emby_syncRunning", "emby_currUser",
"emby_play", "emby_online", "emby.connected", "emby.resume",
"emby_play", "emby_online", "emby.connected", "emby.resume", "emby_startup",
"emby.external", "emby.external_check", "emby_deviceId", "emby_db_check", "emby_pathverified"
]
for prop in properties:

View file

@ -15,6 +15,7 @@ import helper.xmls as xmls
from database import Database, get_sync, save_sync, emby_db
from objects import Movies, TVShows, MusicVideos, Music
from helper import _, settings, progress, dialog, LibraryException
from helper.utils import get_screensaver, set_screensaver
from emby import Emby
##################################################################################################
@ -102,7 +103,7 @@ class FullSync(object):
def select_libraries(self, libraries):
''' Select all or whitelist libraries. Provides a new list.
''' Select all or certain libraries to be whitelisted.
'''
if dialog("yesno", heading="{emby}", line1=_(33125), nolabel=_(33127), yeslabel=_(33126)):
LOG.info("Selected sync later.")
@ -145,6 +146,13 @@ class FullSync(object):
save_sync(self.sync)
start_time = datetime.datetime.now()
if not settings('dbSyncScreensaver.bool'):
xbmc.executebuiltin('InhibitIdleShutdown(true)')
screensaver = get_screensaver()
set_screensaver(value="")
try:
for library in list(self.sync['Libraries']):
self.process_library(library)
@ -154,11 +162,20 @@ class FullSync(object):
self.sync['Libraries'].pop(self.sync['Libraries'].index(library))
self.sync['RestorePoint'] = {}
except Exception as error:
if not settings('dbSyncScreensaver.bool'):
xbmc.executebuiltin('InhibitIdleShutdown(false)')
set_screensaver(value=screensaver)
raise
elapsed = datetime.datetime.now() - start_time
settings('SyncInstallRunDone.bool', True)
self.library.save_last_sync()
save_sync(self.sync)
xbmc.executebuiltin('UpdateLibrary(video)')
dialog("notification", heading="{emby}", message="%s %s" % (_(33025), str(elapsed).split('.')[0]),
icon="{emby}", sound=False)
@ -219,8 +236,10 @@ class FullSync(object):
''' Process movies from a single library.
'''
with self.library.database_lock:
with Database() as videodb:
with Database('emby') as embydb:
obj = Movies(self.server, embydb, videodb, self.direct_path)
for items in server.get_items(library['Id'], "Movie", False, self.sync['RestorePoint'].get('params')):
@ -235,11 +254,27 @@ class FullSync(object):
message=movie['Name'])
obj.movie(movie, library=library)
#self.movies_compare(library, obj, embydb)
def movies_compare(self, library, obj, embydb):
''' Compare entries from library to what's in the embydb. Remove surplus
'''
db = emby_db.EmbyDatabase(embydb.cursor)
items = db.get_item_by_media_folder(library['Id'])
current = obj.item_ids
for x in items:
if x not in current:
obj.remove(x)
@progress()
def tvshows(self, library, dialog):
''' Process tvshows and episodes from a single library.
'''
with self.library.database_lock:
with Database() as videodb:
with Database('emby') as embydb:
obj = TVShows(self.server, embydb, videodb, self.direct_path)
@ -263,11 +298,25 @@ class FullSync(object):
dialog.update(percent, message="%s/%s" % (message, episode['Name'][:10]))
obj.episode(episode)
def tvshows_compare(self, library, obj, embydb):
''' Compare entries from library to what's in the embydb. Remove surplus
'''
db = emby_db.EmbyDatabase(embydb.cursor)
items = db.get_item_by_media_folder(library['Id'])
current = obj.item_ids
for x in items:
if x not in current:
obj.remove(x)
@progress()
def musicvideos(self, library, dialog):
''' Process musicvideos from a single library.
'''
with self.library.database_lock:
with Database() as videodb:
with Database('emby') as embydb:
obj = MusicVideos(self.server, embydb, videodb, self.direct_path)
@ -284,11 +333,27 @@ class FullSync(object):
message=mvideo['Name'])
obj.musicvideo(mvideo, library=library)
#self.movies_compare(library, obj, embydb)
def musicvideos_compare(self, library, obj, embydb):
''' Compare entries from library to what's in the embydb. Remove surplus
'''
db = emby_db.EmbyDatabase(embydb.cursor)
items = db.get_item_by_media_folder(library['Id'])
current = obj.item_ids
for x in items:
if x not in current:
obj.remove(x)
@progress()
def music(self, library, dialog):
''' Process artists, album, songs from a single library.
'''
with self.library.music_database_lock:
with Database('music') as musicdb:
with Database('emby') as embydb:
obj = Music(self.server, embydb, musicdb, self.direct_path)
@ -322,6 +387,7 @@ class FullSync(object):
''' Process all boxsets.
'''
with self.library.database_lock:
with Database() as videodb:
with Database('emby') as embydb:
obj = Movies(self.server, embydb, videodb, self.direct_path)
@ -342,6 +408,7 @@ class FullSync(object):
''' Delete all exisitng boxsets and re-add.
'''
with self.library.database_lock:
with Database() as videodb:
with Database('emby') as embydb:

View file

@ -172,6 +172,27 @@ def should_stop():
return False
def get_screensaver():
''' Get the current screensaver value.
'''
result = JSONRPC('Settings.getSettingValue').execute({'setting': "screensaver.mode"})
try:
return result['result']['value']
except KeyError:
return ""
def set_screensaver(value):
''' Toggle the screensaver
'''
params = {
'setting': "screensaver.mode",
'value': value
}
result = JSONRPC('Settings.setSettingValue').execute(params)
LOG.info("---[ screensaver/%s ] %s", value, result)
class JSONRPC(object):
version = 1

View file

@ -16,7 +16,7 @@ from full_sync import FullSync
from views import Views
from downloader import GetItemWorker
from helper import _, stop, settings, window, dialog, event, progress, LibraryException
from helper.utils import split_list
from helper.utils import split_list, set_screensaver, get_screensaver
from emby import Emby
##################################################################################################
@ -47,6 +47,7 @@ class Library(threading.Thread):
stop_thread = False
suspend = False
pending_refresh = False
screensaver = ""
def __init__(self, monitor):
@ -90,6 +91,8 @@ class Library(threading.Thread):
if not self.startup():
self.stop_client()
window('emby_startup.bool', True)
while not self.stop_thread:
try:
@ -179,17 +182,27 @@ class Library(threading.Thread):
self.writer_threads['removed'].append(new_thread)
self.pending_refresh = True
if self.pending_refresh:
if not settings('dbSyncScreensaver.bool'):
xbmc.executebuiltin('InhibitIdleShutdown(true)')
self.screensaver = get_screensaver()
set_screensaver(value="")
if (self.pending_refresh and not self.download_threads and not self.writer_threads['updated'] and
not self.writer_threads['userdata'] and not self.writer_threads['removed']):
self.pending_refresh = False
self.save_last_sync()
if xbmc.getCondVisibility('Container.Content(musicvideos)'): # Prevent cursor from moving
if not settings('dbSyncScreensaver.bool'):
xbmc.executebuiltin('InhibitIdleShutdown(false)')
set_screensaver(value=self.screensaver)
if xbmc.getCondVisibility('Container.Content(musicvideos)') or xbmc.getCondVisibility('Window.IsMedia'): # Prevent cursor from moving
xbmc.executebuiltin('Container.Refresh')
elif not xbmc.getCondVisibility('Window.IsMedia'): # Update widgets
else: # Update widgets
xbmc.executebuiltin('UpdateLibrary(video)')
else: # Update listing
xbmc.executebuiltin('Container.Refresh')
def stop_client(self):
self.stop_thread = True
@ -198,42 +211,33 @@ class Library(threading.Thread):
''' Run at startup. Will check for the server plugin.
'''
fast_sync = False
Views().get_views()
Views().get_nodes()
try:
if not settings('kodiCompanion.bool') and settings('SyncInstallRunDone.bool'):
return True
if settings('kodiCompanion.bool'):
for plugin in self.server['api'].get_plugins():
if plugin['Name'] in ("Emby.Kodi Sync Queue", "Kodi companion"):
fast_sync = True
break
else:
raise LibraryException('CompanionMissing')
if get_sync()['Libraries']:
if get_sync()['Libraries'] or not settings('SyncInstallRunDone.bool'):
FullSync(self)
Views().get_nodes()
if settings('SyncInstallRunDone.bool'):
if settings('kodiCompanion.bool'):
if fast_sync and not self.fast_sync():
for plugin in self.server['api'].get_plugins():
if plugin['Name'] in ("Emby.Kodi Sync Queue", "Kodi companion"):
if not self.fast_sync():
dialog("ok", heading="{emby}", line1=_(33128))
raise Exception("Failed to retrieve latest updates")
LOG.info("--<[ retrieve changes ]")
break
else:
FullSync(self)
Views().get_nodes()
raise LibraryException('CompanionMissing')
return True
except LibraryException as error:
LOG.error(error.status)
@ -522,7 +526,6 @@ class UpdatedWorker(threading.Thread):
def run(self):
with self.lock:
with self.database as kodidb:
with Database('emby') as embydb:
@ -537,17 +540,19 @@ class UpdatedWorker(threading.Thread):
break
with self.lock:
obj = MEDIA[item['Type']](self.args[0], embydb, kodidb, self.args[1])[item['Type']]
try:
obj(item)
self.queue.task_done()
except LibraryException as error:
if error.status == 'StopCalled':
break
except Exception as error:
LOG.exception(error)
self.queue.task_done()
if window('emby_should_stop.bool'):
break
@ -565,7 +570,6 @@ class UserDataWorker(threading.Thread):
def run(self):
with self.lock:
with self.database as kodidb:
with Database('emby') as embydb:
@ -580,17 +584,19 @@ class UserDataWorker(threading.Thread):
break
with self.lock:
obj = MEDIA[item['Type']](self.args[0], embydb, kodidb, self.args[1])['UserData']
try:
obj(item)
self.queue.task_done()
except LibraryException as error:
if error.status == 'StopCalled':
break
except Exception as error:
LOG.exception(error)
self.queue.task_done()
if window('emby_should_stop.bool'):
break
@ -652,14 +658,13 @@ class RemovedWorker(threading.Thread):
def run(self):
with self.lock:
with self.database as kodidb:
with Database('emby') as embydb:
while True:
try:
item = self.queue.get(timeout=3)
item = self.queue.get(timeout=2)
except Queue.Empty:
LOG.info("--<[ q:removed/%s ]", id(self))
@ -667,17 +672,19 @@ class RemovedWorker(threading.Thread):
break
with self.lock:
obj = MEDIA[item['Type']](self.args[0], embydb, kodidb, self.args[1])['Remove']
try:
obj(item['Id'])
self.queue.task_done()
except LibraryException as error:
if error.status == 'StopCalled':
break
except Exception as error:
LOG.exception(error)
self.queue.task_done()
if window('emby_should_stop.bool'):
break