feat(document-sync): update xaiSyncStatus handling and logging for document synchronization

This commit is contained in:
bsiggel
2026-03-08 19:21:17 +00:00
parent 4ed752b19e
commit 93d4d89531

View File

@@ -48,15 +48,13 @@ class DocumentSync(BaseSyncUtils):
self._log(f"Redis lock bereits aktiv für {entity_type} {entity_id}", level='warn') self._log(f"Redis lock bereits aktiv für {entity_type} {entity_id}", level='warn')
return False return False
# STEP 2: Update syncStatus (für UI visibility) - nur bei Document Entity # STEP 2: Update xaiSyncStatus auf pending_sync
# CDokumente hat dieses Feld nicht - überspringen try:
if entity_type == 'Document': await self.espocrm.update_entity(entity_type, entity_id, {
try: 'xaiSyncStatus': 'pending_sync'
await self.espocrm.update_entity(entity_type, entity_id, { })
'xaiSyncStatus': 'syncing' except Exception as e:
}) self._log(f"Konnte xaiSyncStatus nicht setzen: {e}", level='debug')
except Exception as e:
self._log(f"Konnte xaiSyncStatus nicht setzen: {e}", level='debug')
self._log(f"Sync-Lock für {entity_type} {entity_id} erworben") self._log(f"Sync-Lock für {entity_type} {entity_id} erworben")
return True return True
@@ -89,17 +87,16 @@ class DocumentSync(BaseSyncUtils):
try: try:
update_data = {} update_data = {}
# Status-Felder nur bei Document Entity (CDokumente hat diese Felder nicht) # xaiSyncStatus setzen: clean bei Erfolg, failed bei Fehler
if entity_type == 'Document': try:
try: update_data['xaiSyncStatus'] = 'clean' if success else 'failed'
update_data['xaiSyncStatus'] = 'synced' if success else 'failed'
if error_message:
if error_message: update_data['xaiSyncError'] = error_message[:2000]
update_data['xaiSyncError'] = error_message[:2000] else:
else: update_data['xaiSyncError'] = None
update_data['xaiSyncError'] = None except:
except: pass # Felder existieren evtl. nicht
pass # Felder existieren evtl. nicht
# Merge extra fields (z.B. xaiFileId, xaiCollections) # Merge extra fields (z.B. xaiFileId, xaiCollections)
if extra_fields: if extra_fields:
@@ -148,6 +145,7 @@ class DocumentSync(BaseSyncUtils):
# xAI-relevante Felder # xAI-relevante Felder
xai_file_id = document.get('xaiFileId') xai_file_id = document.get('xaiFileId')
xai_collections = document.get('xaiCollections') or [] xai_collections = document.get('xaiCollections') or []
xai_sync_status = document.get('xaiSyncStatus')
# Datei-Status und Hash-Felder # Datei-Status und Hash-Felder
datei_status = document.get('dateiStatus') or document.get('fileStatus') datei_status = document.get('dateiStatus') or document.get('fileStatus')
@@ -158,7 +156,8 @@ class DocumentSync(BaseSyncUtils):
self._log(f"📋 Document Analysis: {doc_name} (ID: {doc_id})") self._log(f"📋 Document Analysis: {doc_name} (ID: {doc_id})")
self._log(f" xaiFileId: {xai_file_id or 'N/A'}") self._log(f" xaiFileId: {xai_file_id or 'N/A'}")
self._log(f" xaiCollections: {xai_collections}") self._log(f" xaiCollections: {xai_collections}")
self._log(f" Datei-Status: {datei_status or 'N/A'}") self._log(f" xaiSyncStatus: {xai_sync_status or 'N/A'}")
self._log(f" fileStatus: {datei_status or 'N/A'}")
self._log(f" MD5: {file_md5[:16] if file_md5 else 'N/A'}...") self._log(f" MD5: {file_md5[:16] if file_md5 else 'N/A'}...")
self._log(f" SHA: {file_sha[:16] if file_sha else 'N/A'}...") self._log(f" SHA: {file_sha[:16] if file_sha else 'N/A'}...")
self._log(f" xaiSyncedHash: {xai_synced_hash[:16] if xai_synced_hash else 'N/A'}...") self._log(f" xaiSyncedHash: {xai_synced_hash[:16] if xai_synced_hash else 'N/A'}...")
@@ -169,22 +168,34 @@ class DocumentSync(BaseSyncUtils):
entity_type=entity_type entity_type=entity_type
) )
# Prüfe xaiSyncStatus="no_sync" → kein Sync für dieses Dokument
if xai_sync_status == 'no_sync':
self._log("⏭️ Kein xAI-Sync nötig: xaiSyncStatus='no_sync'")
return (False, [], "xaiSyncStatus ist 'no_sync'")
if not target_collections: if not target_collections:
self._log("⏭️ Kein xAI-Sync nötig: Keine Related Entities mit xAI Collections") self._log("⏭️ Kein xAI-Sync nötig: Keine Related Entities mit xAI Collections")
return (False, [], "Keine verknüpften Entities mit xAI Collections") return (False, [], "Keine verknüpften Entities mit xAI Collections")
# ═══════════════════════════════════════════════════════════════ # ═══════════════════════════════════════════════════════════════
# PRIORITY CHECK: Datei-Status "Neu" oder "Geändert" # PRIORITY CHECK 1: xaiSyncStatus="unclean" → Dokument wurde geändert
# ═══════════════════════════════════════════════════════════════ # ═══════════════════════════════════════════════════════════════
if datei_status in ['Neu', 'Geändert', 'neu', 'geändert', 'New', 'Changed']: if xai_sync_status == 'unclean':
self._log(f"🆕 Datei-Status: '{datei_status}' → xAI-Sync ERFORDERLICH") self._log(f"🆕 xaiSyncStatus='unclean' → xAI-Sync ERFORDERLICH")
return (True, target_collections, "xaiSyncStatus='unclean'")
# ═══════════════════════════════════════════════════════════════
# PRIORITY CHECK 2: fileStatus "new" oder "changed"
# ═══════════════════════════════════════════════════════════════
if datei_status in ['new', 'changed', 'neu', 'geändert', 'New', 'Changed', 'Neu', 'Geändert']:
self._log(f"🆕 fileStatus: '{datei_status}' → xAI-Sync ERFORDERLICH")
if target_collections: if target_collections:
return (True, target_collections, f"Datei-Status: {datei_status}") return (True, target_collections, f"fileStatus: {datei_status}")
else: else:
# Datei ist neu/geändert aber keine Collections gefunden # Datei ist neu/geändert aber keine Collections gefunden
self._log(f"⚠️ Datei-Status '{datei_status}' aber keine Collections gefunden - überspringe Sync") self._log(f"⚠️ fileStatus '{datei_status}' aber keine Collections gefunden - überspringe Sync")
return (False, [], f"Datei-Status: {datei_status}, aber keine Collections") return (False, [], f"fileStatus: {datei_status}, aber keine Collections")
# ═══════════════════════════════════════════════════════════════ # ═══════════════════════════════════════════════════════════════
# FALL 1: Document ist bereits in xAI UND Collections sind gesetzt # FALL 1: Document ist bereits in xAI UND Collections sind gesetzt
@@ -513,15 +524,17 @@ class DocumentSync(BaseSyncUtils):
if collection_ids is not None: if collection_ids is not None:
update_data['xaiCollections'] = collection_ids update_data['xaiCollections'] = collection_ids
# Status zurücksetzen wenn xAI-Sync erfolgt ist ODER explizit angefordert # fileStatus auf "unchanged" setzen wenn Dokument verarbeitet/clean ist
if xai_file_id or reset_file_status: if reset_file_status:
# CDokumente verwendet fileStatus, Document verwendet dateiStatus
if entity_type == 'CDokumente': if entity_type == 'CDokumente':
# Bei xAI-Sync: "synced", bei nur Preview: "processed" update_data['fileStatus'] = 'unchanged'
update_data['fileStatus'] = 'synced' if xai_file_id else 'processed'
else: else:
# Bei xAI-Sync: "Gesynct", bei nur Preview: "Verarbeitet" # Document Entity hat kein fileStatus, nur dateiStatus
update_data['dateiStatus'] = 'Gesynct' if xai_file_id else 'Verarbeitet' update_data['dateiStatus'] = 'unchanged'
# xaiSyncStatus auf "clean" setzen wenn xAI-Sync erfolgreich war
if xai_file_id:
update_data['xaiSyncStatus'] = 'clean'
# Hash speichern für zukünftige Change Detection # Hash speichern für zukünftige Change Detection
if file_hash: if file_hash: