Use find instead Ruby to remove files due to performance reasons

parent 1bda4517
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)
......@@ -21,22 +20,14 @@ class RepositoryArchiveCleanUpService
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)
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
Dir.glob("#{path}/**.git/").reverse_each do |dir|
Dir.rmdir(dir) if empty?(dir)
end
run(%W(find #{path} -not -path #{path} -type d -empty -name \*.git -maxdepth 1 -delete))
end
def empty?(dir)
(Dir.entries(dir) - %w[. ..]).empty?
def run(cmd)
Gitlab::Popen.popen(cmd)
end
end
......@@ -30,13 +30,9 @@ describe RepositoryArchiveCleanUpService, services: true do
end
context 'when archives older than 2 hours exists' do
before do
allow_any_instance_of(File).to receive(:mtime).and_return(2.hours.ago)
end
it 'removes old files that matches valid archive extensions' do
dirname = File.join(path, 'sample.git')
files = create_temporary_files(dirname, %w[tar tar.bz2 tar.gz zip])
files = create_temporary_files(dirname, %w[tar tar.bz2 tar.gz zip], 2.hours)
service.execute
......@@ -46,7 +42,7 @@ describe RepositoryArchiveCleanUpService, services: true do
it 'keeps old files that does not matches valid archive extensions' do
dirname = File.join(path, 'sample.git')
files = create_temporary_files(dirname, %w[conf rb])
files = create_temporary_files(dirname, %w[conf rb], 2.hours)
service.execute
......@@ -56,7 +52,7 @@ describe RepositoryArchiveCleanUpService, services: true do
it 'keeps old files inside invalid directories' do
dirname = File.join(path, 'john_doe/sample.git')
files = create_temporary_files(dirname, %w[conf rb tar tar.gz])
files = create_temporary_files(dirname, %w[conf rb tar tar.gz], 2.hours)
service.execute
......@@ -66,13 +62,9 @@ describe RepositoryArchiveCleanUpService, services: true do
end
context 'when archives older than 2 hours does not exist' do
before do
allow_any_instance_of(File).to receive(:mtime).and_return(1.hour.ago)
end
it 'keeps files that matches valid archive extensions' do
dirname = File.join(path, 'sample.git')
files = create_temporary_files(dirname, %w[tar tar.bz2 tar.gz zip])
files = create_temporary_files(dirname, %w[tar tar.bz2 tar.gz zip], 1.hour)
service.execute
......@@ -82,7 +74,7 @@ describe RepositoryArchiveCleanUpService, services: true do
it 'keeps files that does not matches valid archive extensions' do
dirname = File.join(path, 'sample.git')
files = create_temporary_files(dirname, %w[conf rb])
files = create_temporary_files(dirname, %w[conf rb], 1.hour)
service.execute
......@@ -92,7 +84,7 @@ describe RepositoryArchiveCleanUpService, services: true do
it 'keeps files inside invalid directories' do
dirname = File.join(path, 'john_doe/sample.git')
files = create_temporary_files(dirname, %w[conf rb tar tar.gz])
files = create_temporary_files(dirname, %w[conf rb tar tar.gz], 1.hour)
service.execute
......@@ -101,12 +93,9 @@ describe RepositoryArchiveCleanUpService, services: true do
end
end
def create_temporary_files(dirname, extensions)
def create_temporary_files(dirname, extensions, mtime)
FileUtils.mkdir_p(dirname)
extensions.flat_map do |extension|
FileUtils.touch(File.join(dirname, "sample.#{extension}"))
end
FileUtils.touch(extensions.map { |ext| File.join(dirname, "sample.#{ext}") }, mtime: Time.now - mtime)
end
end
end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment