Add time out when optimizing images.

This commit is contained in:
Guo Xiang Tan 2017-04-20 15:18:42 +08:00
parent d7b446c174
commit 2af1b9e93c
2 changed files with 8 additions and 2 deletions

View File

@ -2,10 +2,11 @@ skip_missing_workers: true
allow_lossy: false
# PNG
advpng: false
optipng:
optipng:
level: 2
pngcrush: false
pngout: false
pngquant: false
# JPG
jpegrecompress: false
timeout: 15

View File

@ -155,7 +155,12 @@ class Upload < ActiveRecord::Base
# optimize image (except GIFs, SVGs and large PNGs)
if should_optimize?(file.path, [w, h])
ImageOptim.new.optimize_image!(file.path) rescue nil
begin
ImageOptim.new.optimize_image!(file.path)
rescue ImageOptim::Worker::TimeoutExceeded
# Don't optimize if it takes too long
Rails.logger.warn("ImageOptim timed out while optimizing #{filename}")
end
# update the file size
filesize = File.size(file.path)
end