tender duplication fix
This commit is contained in:
@@ -304,6 +304,14 @@ func (s *TEDScraper) findNoticeByContractNoticeID(ctx context.Context, contractN
|
||||
return existing, nil
|
||||
}
|
||||
|
||||
func isMongoDupKeyOnContractNoticeID(err error) bool {
|
||||
if err == nil {
|
||||
return false
|
||||
}
|
||||
msg := err.Error()
|
||||
return strings.Contains(msg, "E11000") && strings.Contains(msg, "contract_notice_id")
|
||||
}
|
||||
|
||||
func (s *TEDScraper) processXMLContent(ctx context.Context, content []byte, fileName string, result *FileProcessingResult) error {
|
||||
s.logger.Debug("Processing XML content", map[string]interface{}{
|
||||
"file_name": fileName,
|
||||
@@ -382,6 +390,29 @@ func (s *TEDScraper) processXMLContent(ctx context.Context, content []byte, file
|
||||
|
||||
err = s.noticeRepo.Import(ctx, t)
|
||||
if err != nil {
|
||||
if isMongoDupKeyOnContractNoticeID(err) {
|
||||
existingAfterRace, findErr := s.findNoticeByContractNoticeID(ctx, t.ContractNoticeID)
|
||||
if findErr != nil {
|
||||
return fmt.Errorf("import duplicate key, re-fetch notice: %w", findErr)
|
||||
}
|
||||
if existingAfterRace == nil {
|
||||
return fmt.Errorf("failed to import notice: %w", err)
|
||||
}
|
||||
MergeNoticeUpdate(existingAfterRace, t)
|
||||
if upErr := s.noticeRepo.Update(ctx, existingAfterRace); upErr != nil {
|
||||
s.logger.Error("Failed to update notice after duplicate-key race", map[string]interface{}{
|
||||
"contract_notice_id": t.ContractNoticeID,
|
||||
"error": upErr.Error(),
|
||||
})
|
||||
return fmt.Errorf("failed to update notice after duplicate key: %w", upErr)
|
||||
}
|
||||
s.logger.Info("Notice merged after duplicate-key race (parallel ingest)", map[string]interface{}{
|
||||
"contract_notice_id": t.ContractNoticeID,
|
||||
"notice_doc_id": existingAfterRace.ID.Hex(),
|
||||
})
|
||||
result.SuccessCount++
|
||||
return nil
|
||||
}
|
||||
s.logger.Error("Failed to import notice", map[string]interface{}{
|
||||
"contract_notice_id": t.ContractNoticeID,
|
||||
"error": err.Error(),
|
||||
|
||||
Reference in New Issue
Block a user