1

I have an image uploader on a form, and images upload fine without the background processing. Since moving to Heroku, the larger source images often time out, so I'm looking to move this to a background job. In doing so, I looked at carrierwave_backgrounder since it seemed to cover what I need.

I've found a fork of carrierwave_backgrounder and I've forked it to my own repo. To get it working in 5.2, the author said he had to hardcode the path (https://github.com/lardawge/carrierwave_backgrounder/issues/282, https://github.com/lardawge/carrierwave_backgrounder/issues/280)

This fails on store_in_background. Running just process_in_background yields locally processed images in development, but they are not uploaded to S3.

The model field in question is issue.image and here's how it's mounted in issue.rb:

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

Here's the output error:

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'

and here's the relevant method from my store_asset_mixin.rb method from my local version of the gem. The line 40 mentioned in the error is the one that starts with @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

So asset_tmp is "", yet asset but I have no idea why. Stepping through it with debugger it seems that the record.image is present, but the record.image_tmp column is simply empty and record.image_cache is nil.

John Athayde
  • 580
  • 2
  • 13

1 Answers1

2

Not really sure if it helps you but what I did in the past: basically I've created a new worker with is doing both at once. Maybe it can bring some idea for you too.

model:

store_in_background :photo, StoreTmpAndProcessWorker

worker:

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

I'd also did some changes in my fork, but nothing special: https://github.com/igorkasyanchuk/carrierwave_backgrounder. My app works with Rails 5.2.

Igor Kasyanchuk
  • 766
  • 6
  • 12
  • Thanks Igor! I'll give this a whirl this weekend and see how it goes. I appreciate it! – John Athayde Mar 02 '19 at 03:02
  • Re-reading docs and realizing I shouldn't be using `store_in_background` but only `process_in_background` (Heroku production). Will this worker still function when attached to process_in_background? Since it calls store after processing, I would expect it to. Also, what do you do in your channel? Just change out a "processing" image for the final one? – John Athayde Mar 03 '19 at 21:29
  • 1
    I have a form with dropzone (where I can upload multiple files). So I can drop 10 files and see that they are "processing". Once it's processing I'm refreshing processing preview image with real image. – Igor Kasyanchuk Mar 05 '19 at 10:09