DEV: Allow rebakes to generate optimized images at the same time

Previously only Sidekiq was allowed to generate more than one optimized image at the same time per machine. This adds an easy mechanism to allow the same in rake tasks and other tools.
This commit is contained in:
Gerhard Schlager
2024-01-13 23:34:20 +01:00
committed by Gerhard Schlager
parent bcb8b3fab9
commit 241bf48497
4 changed files with 37 additions and 44 deletions

View File

@@ -14,15 +14,24 @@ class OptimizedImage < ActiveRecord::Base
# this can very easily lead to runaway CPU so slowing it down is beneficial and it is hijacked
#
# we can not afford this blocking in Sidekiq cause it can lead to starvation
if Sidekiq.server?
DistributedMutex.synchronize("optimized_image_#{upload_id}_#{width}_#{height}") { yield }
else
if lock_per_machine?
DistributedMutex.synchronize("optimized_image_host_#{@hostname}") do
DistributedMutex.synchronize("optimized_image_#{upload_id}_#{width}_#{height}") { yield }
end
else
DistributedMutex.synchronize("optimized_image_#{upload_id}_#{width}_#{height}") { yield }
end
end
def self.lock_per_machine?
return @lock_per_machine if defined?(@lock_per_machine)
@lock_per_machine = !Sidekiq.server?
end
def self.lock_per_machine=(value)
@lock_per_machine = value
end
def self.create_for(upload, width, height, opts = {})
return if width <= 0 || height <= 0
return if upload.try(:sha1).blank?