summaryrefslogtreecommitdiff
path: root/app/services/repository_archive_clean_up_service.rb
blob: 0b56b09738d7ce2ba6853340958801653d815789 (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
class RepositoryArchiveCleanUpService
  LAST_MODIFIED_TIME_IN_MINUTES = 120

  def initialize(mmin = LAST_MODIFIED_TIME_IN_MINUTES)
    @mmin = mmin
    @path = Gitlab.config.gitlab.repository_downloads_path
  end

  def execute
    Gitlab::Metrics.measure(:repository_archive_clean_up) do
      return unless File.directory?(path)

      clean_up_old_archives
      clean_up_empty_directories
    end
  end

  private

  attr_reader :mmin, :path

  def clean_up_old_archives
    run(%W(find #{path} -not -path #{path} -type f \( -name \*.tar -o -name \*.bz2 -o -name \*.tar.gz -o -name \*.zip \) -maxdepth 2 -mmin +#{mmin} -delete))
  end

  def clean_up_empty_directories
    run(%W(find #{path} -not -path #{path} -type d -empty -name \*.git -maxdepth 1 -delete))
  end

  def run(cmd)
    Gitlab::Popen.popen(cmd)
  end
end