Close DB connections while caching images

- Partially fixes #580
This commit is contained in:
croneter 2018-12-30 21:30:08 +01:00
parent 2dff87dc4b
commit e4cb07df68
2 changed files with 51 additions and 32 deletions

View file

@ -16,6 +16,7 @@ requests.packages.urllib3.disable_warnings()
# Potentially issues with limited number of threads Hence let Kodi wait till # Potentially issues with limited number of threads Hence let Kodi wait till
# download is successful # download is successful
TIMEOUT = (35.1, 35.1) TIMEOUT = (35.1, 35.1)
BATCH_SIZE = 500
IMAGE_CACHING_SUSPENDS = [] IMAGE_CACHING_SUSPENDS = []
@ -32,38 +33,56 @@ class ImageCachingThread(backgroundthread.KillableThread):
def isSuspended(self): def isSuspended(self):
return any(IMAGE_CACHING_SUSPENDS) return any(IMAGE_CACHING_SUSPENDS)
@staticmethod def _url_generator(self, kind, kodi_type):
def _art_url_generator(): """
for kind in (KodiVideoDB, KodiMusicDB): Main goal is to close DB connection between calls
with kind() as kodidb: """
for kodi_type in ('poster', 'fanart'): offset = 0
for url in kodidb.artwork_generator(kodi_type): i = 0
yield url while True:
batch = []
def missing_art_cache_generator(self): with kind(texture_db=True) as kodidb:
with KodiTextureDB() as kodidb: texture_db = KodiTextureDB(cursor=kodidb.artcursor)
for url in self._art_url_generator(): for i, url in enumerate(kodidb.artwork_generator(kodi_type,
if kodidb.url_not_yet_cached(url): BATCH_SIZE,
yield url offset)):
if texture_db.url_not_yet_cached(url):
batch.append(url)
if len(batch) == BATCH_SIZE:
break
offset += i
for url in batch:
yield url
if i + 1 < BATCH_SIZE:
break
def run(self): def run(self):
LOG.info("---===### Starting ImageCachingThread ###===---") LOG.info("---===### Starting ImageCachingThread ###===---")
# Cache already synced artwork first try:
for url in self.missing_art_cache_generator(): self._run()
if self.isCanceled(): except:
return utils.ERROR()
while self.isSuspended(): finally:
# Set in service.py LOG.info("---===### Stopped ImageCachingThread ###===---")
if self.isCanceled():
# Abort was requested while waiting. We should exit def _run(self):
LOG.info("---===### Stopped ImageCachingThread ###===---") kinds = [KodiVideoDB]
return if app.SYNC.enable_music:
app.APP.monitor.waitForAbort(1) kinds.append(KodiMusicDB)
cache_url(url) for kind in kinds:
else: for kodi_type in ('poster', 'fanart'):
# Toggles Image caching completed to Yes for url in self._url_generator(kind, kodi_type):
utils.settings('plex_status_image_caching', value=utils.lang(107)) if self.isCanceled():
LOG.info("---===### Stopped ImageCachingThread ###===---") return
while self.isSuspended():
# Set in service.py
if self.isCanceled():
# Abort was requested while waiting. We should exit
return
app.APP.monitor.waitForAbort(1)
cache_url(url)
# Toggles Image caching completed to Yes
utils.settings('plex_status_image_caching', value=utils.lang(107))
def cache_url(url): def cache_url(url):

View file

@ -41,10 +41,10 @@ class KodiDBBase(object):
self.cursor.execute('SELECT url FROM art WHERE media_id = ? AND media_type = ?', self.cursor.execute('SELECT url FROM art WHERE media_id = ? AND media_type = ?',
(kodi_id, kodi_type))) (kodi_id, kodi_type)))
def artwork_generator(self, kodi_type): def artwork_generator(self, kodi_type, limit, offset):
query = 'SELECT url FROM art WHERE type == ? LIMIT ? OFFSET ?'
return (x[0] for x in return (x[0] for x in
self.cursor.execute('SELECT url FROM art WHERE type == ?', self.cursor.execute(query, (kodi_type, limit, offset)))
(kodi_type, )))
def add_artwork(self, artworks, kodi_id, kodi_type): def add_artwork(self, artworks, kodi_id, kodi_type):
""" """