Merge pull request #268 from khoj-ai/fix/threading-issue-in-update-api

Add try-except-finally blocks around configure calls in /update
This commit is contained in:
sabaimran 2023-07-02 16:08:29 -07:00 committed by GitHub
commit feac71ce1e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 102 additions and 89 deletions

View file

@ -93,6 +93,7 @@ def configure_search(model: SearchModels, config: FullConfig, regenerate: bool,
logger.warning("🚨 No Content or Search type is configured.")
return
try:
# Initialize Org Notes Search
if (t == state.SearchType.Org or t == None) and config.content_type.org and config.search_type.asymmetric:
logger.info("🦄 Setting up search for orgmode notes")
@ -118,7 +119,11 @@ def configure_search(model: SearchModels, config: FullConfig, regenerate: bool,
)
# Initialize Markdown Search
if (t == state.SearchType.Markdown or t == None) and config.content_type.markdown and config.search_type.asymmetric:
if (
(t == state.SearchType.Markdown or t == None)
and config.content_type.markdown
and config.search_type.asymmetric
):
logger.info("💎 Setting up search for markdown notes")
# Extract Entries, Generate Markdown Embeddings
model.markdown_search = text_search.setup(
@ -184,6 +189,9 @@ def configure_search(model: SearchModels, config: FullConfig, regenerate: bool,
regenerate=regenerate,
filters=[DateFilter(), WordFilter(), FileFilter()],
)
except Exception as e:
logger.error("🚨 Failed to setup search")
raise e
# Invalidate Query Cache
state.query_cache = LRU()

View file

@ -357,7 +357,12 @@ def update(
):
try:
state.search_index_lock.acquire()
try:
state.model = configure_search(state.model, state.config, regenerate=force or False, t=t)
except Exception as e:
logger.error(e)
raise HTTPException(status_code=500, detail=str(e))
finally:
state.search_index_lock.release()
except ValueError as e:
logger.error(e)