diff options
author | Douglas Barbosa Alexandre <dbalexandre@gmail.com> | 2016-07-18 16:38:36 -0300 |
---|---|---|
committer | Douglas Barbosa Alexandre <dbalexandre@gmail.com> | 2016-07-21 10:31:49 -0300 |
commit | a6de806498c405355380be4b80f63d134658b779 (patch) | |
tree | 16729b67fb209feb247fa7812a0fc6f48147a082 /app/services | |
parent | 6ae177ef53e57d98f95238df990da2225bca3fab (diff) | |
download | gitlab-ce-a6de806498c405355380be4b80f63d134658b779.tar.gz |
Add service to clean up repository archive cache
Replace invocation of `find` with Ruby code that matches old cached
files in a better, and safe way to avoid data-integrity issues.
Diffstat (limited to 'app/services')
-rw-r--r-- | app/services/repository_archive_clean_up_service.rb | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/app/services/repository_archive_clean_up_service.rb b/app/services/repository_archive_clean_up_service.rb new file mode 100644 index 00000000000..9764df6492d --- /dev/null +++ b/app/services/repository_archive_clean_up_service.rb @@ -0,0 +1,42 @@ +class RepositoryArchiveCleanUpService + ALLOWED_ARCHIVE_EXTENSIONS = %w[tar tar.bz2 tar.gz zip].join(',').freeze + 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 + Dir.glob("#{path}/**.git/*{#{ALLOWED_ARCHIVE_EXTENSIONS}}") do |filename| + File.delete(filename) if older?(filename) + end + end + + def older?(filename) + File.exist?(filename) && File.new(filename).mtime < (Time.now - mmin * 60) + end + + def clean_up_empty_directories + Dir.glob("#{path}/**.git/").reverse_each do |dir| + Dir.rmdir(dir) if empty?(dir) + end + end + + def empty?(dir) + (Dir.entries(dir) - %w[. ..]).empty? + end +end |