Fix OperationalError: Database is locked when video was deleted

This commit is contained in:
croneter 2018-11-25 17:21:32 +01:00
parent 6dc436da91
commit 7670aa7a14
2 changed files with 74 additions and 65 deletions

View file

@ -4,7 +4,8 @@ from __future__ import absolute_import, division, unicode_literals
from logging import getLogger from logging import getLogger
from .get_metadata import GetMetadataTask, reset_collections from .get_metadata import GetMetadataTask, reset_collections
from .process_metadata import InitNewSection, UpdateLastSync, ProcessMetadata from .process_metadata import InitNewSection, UpdateLastSync, ProcessMetadata, \
DeleteItem
from . import common, sections from . import common, sections
from .. import utils, timing, backgroundthread, variables as v, app from .. import utils, timing, backgroundthread, variables as v, app
from .. import plex_functions as PF, itemtypes from .. import plex_functions as PF, itemtypes
@ -64,12 +65,11 @@ class FullSync(common.libsync_mixin):
Removes all the items that have NOT been updated (last_sync timestamp Removes all the items that have NOT been updated (last_sync timestamp
is different) is different)
""" """
with self.context(self.current_sync) as c:
for plex_id in self.plexdb.plex_id_by_last_sync(self.plex_type, for plex_id in self.plexdb.plex_id_by_last_sync(self.plex_type,
self.current_sync): self.current_sync):
if self.isCanceled(): if self.isCanceled():
return return
c.remove(plex_id, plex_type=self.plex_type) self.queue.put(DeleteItem(plex_id))
@utils.log_time @utils.log_time
def process_playstate(self, iterator): def process_playstate(self, iterator):

View file

@ -34,6 +34,11 @@ class UpdateLastSync(object):
self.plex_id = plex_id self.plex_id = plex_id
class DeleteItem(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
@ -71,14 +76,27 @@ class ProcessMetadata(backgroundthread.KillableThread, common.libsync_mixin):
% (self.current, self.total, self.title)) % (self.current, self.total, self.title))
def run(self): def run(self):
"""
Do the work
"""
LOG.debug('Processing thread started') LOG.debug('Processing thread started')
try:
if self.show_dialog: if self.show_dialog:
self.dialog = xbmcgui.DialogProgressBG() self.dialog = xbmcgui.DialogProgressBG()
self.dialog.create(utils.lang(39714)) self.dialog.create(utils.lang(39714))
try:
self._run()
except:
utils.ERROR(notify=True, cancel_sync=True)
finally:
if self.dialog:
self.dialog.close()
while not self.queue.empty():
# We need to empty the queue to let full_sync finish join()
self.queue.get()
self.queue.task_done()
LOG.debug('Processing thread terminated')
def _run(self):
"""
Do the work
"""
# Init with the very first library section. This will block! # Init with the very first library section. This will block!
section = self.queue.get() section = self.queue.get()
self.queue.task_done() self.queue.task_done()
@ -101,24 +119,23 @@ class ProcessMetadata(backgroundthread.KillableThread, common.libsync_mixin):
while not self.isCanceled(): while not self.isCanceled():
# grabs item from queue. This will block! # grabs item from queue. This will block!
item = self.queue.get() item = self.queue.get()
if isinstance(item, InitNewSection) or item is None: if isinstance(item, dict):
section = item
self.queue.task_done()
break
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], context.add_update(item['xml'][0],
section_name=section.name, section_name=section.name,
section_id=section.id, section_id=section.id,
children=item['children']) children=item['children'])
except:
utils.ERROR(notify=True, cancel_sync=True)
self.title = item['xml'][0].get('title') self.title = item['xml'][0].get('title')
self.processed += 1 self.processed += 1
elif isinstance(item, UpdateLastSync):
context.plexdb.update_last_sync(item.plex_id,
section.plex_type,
self.last_sync)
elif isinstance(item, InitNewSection) or item is None:
section = item
self.queue.task_done()
break
else:
context.remove(item.plex_id, plex_type=section.plex_type)
self.update_progressbar() self.update_progressbar()
self.current += 1 self.current += 1
if self.processed == 500: if self.processed == 500:
@ -131,11 +148,3 @@ class ProcessMetadata(backgroundthread.KillableThread, common.libsync_mixin):
stats.print_stats() stats.print_stats()
LOG.info('cProfile result: ') LOG.info('cProfile result: ')
LOG.info(string_io.getvalue()) LOG.info(string_io.getvalue())
finally:
if self.dialog:
self.dialog.close()
while not self.queue.empty():
# We need to empty the queue to let full_sync finish join()
self.queue.get()
self.queue.task_done()
LOG.debug('Processing thread terminated')