thumbnailworker: reduce memory issues

This commit is contained in:
Kyle Drake 2020-05-27 04:02:45 +00:00
parent 7b0df670fd
commit 4533214d71

View file

@ -2,18 +2,31 @@ require 'rmagick'
class ThumbnailWorker class ThumbnailWorker
THUMBNAILS_PATH = Site::THUMBNAILS_ROOT THUMBNAILS_PATH = Site::THUMBNAILS_ROOT
MAXIMUM_IMAGE_SIZE = 2_000_000 # 2MB
include Sidekiq::Worker include Sidekiq::Worker
sidekiq_options queue: :thumbnails, retry: 3, backtrace: true sidekiq_options queue: :thumbnails, retry: 3, backtrace: true
def perform(username, path) def perform(username, path)
site = Site[username: username] site = Site[username: username]
return if site.nil?
site_file_path = site.files_path(path)
return unless File.exist?(site_file_path)
# Large images jam up ImageMagick and eat a ton of memory, so we skip for now.
return if File.size(site_file_path) > MAXIMUM_IMAGE_SIZE
img_list = Magick::ImageList.new img_list = Magick::ImageList.new
begin begin
img_list.from_blob File.read(site.files_path(path)) img_list.from_blob File.read(site_file_path)
rescue Errno::ENOENT => e # Not found, skip rescue Errno::ENOENT => e # Not found, skip
return return
rescue Magick::ImageMagickError => e
GC.start full_mark: true, immediate_sweep: true
puts "thumbnail fail: #{site_file_path} #{e.inspect}"
return
end end
img = img_list.first img = img_list.first
@ -33,7 +46,11 @@ class ThumbnailWorker
resimg.write(full_thumbnail_path) { resimg.write(full_thumbnail_path) {
self.quality = 75 self.quality = 75
} }
resimg.destroy!
#$image_optim.optimize_image! full_thumbnail_path #$image_optim.optimize_image! full_thumbnail_path
end end
img.destroy!
GC.start full_mark: true, immediate_sweep: true
end end
end end