Skip to content

Commit 1520e01

Browse files
committed
make mbox importer more robust
1 parent dadd92d commit 1520e01

1 file changed

Lines changed: 20 additions & 12 deletions

File tree

app/services/email_ingestor.rb

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -42,18 +42,26 @@ def ingest_raw(raw_message, mailing_list:, fallback_threading: false, trust_date
4242
)
4343
import_log = nil if import_log == ""
4444

45-
msg = Message.create!(
46-
topic: topic,
47-
sender: from[0],
48-
sender_person_id: from[0].person_id,
49-
reply_to: reply_to_msg,
50-
reply_to_message_id: reply_to_message_id,
51-
subject: subject,
52-
body: body,
53-
created_at: sent_at,
54-
message_id: message_id,
55-
import_log: import_log
56-
)
45+
begin
46+
msg = Message.create!(
47+
topic: topic,
48+
sender: from[0],
49+
sender_person_id: from[0].person_id,
50+
reply_to: reply_to_msg,
51+
reply_to_message_id: reply_to_message_id,
52+
subject: subject,
53+
body: body,
54+
created_at: sent_at,
55+
message_id: message_id,
56+
import_log: import_log
57+
)
58+
rescue ActiveRecord::RecordNotUnique, ActiveRecord::RecordInvalid => e
59+
raise unless message_id && (e.is_a?(ActiveRecord::RecordNotUnique) || e.message.include?("Message has already been taken"))
60+
existing_message = Message.find_by(message_id: message_id)
61+
raise unless existing_message
62+
associate_mailing_list(existing_message, mailing_list)
63+
return existing_message
64+
end
5765

5866
associate_mailing_list(msg, mailing_list)
5967

0 commit comments

Comments
 (0)