mirror of
https://github.com/MODSetter/SurfSense.git
synced 2026-04-25 00:36:31 +02:00
feat: add folder management features including creation, deletion, and organization of documents within folders
This commit is contained in:
parent
95bb522220
commit
685ad0c02d
41 changed files with 7475 additions and 4330 deletions
|
|
@ -133,6 +133,51 @@ async def _delete_document_background(document_id: int) -> None:
|
|||
await session.commit()
|
||||
|
||||
|
||||
@celery_app.task(
|
||||
name="delete_folder_documents_background",
|
||||
bind=True,
|
||||
autoretry_for=(Exception,),
|
||||
retry_backoff=True,
|
||||
retry_backoff_max=300,
|
||||
max_retries=5,
|
||||
)
|
||||
def delete_folder_documents_task(self, document_ids: list[int]):
|
||||
"""Celery task to batch-delete documents orphaned by folder deletion."""
|
||||
loop = asyncio.new_event_loop()
|
||||
asyncio.set_event_loop(loop)
|
||||
try:
|
||||
loop.run_until_complete(_delete_folder_documents(document_ids))
|
||||
finally:
|
||||
loop.close()
|
||||
|
||||
|
||||
async def _delete_folder_documents(document_ids: list[int]) -> None:
|
||||
"""Delete chunks in batches, then document rows for each orphaned document."""
|
||||
from sqlalchemy import delete as sa_delete, select
|
||||
|
||||
from app.db import Chunk, Document
|
||||
|
||||
async with get_celery_session_maker()() as session:
|
||||
batch_size = 500
|
||||
for doc_id in document_ids:
|
||||
while True:
|
||||
chunk_ids_result = await session.execute(
|
||||
select(Chunk.id)
|
||||
.where(Chunk.document_id == doc_id)
|
||||
.limit(batch_size)
|
||||
)
|
||||
chunk_ids = chunk_ids_result.scalars().all()
|
||||
if not chunk_ids:
|
||||
break
|
||||
await session.execute(sa_delete(Chunk).where(Chunk.id.in_(chunk_ids)))
|
||||
await session.commit()
|
||||
|
||||
doc = await session.get(Document, doc_id)
|
||||
if doc:
|
||||
await session.delete(doc)
|
||||
await session.commit()
|
||||
|
||||
|
||||
@celery_app.task(
|
||||
name="delete_search_space_background",
|
||||
bind=True,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue