从attachment_fu到Carrierwave的多任务转换

时间:2015-12-03 10:49:28

标签: ruby ruby-on-rails-4 carrierwave rake-task attachment-fu

我有一个rake任务将我的照片从attachment_fu转换为Carrierwave。

有没有办法并行多次运行任务:convert以使其更快?

这是我的工作任务:

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

1 个答案:

答案 0 :(得分:0)

看一下parallel gem

对于8个并发进程,它看起来像这样:

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

`