How to use Sidekiq to download a file in the background?

I am working on a Rails application that allows you to download large music files. I would like to load them in the background so that when loading it is moved to the sidekiq handler while the user enters metadata about the file, such as the name of the track and artist, etc.

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

But I can’t understand how to transfer the actual file upload to the background. Is there any callback or Sidekiq method that I should use?

Are there any resources on how to do this?

Here is my song: https://gist.github.com/leemcalilly/5001583

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

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

My bootloader (using carrier with carrier wave_direct on s3): https://gist.github.com/leemcalilly/5001601

This code works to load on s3, but the browser is connected to the Rails process during file upload. I would rather move this to a background process. The sidekiq code, I think, is really from Railscast, which handles image processing, but I don’t quite understand why image processing works in this Railscast.

Any help received in the right direction is greatly appreciated.

+4
source share
1 answer

Your TrackUploader class basically needs a run method that takes an argument. In this method, add logic to load the image.

You also need to specify a queue name (i.e. track_uploader)

You can then add the item to this work queue via TrackUploader.perform_async (argument).

You can run sidekiq manually or use God to stay 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 
+3
source

All Articles