Skip to content
Snippets Groups Projects
Commit 6fb86952 authored by Andréas Livet's avatar Andréas Livet
Browse files

document : try to avoid possible race conditions with minio and db on document creation

parent 1f69ff50
No related branches found
No related tags found
No related merge requests found
......@@ -188,8 +188,17 @@ def handle_pdf_upload(session: Any, collection_id: uuid.UUID, file: UploadFile)
n_pages=len(doc.pages),
title=doc_title,
)
created_doc = cast(Document, crud.create_generic_item(session=session, model=Document, item=new_doc))
# Important : don't use create_generic_item because we want to commit
# data only when file has been written to s3 to avoid potential race condition
created_doc = Document.model_validate(new_doc)
session.add(created_doc)
# Use flush to insert the document in db without closing the transaction
session.flush()
session.refresh(created_doc)
created_doc.path.write_bytes(file.file.read())
assert created_doc.path.exists()
session.commit()
session.refresh(created_doc)
return created_doc
except Exception as exc:
session.rollback()
......
......@@ -265,6 +265,7 @@ class Document(DocumentCreate, table=True):
def qdrant_collection_name(self) -> str:
return settings.QDRANT.collection_names[self.collection.type]
@computed_field # type: ignore[prop-decorator]
@property
def path(self) -> Any:
return client.CloudPath(f"s3://{COLLECTIONS_BUCKET_NAME}/{self.collection.title}/{self.id}/raw_document.pdf")
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment