Class: PuushLoad

Inherits:
Object
  • Object
show all
Defined in:
lib/puushload.rb

Constant Summary collapse

MAX_THREADS =
50
ARCHIVE_SIZE =
1000

Instance Method Summary collapse

Constructor Details

#initialize(outputFolder, logger) ⇒ PuushLoad

Returns a new instance of PuushLoad.



19
20
21
22
23
24
# File 'lib/puushload.rb', line 19

def initialize(outputFolder, logger)
  @logger = logger
  @logger = Logger.new(STDOUT) if @logger.nil?
  @logger.level = Logger::INFO
  @output_folder = File.absolute_path(outputFolder)
end

Instance Method Details

#download_filesObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/puushload.rb', line 26

def download_files
  @logger.info "Starting download!"
  
  spawned_threads = []
  downloaded_files = 0
  archives = 0
  current_tmp_dir = Dir.mktmpdir
  archive_file_counter = 0
  FileUtils.mkdir_p(@output_folder)
  
  PuushIDGenerator.generate do |id|
    spawned_threads << Thread.new {
      if PuushDownloader.download_file(id, current_tmp_dir, @logger) then
        downloaded_files += 1 
        archive_file_counter += 1
      end
      
    }

    if spawned_threads.length >= MAX_THREADS then
      spawned_threads.each { |spawnedThread| spawnedThread.join }
      spawned_threads.clear
      @logger.info "Killed some threads. #{downloaded_files} Files so far!"
    end
    
    if archive_file_counter >= ARCHIVE_SIZE then
      archive_file_counter = 0
      make_archive((@output_folder + "/" + archive_name(archives.to_s)), (current_tmp_dir))
        
      @logger.info "Created archive with id: #{archives} !"
      current_tmp_dir = Dir.mktmpdir
      archives += 1
    end
    
    sleep 0.01
    
  end
  @logger.info "Actually, this is impossible, but you finished it!"
end