diff --git a/oml/library.py b/oml/library.py index eee921e..d840872 100644 --- a/oml/library.py +++ b/oml/library.py @@ -377,6 +377,10 @@ def sync_db(): from sqlalchemy.orm import load_only import item.models first = True + missing_previews = [] + state.sync_db = True + + #FIXME: why is this loop needed with db.session(): sort_ids = {i.item_id for i in item.models.Sort.query.options(load_only('item_id'))} if sort_ids: @@ -387,9 +391,11 @@ def sync_db(): if first: first = False logger.debug('sync items') - i.update(commit=False) - if i.info.get('mediastate') == 'unavailable' and state.tasks: - state.tasks.queue('getpreview', i.id) + #why? + #i.update(commit=False) + i.update_sort(commit=False) + if i.info.get('mediastate') == 'unavailable': + missing_previews.append(i.id) commit = True #logger.debug('sync:%s', i) t0 = maybe_commit(t0) @@ -397,6 +403,7 @@ def sync_db(): break if commit: state.db.session.commit() + if not first: logger.debug('synced items') if not state.shutdown: @@ -408,6 +415,12 @@ def sync_db(): item.models.Sort.query.filter_by(item_id=None).delete() item.models.Find.query.filter_by(item_id=None).delete() + if missing_previews and state.tasks: + logger.debug('queueing download of %s missing previews', len(missing_previews)) + for id in missing_previews: + state.tasks.queue('getpreview', id) + state.sync_db = False + def cleanup_lists(): import item.models import user.models diff --git a/oml/nodes.py b/oml/nodes.py index 27d7658..85ee9b7 100644 --- a/oml/nodes.py +++ b/oml/nodes.py @@ -598,6 +598,8 @@ class Nodes(Thread): def _pull(self): if not state.sync_enabled or settings.preferences.get('downloadRate') == 0: return + if state.sync_db: + return if state.activity and state.activity.get('activity') == 'import': return self._pulling = True diff --git a/oml/state.py b/oml/state.py index cc1e804..4dbf2da 100644 --- a/oml/state.py +++ b/oml/state.py @@ -16,6 +16,7 @@ websockets = [] uisockets = [] peers = {} changelog_size = None +sync_db = False activity = {} removepeer = {}