56
56
from onyx .db .enums import SyncStatus
57
57
from onyx .db .enums import SyncType
58
58
from onyx .db .models import ConnectorCredentialPair
59
+ from onyx .db .permission_sync_attempt import create_doc_permission_sync_attempt
60
+ from onyx .db .permission_sync_attempt import (
61
+ mark_doc_permission_sync_attempt_completed_with_errors ,
62
+ )
63
+ from onyx .db .permission_sync_attempt import mark_doc_permission_sync_attempt_failed
64
+ from onyx .db .permission_sync_attempt import (
65
+ mark_doc_permission_sync_attempt_in_progress ,
66
+ )
67
+ from onyx .db .permission_sync_attempt import (
68
+ mark_doc_permission_sync_attempt_succeeded ,
69
+ )
70
+ from onyx .db .permission_sync_attempt import update_doc_permission_sync_progress
59
71
from onyx .db .sync_record import insert_sync_record
60
72
from onyx .db .sync_record import update_sync_record_status
61
73
from onyx .db .users import batch_add_ext_perm_user_if_not_exists
@@ -379,6 +391,15 @@ def connector_permission_sync_generator_task(
379
391
doc_permission_sync_ctx_dict ["request_id" ] = self .request .id
380
392
doc_permission_sync_ctx .set (doc_permission_sync_ctx_dict )
381
393
394
+ with get_session_with_current_tenant () as db_session :
395
+ attempt_id = create_doc_permission_sync_attempt (
396
+ connector_credential_pair_id = cc_pair_id ,
397
+ db_session = db_session ,
398
+ )
399
+ task_logger .info (
400
+ f"Created doc permission sync attempt: { attempt_id } for cc_pair={ cc_pair_id } "
401
+ )
402
+
382
403
redis_connector = RedisConnector (tenant_id , cc_pair_id )
383
404
384
405
r = get_redis_client ()
@@ -483,6 +504,8 @@ def connector_permission_sync_generator_task(
483
504
484
505
logger .info (f"Syncing docs for { source_type } with cc_pair={ cc_pair_id } " )
485
506
507
+ mark_doc_permission_sync_attempt_in_progress (attempt_id , db_session )
508
+
486
509
payload = redis_connector .permissions .payload
487
510
if not payload :
488
511
raise ValueError (f"No fence payload found: cc_pair={ cc_pair_id } " )
@@ -533,22 +556,53 @@ def fetch_all_existing_docs_ids_fn() -> list[str]:
533
556
)
534
557
535
558
tasks_generated = 0
559
+ docs_with_errors = 0
536
560
for doc_external_access in document_external_accesses :
537
- redis_connector .permissions .update_db (
538
- lock = lock ,
539
- new_permissions = [doc_external_access ],
540
- source_string = source_type ,
541
- connector_id = cc_pair .connector .id ,
542
- credential_id = cc_pair .credential .id ,
543
- task_logger = task_logger ,
544
- )
545
- tasks_generated += 1
561
+ try :
562
+ redis_connector .permissions .update_db (
563
+ lock = lock ,
564
+ new_permissions = [doc_external_access ],
565
+ source_string = source_type ,
566
+ connector_id = cc_pair .connector .id ,
567
+ credential_id = cc_pair .credential .id ,
568
+ task_logger = task_logger ,
569
+ )
570
+ tasks_generated += 1
571
+ except Exception as e :
572
+ docs_with_errors += 1
573
+ task_logger .exception (
574
+ f"Error updating permissions for doc { doc_external_access .doc_id } : { e } "
575
+ )
576
+ # Continue processing other documents
546
577
547
578
task_logger .info (
548
579
f"RedisConnector.permissions.generate_tasks finished. "
549
- f"cc_pair={ cc_pair_id } tasks_generated={ tasks_generated } "
580
+ f"cc_pair={ cc_pair_id } tasks_generated={ tasks_generated } docs_with_errors= { docs_with_errors } "
550
581
)
551
582
583
+ update_doc_permission_sync_progress (
584
+ db_session = db_session ,
585
+ attempt_id = attempt_id ,
586
+ total_docs_synced = tasks_generated ,
587
+ docs_with_permission_errors = docs_with_errors ,
588
+ )
589
+ task_logger .info (
590
+ f"Updated progress for attempt { attempt_id } : { tasks_generated } docs, { docs_with_errors } errors"
591
+ )
592
+
593
+ if docs_with_errors > 0 :
594
+ mark_doc_permission_sync_attempt_completed_with_errors (
595
+ attempt_id , db_session
596
+ )
597
+ task_logger .info (
598
+ f"Marked doc permission sync attempt { attempt_id } as completed with errors"
599
+ )
600
+ else :
601
+ mark_doc_permission_sync_attempt_succeeded (attempt_id , db_session )
602
+ task_logger .info (
603
+ f"Marked doc permission sync attempt { attempt_id } as succeeded"
604
+ )
605
+
552
606
redis_connector .permissions .generator_complete = tasks_generated
553
607
554
608
except Exception as e :
@@ -561,6 +615,12 @@ def fetch_all_existing_docs_ids_fn() -> list[str]:
561
615
f"Permission sync exceptioned: cc_pair={ cc_pair_id } payload_id={ payload_id } "
562
616
)
563
617
618
+ with get_session_with_current_tenant () as db_session :
619
+ mark_doc_permission_sync_attempt_failed (attempt_id , db_session )
620
+ task_logger .info (
621
+ f"Marked doc permission sync attempt { attempt_id } as failed"
622
+ )
623
+
564
624
redis_connector .permissions .generator_clear ()
565
625
redis_connector .permissions .taskset_clear ()
566
626
redis_connector .permissions .set_fence (None )
0 commit comments