FIX: concurrency bug when creating topic thumbnails

We were failing erratically when backfilling topic thumbnails.

This ensures that racing threads/processes will not conflict.
This commit is contained in:
Sam Saffron 2020-05-26 16:10:05 +10:00
parent 2211581a85
commit 337bd9a0f7
No known key found for this signature in database
GPG Key ID: B9606168D2FFD9F5

View File

@ -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!