Rails 5.2中的carrierwave_backgrounder asset_tmp无

时间:2019-03-01 13:55:58

标签: ruby-on-rails carrierwave carrierwave-backgrounder

我在表单上有一个图像上传器,图像无需后台处理就可以很好地上传。自从移至Heroku以来,较大的源图像通常会超时,因此我希望将其移至后台作业。为此,我查看了carrierwave_backgrounder,因为它似乎满足了我的需求。

我找到了carrierwave_backgrounder的分叉,并将其分叉到自己的存储库中。为了使其在5.2中工作,作者说他必须对路径(https://github.com/lardawge/carrierwave_backgrounder/issues/282https://github.com/lardawge/carrierwave_backgrounder/issues/280)进行硬编码

这在store_in_background上失败。仅运行process_in_background会在开发过程中生成本地处理的图像,但不会将其上传到S3。

有问题的模型字段是issue.image,这是它在issue.rb中的安装方式:

# Image attachment via Carrierwave
  mount_uploader :image, ImageUploader
  process_in_background :image
  store_in_background :image
  attr_accessor :image_cache

这是输出错误:

2019-02-24T01:05:33.168Z 43666 TID-oxnydp4bq CarrierWave::Workers::StoreAsset JID-83163770b014d37186118cf9 INFO: start
2019-02-24T01:05:33.545Z 43666 TID-oxnydp4bq CarrierWave::Workers::StoreAsset JID-83163770b014d37186118cf9 INFO: fail: 0.377 sec
2019-02-24T01:05:33.545Z 43666 TID-oxnydp4bq WARN: {"context":"Job raised exception","job":{"class":"CarrierWave::Workers::StoreAsset","args":["Issue","6355","image"],"queue":"carrierwave","retry":true,"jid":"83163770b014d37186118cf9","created_at":1550970333.167957,"enqueued_at":1550970333.168011},"jobstr":"{\"class\":\"CarrierWave::Workers::StoreAsset\",\"args\":[\"Issue\",\"6355\",\"image\"],\"queue\":\"carrierwave\",\"retry\":true,\"jid\":\"83163770b014d37186118cf9\",\"created_at\":1550970333.167957,\"enqueued_at\":1550970333.168011}"}
2019-02-24T01:05:33.545Z 43666 TID-oxnydp4bq WARN: TypeError: no implicit conversion of nil into String
2019-02-24T01:05:33.545Z 43666 TID-oxnydp4bq WARN: /Users/jathayde/Development/Meticulous/carrierwave_backgrounder/lib/backgrounder/workers/store_asset_mixin.rb:40:in `join'

,这是我的gem本地版本中的store_asset_mixin.rb方法中的相关方法。错误中提到的第40行是以@tmp_directory开头的行:

      def store_directories(record)
        asset, asset_tmp = record.send(:"#{column}"), record.send(:"#{column}_tmp")
        cache_directory  = File.expand_path(asset.cache_dir, asset.root)
        # @cache_path      = File.join(cache_directory, asset_tmp)
        # # XXX Hardcoded our path here... not ideal..
        @cache_path      = open("https://patchvault.s3.amazonaws.com/uploads/tmp/#{asset_tmp}")
        @tmp_directory   = File.join(cache_directory, asset_tmp.split("/").first)
      end

所以asset_tmp"",虽然很有价值,但我不知道为什么。用debugger逐步执行该操作,似乎存在record.image,但是record.image_tmp列只是空的,而record.image_cache为nil。

1 个答案:

答案 0 :(得分:2)

不太确定是否对您有帮助,但是我过去做过什么:基本上,我创建了一个新员工,可以同时完成这两项工作。也许它也可以为您带来一些想法。

型号:

store_in_background :photo, StoreTmpAndProcessWorker

工人:

class StoreTmpAndProcessWorker < ::CarrierWave::Workers::StoreAsset
  sidekiq_options retry: 1

  def perform(*args)
    Chewy.strategy(:atomic) do
      record = super(*args)
      ::CarrierWave::Workers::ProcessAsset.new.perform(*args)

      photo = Photo.find_by(id: args[1])

      if photo
        scheme = case Rails.env
                 when 'production' then 'https'
                 when 'staging' then 'http'
                 end

        ActionCable.server.broadcast "gallery_#{photo.photable_id}_channel",
          url: BasePresenter.urlify(photo.photo.scaled.url, scheme: scheme), id: photo.id
      end
    end
  end
end

我也对叉子进行了一些更改,但没什么特别的:https://github.com/igorkasyanchuk/carrierwave_backgrounder。我的应用程序适用于Rails 5.2。