-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
draft of a multithreaded sync command with a configurable pool size (…
…issue #19)
- Loading branch information
Showing
4 changed files
with
51 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
module NightcrawlerSwift | ||
class MultithreadSync < Command | ||
DEFAULT_POOL_SIZE = 5 | ||
|
||
def initialize | ||
require 'concurrent/utilities' | ||
require 'concurrent/executors' | ||
@logger = NightcrawlerSwift.logger | ||
end | ||
|
||
def execute args = {} | ||
pool_size = args[:pool_size] || DEFAULT_POOL_SIZE | ||
dir_path = args[:dir_path] | ||
|
||
@logger.info "[NightcrawlerSwift] dir_path: #{dir_path}" | ||
@logger.info "[NightcrawlerSwift] multithread sync, #{Concurrent.processor_count} processors" | ||
|
||
assets = Dir["#{dir_path}/**/**"]. | ||
reject {|fullpath| File.directory?(fullpath)}. | ||
map {|fullpath| | ||
path = fullpath.gsub("#{dir_path}/", "") | ||
OpenStruct.new(path: path, fullpath: fullpath) | ||
} | ||
|
||
pool = Concurrent::FixedThreadPool.new pool_size | ||
|
||
assets.each do |asset| | ||
pool.post do | ||
@logger.info "[NightcrawlerSwift] #{asset.path}" | ||
|
||
upload = Upload.new | ||
upload.execute asset.path, File.open(asset.fullpath, "r") | ||
end | ||
end | ||
|
||
sleep(1) while pool.queue_length > 0 | ||
|
||
@logger.info "[NightcrawlerSwift] shutting down" | ||
pool.shutdown | ||
pool.wait_for_termination | ||
end | ||
|
||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters