Skip to content

Commit 16db068

Browse files
authored
Run populate_vector_db in thread (#143)
1 parent 47c38b0 commit 16db068

2 files changed

Lines changed: 13 additions & 6 deletions

File tree

app/gunicorn.conf.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,12 @@
1010
"""
1111

1212
import os
13+
import threading
1314

1415
from src.app_config import config as app_config
1516
from src.ingestion import rag_utils
1617

17-
bind = app_config.host + ':' + str(app_config.port)
18+
bind = app_config.host + ":" + str(app_config.port)
1819
# Calculates the number of usable cores and doubles it. Recommended number of workers per core is two.
1920
# https://docs.gunicorn.org/en/latest/design.html#how-many-workers
2021
# We use 'os.sched_getaffinity(pid)' not 'os.cpu_count()' because it returns only allowable CPUs.
@@ -28,4 +29,5 @@
2829
# https://stackoverflow.com/questions/24101724/gunicorn-with-multiple-workers-is-there-an-easy-way-to-execute-certain-code-onl
2930
def when_ready(server: object) -> None:
3031
print("when_ready()", server)
31-
rag_utils.populate_vector_db()
32+
populate_rag_db_thread = threading.Thread(target=rag_utils.populate_vector_db)
33+
populate_rag_db_thread.start()

app/src/ingestion/rag_utils.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,14 @@ def populate_vector_db() -> None:
3939
collection_name = doc_store._collection_name
4040

4141
# Clear existing collection if any
42-
if doc_store.count_documents() > 0:
42+
if (doc_count := doc_store.count_documents()) > 0:
4343
try:
4444
# Don't delete collection since it's referenced by existing pipelines upon their startup
45-
logger.info("Clearing out existing vector DB collection=%r", collection_name)
45+
logger.info(
46+
"Clearing out existing vector DB collection=%r with %d docs",
47+
collection_name,
48+
doc_count,
49+
)
4650
# recreate_index=True results in a new id for the collection, which breaks existing pipelines
4751
doc_store.delete_all_documents(recreate_index=False)
4852
except DocumentStoreError as e:
@@ -65,7 +69,6 @@ def populate_vector_db() -> None:
6569
pipeline = _create_ingest_pipeline(doc_store)
6670
pipeline.run({"converter": {"sources": files_to_ingest}})
6771
logger.info("Ingested documents doc_count=%d", doc_store.count_documents())
68-
6972
logger.info("ChromaDB collections: %s", chroma_client.list_collections())
7073

7174

@@ -78,16 +81,18 @@ def download_s3_folder_to_local(s3_folder: str = "files_to_ingest_into_vector_db
7881
except PermissionError as e:
7982
logger.error("Error creating directories for %s: %s", s3_folder, e)
8083
local_folder = f"/tmp/{s3_folder}" # nosec B108
81-
logger.info("Downloading s3://%s/%s to local folder %s", bucket, s3_folder, local_folder)
8284

8385
if config.environment == "local":
8486
assert os.path.exists(
8587
local_folder
8688
), f"Local folder {local_folder} should exist with manually downloaded files from S3"
8789
return local_folder
8890

91+
logger.info("Downloading s3://%s/%s to local folder %s", bucket, s3_folder, local_folder)
8992
s3 = file_util.get_s3_client()
9093
paginator = s3.get_paginator("list_objects_v2")
94+
if not s3_folder.endswith("/"):
95+
s3_folder += "/"
9196
try:
9297
for result in paginator.paginate(Bucket=bucket, Prefix=s3_folder):
9398
for obj in result.get("Contents", []):

0 commit comments

Comments
 (0)