From 337bd9a0f7616414253ffb26472bda59488e6e85 Mon Sep 17 00:00:00 2001 From: Sam Saffron Date: Tue, 26 May 2020 16:10:05 +1000 Subject: [PATCH] FIX: concurrency bug when creating topic thumbnails We were failing erratically when backfilling topic thumbnails. This ensures that racing threads/processes will not conflict. --- app/models/topic_thumbnail.rb | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/app/models/topic_thumbnail.rb b/app/models/topic_thumbnail.rb index 139ddde2321..ae8d17be8bc 100644 --- a/app/models/topic_thumbnail.rb +++ b/app/models/topic_thumbnail.rb @@ -21,7 +21,19 @@ class TopicThumbnail < ActiveRecord::Base optimized = OptimizedImage.create_for(original, target_width, target_height) end - create!(upload: original, max_width: max_width, max_height: max_height, optimized_image: optimized) + # may have been associated already, bulk insert will skip dupes + TopicThumbnail.insert_all([ + upload_id: original.id, + max_width: max_width, + max_height: max_height, + optimized_image_id: optimized.id + ]) + + TopicThumbnail.find_by( + upload: original, + max_width: max_width, + max_height: max_height + ) end def self.ensure_consistency!