Fix database is locked

- Fixes #580
This commit is contained in:
croneter 2018-12-25 19:12:49 +01:00
parent 4ee828dfe9
commit 50686ae191
2 changed files with 43 additions and 26 deletions

View file

@ -15,6 +15,7 @@ LOG = getLogger('PLEX.sync.fanart')
SUPPORTED_TYPES = (v.PLEX_TYPE_MOVIE, v.PLEX_TYPE_SHOW) SUPPORTED_TYPES = (v.PLEX_TYPE_MOVIE, v.PLEX_TYPE_SHOW)
SYNC_FANART = utils.settings('FanartTV') == 'true' SYNC_FANART = utils.settings('FanartTV') == 'true'
PREFER_KODI_COLLECTION_ART = utils.settings('PreferKodiCollectionArt') == 'false' PREFER_KODI_COLLECTION_ART = utils.settings('PreferKodiCollectionArt') == 'false'
BATCH_SIZE = 500
def suspends(): def suspends():
@ -45,28 +46,36 @@ class FanartThread(backgroundthread.KillableThread):
def _run_internal(self): def _run_internal(self):
LOG.info('Starting FanartThread') LOG.info('Starting FanartThread')
finished = False finished = False
try:
for typus in SUPPORTED_TYPES:
offset = 0
while True: while True:
with PlexDB() as plexdb: with PlexDB() as plexdb:
func = plexdb.every_plex_id if self.refresh else plexdb.missing_fanart # Keep DB connection open only for a short period of time!
for typus in SUPPORTED_TYPES: if self.refresh:
for plex_id in func(typus): batch = list(plexdb.every_plex_id(typus,
if self.isCanceled() or self.isSuspended(): offset,
break BATCH_SIZE))
process_fanart(plex_id, typus, self.refresh)
if self.isCanceled() or self.isSuspended():
break
else: else:
# Done processing! batch = list(plexdb.missing_fanart(typus,
finished = True offset,
break BATCH_SIZE))
# Need to have these outside our DB context to close the connection for plex_id in batch:
# Do the actual, time-consuming processing
if self.isCanceled(): if self.isCanceled():
return return
if self.isSuspended(): if self.isSuspended():
if self.isCanceled(): if self.isCanceled():
return return
app.APP.monitor.waitForAbort(1) app.APP.monitor.waitForAbort(1)
LOG.info('FanartThread finished') process_fanart(plex_id, typus, self.refresh)
if len(batch) < BATCH_SIZE:
break
offset += BATCH_SIZE
else:
finished = True
finally:
LOG.info('FanartThread finished: %s', finished)
self.callback(finished) self.callback(finished)

View file

@ -121,20 +121,28 @@ class PlexDBBase(object):
""" """
self.cursor.execute('DELETE FROM %s WHERE plex_id = ?' % plex_type, (plex_id, )) self.cursor.execute('DELETE FROM %s WHERE plex_id = ?' % plex_type, (plex_id, ))
def every_plex_id(self, plex_type): def every_plex_id(self, plex_type, offset, limit):
""" """
Returns an iterator for plex_type for every single plex_id Returns an iterator for plex_type for every single plex_id
Will start with records at DB position offset [int] and return limit
[int] number of items
""" """
return (x[0] for x in return (x[0] for x in
self.cursor.execute('SELECT plex_id from %s' % plex_type)) self.cursor.execute('SELECT plex_id FROM %s LIMIT %s OFFSET %s'
% (plex_type, limit, offset)))
def missing_fanart(self, plex_type): def missing_fanart(self, plex_type, offset, limit):
""" """
Returns an iterator for plex_type for all plex_id, where fanart_synced Returns an iterator for plex_type for all plex_id, where fanart_synced
has not yet been set to 1 has not yet been set to 1
Will start with records at DB position offset [int] and return limit
[int] number of items
""" """
return (x[0] for x in query = '''
self.cursor.execute('SELECT plex_id from %s WHERE fanart_synced = 0' % plex_type)) SELECT plex_id FROM %s WHERE fanart_synced = 0
LIMIT %s OFFSET %s
''' % (plex_type, limit, offset)
return (x[0] for x in self.cursor.execute(query))
def set_fanart_synced(self, plex_id, plex_type): def set_fanart_synced(self, plex_id, plex_type):
""" """