Iacutone.rb

coding and things

Sidekiq and Carrierwave, Part 3

| Comments

I decided to use the Carrierwave Backgrounder Gem in order to asynchronously upload photos to Amazon S3. This is a different gem than the one Ryan Bates uses in his Railscast. I was having difficulty implimenting the Carrierwave Direct Gem However, the Backgrounder Gem works great! This is the last installment of the three part series on uploading photos.

This is the final version of the avatar_uploader.rb file. Just add line 3, which is in the Backgrounder documentaion.

avatar_uploader.rb lang: ruby
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
  class AvatarUploader < CarrierWave::Uploader::Base
  # include CarrierWaveDirect::Uploader
  include ::CarrierWave::Backgrounder::Delay

  # Include RMagick or MiniMagick support:
  include CarrierWave::RMagick
  # include CarrierWave::MiniMagick

  # Include the Sprockets helpers for Rails 3.1+ asset pipeline compatibility:
  include Sprockets::Helpers::RailsHelper
  include Sprockets::Helpers::IsolatedHelper

  # Choose what kind of storage to use for this uploader:
  # storage :file
  storage :fog

  include CarrierWave::MimeTypes
  process :set_content_type

  # Override the directory where uploaded files will be stored.
  # This is a sensible default for uploaders that are meant to be mounted:
  # def store_dir
  #   "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
  # end

  # Provide a default URL as a default if there hasn't been a file uploaded:
  # def default_url
  #   # For Rails 3.1+ asset pipeline compatibility:
  #   # asset_path("fallback/" + [version_name, "default.png"].compact.join('_'))
  #
  #   "/images/fallback/" + [version_name, "default.png"].compact.join('_')
  # end

  # Process files as they are uploaded:
  # process :scale => [200, 300]
  #
  # def scale(width, height)
  #   # do something
  # end

  # Create different versions of your uploaded files:
  version :thumb do
    process :resize_to_limit => [150, 150]
  end

  version :profile do
    process :resize_to_limit => [200, 200]
  end

  # Add a white list of extensions which are allowed to be uploaded.
  # For images you might use something like this:
  # def extension_white_list
  #   %w(jpg jpeg gif png)
  # end

  # Override the filename of the uploaded files:
  # Avoid using model.id or version_name here, see uploader/store.rb for details.
  # def filename
  #   "something.jpg" if original_filename
  # end
  end

Also, add this code to your model.

profile.rb
1
2
 mount_uploader :avatar, AvatarUploader
  process_in_background :avatar

Following the install instructions, this file is created in the initializers directory.

initializers/carrierwave_backgrounder.rb
1
2
3
4
5
6
7
8
 CarrierWave::Backgrounder.configure do |c|
    # c.backend :delayed_job, queue: :carrierwave
    # c.backend :resque, queue: :carrierwave
    c.backend :sidekiq, queue: :carrierwave
    # c.backend :girl_friday, queue: :carrierwave
    # c.backend :qu, queue: :carrierwave
    # c.backend :qc
  end

Then boot up Sidekiq to listen for jobs with sidekiq -q carrierwave command in your terminal. You might need to start your Redis server with the command redis-server. Your background worker should now asynchronously process background jobs!

Sidekiq also has a cool interface to show what workers are up, how many successes and failures there have been and some other neat features. It is real easy to set up. First add the following code to your Gemfile.

Gemfile
1
2
 gem 'slim', '>= 1.1.0'
  gem 'sinatra', '>= 1.3.0', :require => nil

Then create this route with the require line before the beginning of the block.

routes.rb
1
2
3
4
 require 'sidekiq/web'

  mount Sidekiq::Web => '/sidekiq'

I really like this gem and recommend it to anyone trying to run background processes to upload pictures via Carrierwave.

h/t Blake Johnson

Comments