Search code examples
rubyruby-on-rails-4carrierwaverake-taskattachment-fu

Multitasked conversion from attachment_fu to Carrierwave


I have a rake task to convert my photos from attachment_fu to Carrierwave.

Is there a way to run the task :convert multiple times in parallel to make this faster ?

Here is my working task :

namespace :photos do
  task :convert => :environment do
    Item.all.each do |item|
      item.photos.each do |photo|
        new_photo = ItemPhoto.new :photo => File.open(File.join(Rails.root, "public", photo.public_filename)),
                                  :item_id => item.id

        new_photo.save
      end
      item.update_attribute :migrated, true
    end     
  end
end

Solution

  • Take a look at the parallel gem

    It could look like this for 8 concurrent processes:

    namespace :photos do
      task :convert => :environment do
        Parallel.each(Item.all, :in_processes => 8) do |item|
           item.photos.each do |photo|
              new_photo = ItemPhoto.new :photo => File.open(File.join(Rails.root, "public", photo.public_filename)),
                                  :item_id => item.id
    
              new_photo.save
           end
           item.update_attribute :migrated, true
        end
      end
    end
    

    `