Skip to content

Commit ef17f16

Browse files
committed
nits and more efficient active index attempt check
1 parent df627ab commit ef17f16

File tree

1 file changed

+14
-16
lines changed
  • backend/onyx/background/celery/tasks/docprocessing

1 file changed

+14
-16
lines changed

backend/onyx/background/celery/tasks/docprocessing/tasks.py

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
from pydantic import BaseModel
1414
from redis import Redis
1515
from redis.lock import Lock as RedisLock
16+
from sqlalchemy import exists
1617
from sqlalchemy import select
1718
from sqlalchemy.orm import Session
1819

@@ -562,7 +563,7 @@ def check_indexing_completion(
562563
logger.info(f"Database coordination completed for attempt {index_attempt_id}")
563564

564565

565-
def _existing_index_attempts(
566+
def active_indexing_attempt(
566567
cc_pair_id: int,
567568
search_settings_id: int,
568569
db_session: Session,
@@ -574,9 +575,9 @@ def _existing_index_attempts(
574575
575576
Returns True if there's an active indexing attempt, False otherwise.
576577
"""
577-
existing_attempts = (
578-
db_session.execute(
579-
select(IndexAttempt).where(
578+
active_indexing_attempt = db_session.execute(
579+
select(
580+
exists().where(
580581
IndexAttempt.connector_credential_pair_id == cc_pair_id,
581582
IndexAttempt.search_settings_id == search_settings_id,
582583
IndexAttempt.status.in_(
@@ -587,18 +588,15 @@ def _existing_index_attempts(
587588
),
588589
)
589590
)
590-
.scalars()
591-
.all()
592-
)
591+
).scalar()
593592

594-
if existing_attempts:
593+
if active_indexing_attempt:
595594
task_logger.debug(
596-
f"_existing_index_attempts - Skipping due to active indexing attempt: "
597-
f"cc_pair={cc_pair_id} search_settings={search_settings_id} "
598-
f"active_attempts={[a.id for a in existing_attempts]}"
595+
f"active_indexing_attempt - Skipping due to active indexing attempt: "
596+
f"cc_pair={cc_pair_id} search_settings={search_settings_id}"
599597
)
600-
return True
601-
return False
598+
599+
return bool(active_indexing_attempt)
602600

603601

604602
def _kickoff_indexing_tasks(
@@ -621,7 +619,7 @@ def _kickoff_indexing_tasks(
621619
lock_beat.reacquire()
622620

623621
# Lightweight check prior to fetching cc pair
624-
if _existing_index_attempts(
622+
if active_indexing_attempt(
625623
cc_pair_id=cc_pair_id,
626624
search_settings_id=search_settings.id,
627625
db_session=db_session,
@@ -685,7 +683,7 @@ def _kickoff_indexing_tasks(
685683
tenant_id,
686684
)
687685

688-
if attempt_id:
686+
if attempt_id is not None:
689687
task_logger.info(
690688
f"Connector indexing queued: "
691689
f"index_attempt={attempt_id} "
@@ -694,7 +692,7 @@ def _kickoff_indexing_tasks(
694692
)
695693
tasks_created += 1
696694
else:
697-
task_logger.info(
695+
task_logger.error(
698696
f"Failed to create indexing task: "
699697
f"cc_pair={cc_pair.id} "
700698
f"search_settings={search_settings.id}"

0 commit comments

Comments
 (0)