debian-mirror-gitlab/app/services/repository_archive_clean_up_service.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

55 lines
1.5 KiB
Ruby
Raw Permalink Normal View History

2018-11-18 11:00:15 +05:30
# frozen_string_literal: true
2021-01-03 14:25:43 +05:30
# RepositoryArchiveCleanUpService removes cached repository archives
# that are generated on-the-fly by Gitaly. These files are stored in the
# following form (as defined in lib/gitlab/git/repository.rb) and served
# by GitLab Workhorse:
#
# /path/to/repository/downloads/project-N/sha/@v2/archive.format
#
# Legacy paths omit the @v2 prefix.
#
# For example:
#
# /var/opt/gitlab/gitlab-rails/shared/cache/archive/project-1/master/@v2/archive.zip
2016-08-24 12:49:21 +05:30
class RepositoryArchiveCleanUpService
LAST_MODIFIED_TIME_IN_MINUTES = 120
2021-01-03 14:25:43 +05:30
# For `/path/project-N/sha/@v2/archive.zip`, `find /path -maxdepth 4` will find this file
MAX_ARCHIVE_DEPTH = 4
2016-08-24 12:49:21 +05:30
attr_reader :mmin, :path
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
2018-10-15 14:42:47 +05:30
next unless File.directory?(path)
2016-08-24 12:49:21 +05:30
clean_up_old_archives
clean_up_empty_directories
end
end
private
def clean_up_old_archives
2021-01-03 14:25:43 +05:30
run(%W(find #{path} -mindepth 1 -maxdepth #{MAX_ARCHIVE_DEPTH} -type f \( -name \*.tar -o -name \*.bz2 -o -name \*.tar.gz -o -name \*.zip \) -mmin +#{mmin} -delete))
2016-08-24 12:49:21 +05:30
end
def clean_up_empty_directories
2021-01-03 14:25:43 +05:30
(1...MAX_ARCHIVE_DEPTH).reverse_each { |depth| clean_up_empty_directories_with_depth(depth) }
end
def clean_up_empty_directories_with_depth(depth)
run(%W(find #{path} -mindepth #{depth} -maxdepth #{depth} -type d -empty -delete))
2016-08-24 12:49:21 +05:30
end
def run(cmd)
Gitlab::Popen.popen(cmd)
end
end