class CyberarmEngine::Cache::DownloadManager
Attributes
downloads[R]
Public Class Methods
new(max_parallel_downloads: 4)
click to toggle source
# File lib/cyberarm_engine/cache/download_manager.rb, line 6 def initialize(max_parallel_downloads: 4) @max_parallel_downloads = max_parallel_downloads @downloads = [] end
Public Instance Methods
active_downloads()
click to toggle source
# File lib/cyberarm_engine/cache/download_manager.rb, line 36 def active_downloads @downloads.select { |d| %i[pending downloading].include?(d.status) } end
download(url:, save_as: nil, &callback)
click to toggle source
# File lib/cyberarm_engine/cache/download_manager.rb, line 11 def download(url:, save_as: nil, &callback) uri = URI(url) save_as ||= "filename_path" # TODO: if no save_as path is provided, then get one from the Cache controller @downloads << Download.new(uri: uri, save_as: save_as, callback: callback) end
progress()
click to toggle source
# File lib/cyberarm_engine/cache/download_manager.rb, line 26 def progress remaining_bytes = @downloads.map { |d| d.remaining_bytes }.sum total_bytes = @downloads.map { |d| d.total_bytes }.sum v = 1.0 - (remaining_bytes.to_f / total_bytes) return 0.0 if v.nan? v end
prune()
click to toggle source
# File lib/cyberarm_engine/cache/download_manager.rb, line 49 def prune @downloads.delete_if { |d| d.status == :finished || d.status == :failed } end
status()
click to toggle source
# File lib/cyberarm_engine/cache/download_manager.rb, line 18 def status if active_downloads > 0 :busy else :idle end end
update()
click to toggle source
# File lib/cyberarm_engine/cache/download_manager.rb, line 40 def update @downloads.each do |download| if download.status == :pending && active_downloads.size <= @max_parallel_downloads download.status = :downloading Thread.start { download.download } end end end