Search code examples
ruby-on-railscarrierwavebackground-processworkersidekiq

How to use Sidekiq to push a file upload into the background?


I'm working on a Rails app that allows you to upload large music files. I'd like to upload these in the background so that when you start an upload it gets moved to a sidekiq worker while the user enters meta data about the file such as the track name and artist, etc.

I was able to follow this railscast to get image processing moved to the background: http://railscasts.com/episodes/383-uploading-to-amazon-s3?view=asciicast

But I can't quite figure out how to move the actual upload of the file to the background. Is there a certain callback or Sidekiq method I should use?

Are there any resources on how to do something like this?

Here's my song model: https://gist.github.com/leemcalilly/5001583

My songs controller: https://gist.github.com/leemcalilly/5001590

My upload form: https://gist.github.com/leemcalilly/5001586

My uploader (using carrierwave with carrierwave_direct to s3): https://gist.github.com/leemcalilly/5001601

That code works to upload to s3, but the browser is tied up in a Rails process while the file is uploading. I'd prefer to move that to a background process. The sidekiq code there I think is really from the Railscast that handles image processing, but I don't quite understand why the image processing is working in that Railscast.

Any help getting pointed in the right direction is much appreciated.


Solution

  • Your TrackUploader class basically needs a "perform" method that takes an argument. In that method, put in the logic to upload the image.

    You need to specify the name of the queue as well (ie track_uploader)

    Afterwards you can add an item to this worker queue via TrackUploader.perform_async(argument).

    You can start sidekiq manually, or using God, to ensure it stays alive.

    class TrackUploader < CarrierWave::Uploader::Base
    
        include Sidekiq::Worker
        sidekiq_options :queue => :track_uploader
    
        include Sprockets::Helpers::RailsHelper
        include Sprockets::Helpers::IsolatedHelper
    
        include CarrierWaveDirect::Uploader
    
        # Recommended for use with fog
        include CarrierWave::MimeTypes
        process :set_content_type
    
        def extension_white_list
          %w(mp3 m4a wav aiff flac)
        end
    
        def perform(argument)
           #do the actual uploading here
        end
    
    end