Fix OperationalError: database is locked

This commit is contained in:
croneter 2018-11-09 11:19:32 +01:00
parent fd542936b8
commit d16da6fa6f
2 changed files with 51 additions and 34 deletions

View file

@ -5,7 +5,8 @@ from logging import getLogger
import xbmc import xbmc
from .get_metadata import GetMetadataTask, reset_collections 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 utils, backgroundthread, variables as v, state
from .. import plex_functions as PF, itemtypes from .. import plex_functions as PF, itemtypes
from ..plex_db import PlexDB from ..plex_db import PlexDB
@ -51,10 +52,9 @@ class FullSync(common.libsync_mixin):
int('%s%s' % (plex_id, int('%s%s' % (plex_id,
xml_item.get('updatedAt', xml_item.get('updatedAt',
xml_item.get('addedAt', 1541572987)))): xml_item.get('addedAt', 1541572987)))):
# Already got EXACTLY this item in our DB # Already got EXACTLY this item in our DB. BUT need to collect all
self.plexdb.update_last_sync(plex_id, # DB updates within the same thread
self.plex_type, self.queue.put(UpdateLastSync(plex_id))
self.current_sync)
return return
task = GetMetadataTask() task = GetMetadataTask()
task.setup(self.queue, plex_id, self.plex_type, self.get_children) 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'], iterator = PF.SectionItems(section['section_id'],
plex_type=self.plex_type) plex_type=self.plex_type)
# Tell the processing thread about this new section # Tell the processing thread about this new section
queue_info = process_metadata.InitNewSection( queue_info = InitNewSection(self.context,
self.context, utils.cast(int,
utils.cast(int, iterator.get('totalSize', 0)), iterator.get('totalSize', 0)),
iterator.get('librarySectionTitle'), iterator.get('librarySectionTitle'),
section['section_id'], section['section_id'],
self.plex_type) self.plex_type)
self.queue.put(queue_info) self.queue.put(queue_info)
with PlexDB() as self.plexdb: with PlexDB() as self.plexdb:
for xml_item in iterator: for xml_item in iterator:
@ -130,12 +130,12 @@ class FullSync(common.libsync_mixin):
iterator = PF.SectionItems(section['section_id'], iterator = PF.SectionItems(section['section_id'],
plex_type=self.plex_type) plex_type=self.plex_type)
# Tell the processing thread that we're syncing playstate # Tell the processing thread that we're syncing playstate
queue_info = process_metadata.InitNewSection( queue_info = InitNewSection(self.context,
self.context, utils.cast(int,
utils.cast(int, iterator.get('totalSize', 0)), iterator.get('totalSize', 0)),
iterator.get('librarySectionTitle'), iterator.get('librarySectionTitle'),
section['section_id'], section['section_id'],
self.plex_type) self.plex_type)
self.queue.put(queue_info) self.queue.put(queue_info)
# Ensure that the DB connection is closed to commit the # Ensure that the DB connection is closed to commit the
# changes above - avoids "Item not yet synced" error # changes above - avoids "Item not yet synced" error
@ -192,9 +192,10 @@ class FullSync(common.libsync_mixin):
return return
try: try:
# Fire up our single processing thread # Fire up our single processing thread
self.queue = backgroundthread.Queue.Queue(maxsize=400) self.queue = backgroundthread.Queue.Queue(maxsize=600)
self.processing_thread = process_metadata.ProcessMetadata( self.processing_thread = ProcessMetadata(self.queue,
self.queue, self.current_sync, self.show_dialog) self.current_sync,
self.show_dialog)
self.processing_thread.start() self.processing_thread.start()
# Actual syncing - do only new items first # Actual syncing - do only new items first

View file

@ -29,6 +29,11 @@ class InitNewSection(object):
self.plex_type = plex_type 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): class ProcessMetadata(backgroundthread.KillableThread, common.libsync_mixin):
""" """
Not yet implemented for more than 1 thread - if ever. Only to be called by 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.show_dialog = show_dialog
self.total = 0 self.total = 0
self.current = 1 self.current = 1
self.title = None self.processed = 0
self.title = ''
self.section_name = None self.section_name = None
self.dialog = None self.dialog = None
super(ProcessMetadata, self).__init__() super(ProcessMetadata, self).__init__()
@ -81,8 +87,10 @@ class ProcessMetadata(backgroundthread.KillableThread, common.libsync_mixin):
while not self.isCanceled(): while not self.isCanceled():
if section is None: if section is None:
break break
LOG.debug('Start processing section %s', section) LOG.debug('Start processing section %s: %s',
section.plex_type, section.name)
self.current = 1 self.current = 1
self.processed = 0
self.total = section.total self.total = section.total
self.section_name = section.name self.section_name = section.name
profile = Profile() profile = Profile()
@ -95,21 +103,29 @@ class ProcessMetadata(backgroundthread.KillableThread, common.libsync_mixin):
section = item section = item
self.queue.task_done() self.queue.task_done()
break break
try: elif isinstance(item, UpdateLastSync):
context.add_update(item['xml'][0], context.plexdb.update_last_sync(item.plex_id,
section_name=section.name, section.plex_type,
section_id=section.id, self.last_sync)
children=item['children']) else:
except: try:
utils.ERROR(txt='process_metadata crashed', context.add_update(item['xml'][0],
notify=True, section_name=section.name,
cancel_sync=True) section_id=section.id,
self.title = item['xml'][0].get('title') 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.update_progressbar()
self.current += 1 self.current += 1
if self.current % 200 == 0: if self.processed == 500:
context.plexconn.commit() self.processed = 0
context.kodiconn.commit() context.kodiconn.commit()
context.artconn.commit()
context.plexconn.commit()
self.queue.task_done() self.queue.task_done()
profile.disable() profile.disable()
string_io = StringIO() string_io = StringIO()