@@ -387,30 +387,37 @@ def _prepare_index_attempt(
387
387
# after the next commit:
388
388
# https://docs.sqlalchemy.org/en/20/orm/session_transaction.html#setting-isolation-for-individual-transactions
389
389
db_session .connection (execution_options = {"isolation_level" : "SERIALIZABLE" }) # type: ignore
390
- if tenant_id is not None :
391
- # Explicitly set the search path for the given tenant
392
- db_session .execute (text (f'SET search_path TO "{ tenant_id } "' ))
393
- # Verify the search path was set correctly
394
- result = db_session .execute (text ("SHOW search_path" ))
395
- current_search_path = result .scalar ()
396
- logger .info (f"Current search path set to: { current_search_path } " )
397
-
398
- attempt = get_index_attempt (
399
- db_session = db_session ,
400
- index_attempt_id = index_attempt_id ,
401
- )
390
+ try :
391
+ if tenant_id is not None :
392
+ # Explicitly set the search path for the given tenant
393
+ db_session .execute (text (f'SET search_path TO "{ tenant_id } "' ))
394
+ # Verify the search path was set correctly
395
+ result = db_session .execute (text ("SHOW search_path" ))
396
+ current_search_path = result .scalar ()
397
+ logger .info (f"Current search path set to: { current_search_path } " )
398
+
399
+ attempt = get_index_attempt (
400
+ db_session = db_session ,
401
+ index_attempt_id = index_attempt_id ,
402
+ )
402
403
403
- if attempt is None :
404
- raise RuntimeError (f"Unable to find IndexAttempt for ID '{ index_attempt_id } '" )
404
+ if attempt is None :
405
+ raise RuntimeError (
406
+ f"Unable to find IndexAttempt for ID '{ index_attempt_id } '"
407
+ )
405
408
406
- if attempt .status != IndexingStatus .NOT_STARTED :
407
- raise RuntimeError (
408
- f"Indexing attempt with ID '{ index_attempt_id } ' is not in NOT_STARTED status. "
409
- f"Current status is '{ attempt .status } '."
410
- )
409
+ if attempt .status != IndexingStatus .NOT_STARTED :
410
+ raise RuntimeError (
411
+ f"Indexing attempt with ID '{ index_attempt_id } ' is not in NOT_STARTED status. "
412
+ f"Current status is '{ attempt .status } '."
413
+ )
414
+
415
+ mark_attempt_in_progress (attempt , db_session )
411
416
412
- # only commit once, to make sure this all happens in a single transaction
413
- mark_attempt_in_progress (attempt , db_session )
417
+ # only commit once, to make sure this all happens in a single transaction
418
+ db_session .commit ()
419
+ except Exception :
420
+ db_session .rollback ()
414
421
415
422
return attempt
416
423
0 commit comments