CarrierWave不存储上传文件的URL

时间:2017-10-15 12:47:48

标签: ruby-on-rails ruby carrierwave background-process sidekiq

我正在尝试在后台处理我上传的图片(CarrierWave)(通过Sidekiq)。

这是我的配置(carrierwave_delay gem):

class IconUploader < CarrierWave::Uploader::Base

    def is_processing_delayed? img = nil
        !! @is_processing_delayed
    end

    def is_processing_immediate? img = nil
        ! is_processing_delayed?
    end

    def is_processing_delayed= value
        @is_processing_delayed = value
    end

    include CarrierWave::MiniMagick
    storage :fog

    def store_dir
        "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
    end

    process resize_to_fill: [500, 500]
    version :large, if: :is_processing_delayed? do
        process resize_to_fill: [192, 192]
    end
    version :medium, from: :large, if: :is_processing_delayed? do
        process resize_to_fill: [144, 144]
    end

    def extension_whitelist
        %w(png)
    end

end

class App < ApplicationRecord

    mount_uploader :icon, IconUploader

    carrierwave_delay [:icon]

    cattr_accessor :carrierwave_delay_columns

    def self.carrierwave_delay columns
        after_save :carrierwave_delay_process
        self.carrierwave_delay_columns = columns
    end

    def self.carrierwave_delay_queue
        :default
    end
    def self.carrierwave_delay_priority
        20
    end

    def recreate_delayed_versions!
        self.class.carrierwave_delay_columns.each do |column|
            unless self.send(column).file.nil?
                self.send(column).is_processing_delayed = true
                self.send(column).recreate_versions!
            end
        end
    end

    private

    def carrierwave_delay_process
        self.delay(queue: self.class.carrierwave_delay_queue, priority: self.class.carrierwave_delay_priority, retry: false).recreate_delayed_versions!
    end

end

所有文件都已成功处理并存储在S3上,如config/initializers/carrierwave.rb中所配置。但不幸的是icon.send(version).url方法仍然返回nil(因此在成功上传文件后不会更新)。

定期上传效果很好。

0 个答案:

没有答案