summaryrefslogtreecommitdiff
path: root/app/services/archive_repository_service.rb
blob: e1b41527d8dc5d5efb86d47a1f11186de9845a6c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
class ArchiveRepositoryService
  attr_reader :project, :ref, :format

  def initialize(project, ref, format)
    format ||= 'tar.gz'
    @project, @ref, @format = project, ref, format.downcase
  end

  def execute(options = {})
    project.repository.clean_old_archives

    raise "No archive file path" unless file_path

    return file_path if archived?

    unless archiving?
      RepositoryArchiveWorker.perform_async(project.id, ref, format)
    end

    archived = wait_until_archived(options[:timeout] || 5.0)

    file_path if archived
  end

  private

  def storage_path
    Gitlab.config.gitlab.repository_downloads_path
  end

  def file_path
    @file_path ||= project.repository.archive_file_path(ref, storage_path, format)
  end

  def pid_file_path
    @pid_file_path ||= project.repository.archive_pid_file_path(ref, storage_path, format)
  end

  def archived?
    File.exist?(file_path)
  end

  def archiving?
    File.exist?(pid_file_path)
  end

  def wait_until_archived(timeout = 5.0)
    return archived? if timeout == 0.0
    
    t1 = Time.now

    begin
      sleep 0.1

      success = archived?

      t2 = Time.now
    end until success || t2 - t1 >= timeout

    success
  end
end