PERF: guard so we only create one optimized image per process
This protectd sidekiq and other cases where optimized images are created on demand so they do not dominate the machine.
This commit is contained in:
parent
a469c65631
commit
bfd052a317
|
@ -9,11 +9,22 @@ class OptimizedImage < ActiveRecord::Base
|
|||
# BUMP UP if optimized image algorithm changes
|
||||
VERSION = 1
|
||||
|
||||
def self.lock(upload_id, width, height)
|
||||
# note, the extra lock here ensures we only optimize one image per process
|
||||
# this can very easily lead to runaway CPU so slowing it down is beneficial
|
||||
@mutex ||= Mutex.new
|
||||
@mutex.synchronize do
|
||||
DistributedMutex.synchronize("optimized_image_#{upload_id}_#{width}_#{height}") do
|
||||
yield
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def self.create_for(upload, width, height, opts = {})
|
||||
return unless width > 0 && height > 0
|
||||
return if upload.try(:sha1).blank?
|
||||
|
||||
DistributedMutex.synchronize("optimized_image_#{upload.id}_#{width}_#{height}") do
|
||||
lock(upload.id, width, height) do
|
||||
# do we already have that thumbnail?
|
||||
thumbnail = find_by(upload_id: upload.id, width: width, height: height)
|
||||
|
||||
|
|
Loading…
Reference in New Issue