Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable concurrent (threaded) uploading of files #284

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions lib/asset_sync/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class Invalid < StandardError; end
attr_accessor :run_on_precompile
attr_accessor :invalidate
attr_accessor :cdn_distribution_id
attr_accessor :concurrent_uploads

# FOG configuration
attr_accessor :fog_provider # Currently Supported ['AWS', 'Rackspace']
Expand All @@ -45,6 +46,7 @@ class Invalid < StandardError; end
validates :rackspace_api_key, :presence => true, :if => :rackspace?
validates :google_storage_secret_access_key, :presence => true, :if => :google?
validates :google_storage_access_key_id, :presence => true, :if => :google?
validates :concurrent_uploads, :inclusion => { :in => [true, false] }

def initialize
self.fog_region = nil
Expand All @@ -59,6 +61,7 @@ def initialize
self.enabled = true
self.run_on_precompile = true
self.cdn_distribution_id = nil
self.concurrent_uploads = true
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be off by default?

self.invalidate = []
load_yml! if defined?(Rails) && yml_exists?
end
Expand Down
10 changes: 9 additions & 1 deletion lib/asset_sync/storage.rb
Original file line number Diff line number Diff line change
Expand Up @@ -212,13 +212,21 @@ def upload_files
# fixes: https://github.com/rumblelabs/asset_sync/issues/19
local_files_to_upload = local_files - ignored_files - remote_files + always_upload_files
local_files_to_upload = (local_files_to_upload + get_non_fingerprinted(local_files_to_upload)).uniq
# keep track of threads for uploading
threads = ThreadGroup.new
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this only be initialized if concurrent_uploads is enabled?


# Upload new files
local_files_to_upload.each do |f|
next unless File.file? "#{path}/#{f}" # Only files.
upload_file f
if self.config.concurrent_uploads
threads.add(Thread.new { upload_file f })
else
upload_file f
end
end

sleep 1 while threads.list.any? # wait for threads to finish uploading

if self.config.cdn_distribution_id && files_to_invalidate.any?
log "Invalidating Files"
cdn ||= Fog::CDN.new(self.config.fog_options.except(:region))
Expand Down
4 changes: 1 addition & 3 deletions spec/unit/storage_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,7 @@ def check_file(file)
end

files = double()
local_files.count.times do
expect(files).to receive(:create) { |file| check_file(file) }
end
expect(files).to receive(:create){ |file| check_file(file) }.exactly(local_files.count).times
allow(storage).to receive_message_chain(:bucket, :files).and_return(files)
storage.upload_files
end
Expand Down