namespace :s3 do
  desc "Upload clean assets to S3 (percentage: fraction of assets to upload; default: 0)"
  task :upload_all_clean_assets, [:percentage] => [:environment] do |_t, args|
    random_number_generator = Random.new
    Asset.where(state: 'clean').each do |asset|
      if asset.file.file.exists?
        if random_number_generator.rand(100) < args[:percentage].to_i
          asset.delay(priority: 10).save_to_cloud_storage
        end
      else
        puts "Ignoring asset (#{asset.id}) with missing file: #{asset.file.file.path}"
      end
    end
  end
end

Add rake task to upload assets to S3
Add rake task to upload assets to S3

This iterates through the assets and enqueues a job to upload those that
have a file on the NFS mount (this should be most of them). The jobs are
enqueued with a "lower" priority (10) than the default (0) [1] with the
idea that they should not interfere with publishing apps uploading new
assets.

The percentage argument is intended to allow us to test uploading a
fraction of the assets so we can estimate how long these jobs will take
to run.

Note that it's safe to run the task multiple times, because assets with
identical content will only be uploaded the first time.

[1]: https://github.com/collectiveidea/delayed_job#parameters
