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.
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 |
|
Also, add this code to your model.
1
2
mount_uploader :avatar, AvatarUploader
process_in_background :avatar
Following the install instructions, this file is created in the initializers directory.
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.
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.
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