Class: RedSnapper

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

Constant Summary collapse

TARSNAP =
'tarsnap'
THREAD_POOL_SIZE =
10
MAX_FILES_PER_JOB =
1000

Instance Method Summary collapse

Constructor Details

#initialize(archive, options = {}) ⇒ RedSnapper

Returns a new instance of RedSnapper.



10
11
12
13
# File 'lib/redsnapper.rb', line 10

def initialize(archive, options = {})
  @archive = archive
  @options = options
end

Instance Method Details

#file_groupsObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/redsnapper.rb', line 15

def file_groups
  command = [ TARSNAP, '-tf', @archive, *@options[:tarsnap_options] ]
  command.push(@options[:directory]) if @options[:directory]

  files = []
  dirs = Set.new

  Open3.popen3(*command) do |_, out, _|
    out.gets(nil).split("\n").each do |entry|
      if entry.end_with?('/')
        dirs.add(entry)
      else
        files.push(entry)
      end
    end
  end

  files.each { |f| dirs.delete(File.dirname(f) + '/') }
  empty_dirs = dirs.clone

  dirs.each do |dir|
    components = dir.split('/')
    components.each_with_index do |component, i|
      empty_dirs.delete(components[0, i].join('/') + '/')
    end
  end

  files.push(*empty_dirs)
  files.each_slice([ (files.size.to_f / THREAD_POOL_SIZE).ceil, MAX_FILES_PER_JOB ].min).to_a
end

#runObject



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/redsnapper.rb', line 46

def run
  pool = Thread.pool(THREAD_POOL_SIZE)
  mutex = Mutex.new

  file_groups.each do |chunk|
    pool.process do
      command = [ TARSNAP, '-xvf', @archive, *(@options[:tarsnap_options] + chunk) ]
      Open3.popen3(*command) do |_, _, err|
        while line = err.gets
          mutex.synchronize { warn line.chomp }
        end
      end
    end
  end

  pool.shutdown
end