add DistributedMutex around uploads/optimized_images creation

This commit is contained in:
Régis Hanol 2015-05-12 16:45:33 +02:00
parent 440331e36e
commit 6ae9bcab56
2 changed files with 96 additions and 92 deletions

View file

@ -6,6 +6,7 @@ class OptimizedImage < ActiveRecord::Base
def self.create_for(upload, width, height, opts={})
return unless width > 0 && height > 0
DistributedMutex.synchronize("optimized_image_#{upload.id}_#{width}_#{height}") do
# do we already have that thumbnail?
thumbnail = find_by(upload_id: upload.id, width: width, height: height)
@ -73,6 +74,7 @@ class OptimizedImage < ActiveRecord::Base
thumbnail
end
end
def destroy
OptimizedImage.transaction do

View file

@ -52,6 +52,7 @@ class Upload < ActiveRecord::Base
def self.create_for(user_id, file, filename, filesize, options = {})
sha1 = Digest::SHA1.file(file).hexdigest
DistributedMutex.synchronize("upload_#{sha1}") do
# do we already have that upload?
upload = find_by(sha1: sha1)
@ -90,6 +91,7 @@ class Upload < ActiveRecord::Base
# return the uploaded file
upload
end
end
def self.resize_image(filename, file, upload)
begin