From d4ffeca90a78deeaac410de900bc70b065f9f91e Mon Sep 17 00:00:00 2001 From: sabaimran Date: Sat, 5 Oct 2024 09:13:16 -0700 Subject: [PATCH] Fix notion indexing with manually set token --- .../processor/content/notion/notion_to_entries.py | 3 ++- src/khoj/routers/api_content.py | 14 ++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/src/khoj/processor/content/notion/notion_to_entries.py b/src/khoj/processor/content/notion/notion_to_entries.py index c53d4020..fc6e296f 100644 --- a/src/khoj/processor/content/notion/notion_to_entries.py +++ b/src/khoj/processor/content/notion/notion_to_entries.py @@ -52,7 +52,8 @@ class NotionToEntries(TextToEntries): token=config.token, ) self.session = requests.Session() - self.session.headers.update({"Authorization": f"Bearer {config.token}", "Notion-Version": "2022-02-22"}) + if config.token: + self.session.headers.update({"Authorization": f"Bearer {config.token}", "Notion-Version": "2022-02-22"}) self.unsupported_block_types = [ NotionBlockType.BOOKMARK.value, NotionBlockType.DIVIDER.value, diff --git a/src/khoj/routers/api_content.py b/src/khoj/routers/api_content.py index ad9e3b56..72b304ef 100644 --- a/src/khoj/routers/api_content.py +++ b/src/khoj/routers/api_content.py @@ -2,11 +2,13 @@ import asyncio import json import logging import math +from concurrent.futures import ThreadPoolExecutor from typing import Dict, List, Optional, Union from asgiref.sync import sync_to_async from fastapi import ( APIRouter, + BackgroundTasks, Depends, Header, HTTPException, @@ -58,6 +60,8 @@ logger = logging.getLogger(__name__) api_content = APIRouter() +executor = ThreadPoolExecutor() + class File(BaseModel): path: str @@ -77,6 +81,11 @@ class IndexerInput(BaseModel): docx: Optional[dict[str, bytes]] = None +async def run_in_executor(func, *args): + loop = asyncio.get_event_loop() + return await loop.run_in_executor(executor, func, *args) + + @api_content.put("") @requires(["authenticated"]) async def put_content( @@ -209,6 +218,7 @@ async def set_content_github( @requires(["authenticated"]) async def set_content_notion( request: Request, + background_tasks: BackgroundTasks, updated_config: Union[NotionContentConfig, None], client: Optional[str] = None, ): @@ -225,6 +235,10 @@ async def set_content_notion( logger.error(e, exc_info=True) raise HTTPException(status_code=500, detail="Failed to set Notion config") + if updated_config.token: + # Trigger an async job to configure_content. Let it run without blocking the response. + background_tasks.add_task(run_in_executor, configure_content, {}, False, SearchType.Notion, user) + update_telemetry_state( request=request, telemetry_type="api",