diff --git a/resources/lib/library_sync/full_sync.py b/resources/lib/library_sync/full_sync.py index 296a2805..9dab3bd9 100644 --- a/resources/lib/library_sync/full_sync.py +++ b/resources/lib/library_sync/full_sync.py @@ -5,7 +5,8 @@ from logging import getLogger import xbmc from .get_metadata import GetMetadataTask, reset_collections -from . import common, process_metadata, sections +from .process_metadata import InitNewSection, UpdateLastSync, ProcessMetadata +from . import common, sections from .. import utils, backgroundthread, variables as v, state from .. import plex_functions as PF, itemtypes from ..plex_db import PlexDB @@ -51,10 +52,9 @@ class FullSync(common.libsync_mixin): int('%s%s' % (plex_id, xml_item.get('updatedAt', xml_item.get('addedAt', 1541572987)))): - # Already got EXACTLY this item in our DB - self.plexdb.update_last_sync(plex_id, - self.plex_type, - self.current_sync) + # Already got EXACTLY this item in our DB. BUT need to collect all + # DB updates within the same thread + self.queue.put(UpdateLastSync(plex_id)) return task = GetMetadataTask() task.setup(self.queue, plex_id, self.plex_type, self.get_children) @@ -103,12 +103,12 @@ class FullSync(common.libsync_mixin): iterator = PF.SectionItems(section['section_id'], plex_type=self.plex_type) # Tell the processing thread about this new section - queue_info = process_metadata.InitNewSection( - self.context, - utils.cast(int, iterator.get('totalSize', 0)), - iterator.get('librarySectionTitle'), - section['section_id'], - self.plex_type) + queue_info = InitNewSection(self.context, + utils.cast(int, + iterator.get('totalSize', 0)), + iterator.get('librarySectionTitle'), + section['section_id'], + self.plex_type) self.queue.put(queue_info) with PlexDB() as self.plexdb: for xml_item in iterator: @@ -130,12 +130,12 @@ class FullSync(common.libsync_mixin): iterator = PF.SectionItems(section['section_id'], plex_type=self.plex_type) # Tell the processing thread that we're syncing playstate - queue_info = process_metadata.InitNewSection( - self.context, - utils.cast(int, iterator.get('totalSize', 0)), - iterator.get('librarySectionTitle'), - section['section_id'], - self.plex_type) + queue_info = InitNewSection(self.context, + utils.cast(int, + iterator.get('totalSize', 0)), + iterator.get('librarySectionTitle'), + section['section_id'], + self.plex_type) self.queue.put(queue_info) # Ensure that the DB connection is closed to commit the # changes above - avoids "Item not yet synced" error @@ -192,9 +192,10 @@ class FullSync(common.libsync_mixin): return try: # Fire up our single processing thread - self.queue = backgroundthread.Queue.Queue(maxsize=400) - self.processing_thread = process_metadata.ProcessMetadata( - self.queue, self.current_sync, self.show_dialog) + self.queue = backgroundthread.Queue.Queue(maxsize=600) + self.processing_thread = ProcessMetadata(self.queue, + self.current_sync, + self.show_dialog) self.processing_thread.start() # Actual syncing - do only new items first diff --git a/resources/lib/library_sync/process_metadata.py b/resources/lib/library_sync/process_metadata.py index 1c9d1372..f0c467a5 100644 --- a/resources/lib/library_sync/process_metadata.py +++ b/resources/lib/library_sync/process_metadata.py @@ -29,6 +29,11 @@ class InitNewSection(object): self.plex_type = plex_type +class UpdateLastSync(object): + def __init__(self, plex_id): + self.plex_id = plex_id + + class ProcessMetadata(backgroundthread.KillableThread, common.libsync_mixin): """ Not yet implemented for more than 1 thread - if ever. Only to be called by @@ -48,7 +53,8 @@ class ProcessMetadata(backgroundthread.KillableThread, common.libsync_mixin): self.show_dialog = show_dialog self.total = 0 self.current = 1 - self.title = None + self.processed = 0 + self.title = '' self.section_name = None self.dialog = None super(ProcessMetadata, self).__init__() @@ -81,8 +87,10 @@ class ProcessMetadata(backgroundthread.KillableThread, common.libsync_mixin): while not self.isCanceled(): if section is None: break - LOG.debug('Start processing section %s', section) + LOG.debug('Start processing section %s: %s', + section.plex_type, section.name) self.current = 1 + self.processed = 0 self.total = section.total self.section_name = section.name profile = Profile() @@ -95,21 +103,29 @@ class ProcessMetadata(backgroundthread.KillableThread, common.libsync_mixin): section = item self.queue.task_done() break - try: - context.add_update(item['xml'][0], - section_name=section.name, - section_id=section.id, - children=item['children']) - except: - utils.ERROR(txt='process_metadata crashed', - notify=True, - cancel_sync=True) - self.title = item['xml'][0].get('title') + elif isinstance(item, UpdateLastSync): + context.plexdb.update_last_sync(item.plex_id, + section.plex_type, + self.last_sync) + else: + try: + context.add_update(item['xml'][0], + section_name=section.name, + section_id=section.id, + children=item['children']) + except: + utils.ERROR(txt='process_metadata crashed', + notify=True, + cancel_sync=True) + self.title = item['xml'][0].get('title') + self.processed += 1 self.update_progressbar() self.current += 1 - if self.current % 200 == 0: - context.plexconn.commit() + if self.processed == 500: + self.processed = 0 context.kodiconn.commit() + context.artconn.commit() + context.plexconn.commit() self.queue.task_done() profile.disable() string_io = StringIO()