Use find instead Ruby to remove files due to performance reasons

parent 1bda4517
class RepositoryArchiveCleanUpService class RepositoryArchiveCleanUpService
ALLOWED_ARCHIVE_EXTENSIONS = %w[tar tar.bz2 tar.gz zip].join(',').freeze
LAST_MODIFIED_TIME_IN_MINUTES = 120 LAST_MODIFIED_TIME_IN_MINUTES = 120
def initialize(mmin = LAST_MODIFIED_TIME_IN_MINUTES) def initialize(mmin = LAST_MODIFIED_TIME_IN_MINUTES)
...@@ -21,22 +20,14 @@ class RepositoryArchiveCleanUpService ...@@ -21,22 +20,14 @@ class RepositoryArchiveCleanUpService
attr_reader :mmin, :path attr_reader :mmin, :path
def clean_up_old_archives def clean_up_old_archives
Dir.glob("#{path}/**.git/*{#{ALLOWED_ARCHIVE_EXTENSIONS}}") do |filename| 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))
File.delete(filename) if older?(filename)
end
end
def older?(filename)
File.exist?(filename) && File.new(filename).mtime < (Time.now - mmin * 60)
end end
def clean_up_empty_directories def clean_up_empty_directories
Dir.glob("#{path}/**.git/").reverse_each do |dir| run(%W(find #{path} -not -path #{path} -type d -empty -name \*.git -maxdepth 1 -delete))
Dir.rmdir(dir) if empty?(dir)
end
end end
def empty?(dir) def run(cmd)
(Dir.entries(dir) - %w[. ..]).empty? Gitlab::Popen.popen(cmd)
end end
end end
...@@ -30,13 +30,9 @@ describe RepositoryArchiveCleanUpService, services: true do ...@@ -30,13 +30,9 @@ describe RepositoryArchiveCleanUpService, services: true do
end end
context 'when archives older than 2 hours exists' do 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 it 'removes old files that matches valid archive extensions' do
dirname = File.join(path, 'sample.git') 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 service.execute
...@@ -46,7 +42,7 @@ describe RepositoryArchiveCleanUpService, services: true do ...@@ -46,7 +42,7 @@ describe RepositoryArchiveCleanUpService, services: true do
it 'keeps old files that does not matches valid archive extensions' do it 'keeps old files that does not matches valid archive extensions' do
dirname = File.join(path, 'sample.git') 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 service.execute
...@@ -56,7 +52,7 @@ describe RepositoryArchiveCleanUpService, services: true do ...@@ -56,7 +52,7 @@ describe RepositoryArchiveCleanUpService, services: true do
it 'keeps old files inside invalid directories' do it 'keeps old files inside invalid directories' do
dirname = File.join(path, 'john_doe/sample.git') 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 service.execute
...@@ -66,13 +62,9 @@ describe RepositoryArchiveCleanUpService, services: true do ...@@ -66,13 +62,9 @@ describe RepositoryArchiveCleanUpService, services: true do
end end
context 'when archives older than 2 hours does not exist' do 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 it 'keeps files that matches valid archive extensions' do
dirname = File.join(path, 'sample.git') 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 service.execute
...@@ -82,7 +74,7 @@ describe RepositoryArchiveCleanUpService, services: true do ...@@ -82,7 +74,7 @@ describe RepositoryArchiveCleanUpService, services: true do
it 'keeps files that does not matches valid archive extensions' do it 'keeps files that does not matches valid archive extensions' do
dirname = File.join(path, 'sample.git') 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 service.execute
...@@ -92,7 +84,7 @@ describe RepositoryArchiveCleanUpService, services: true do ...@@ -92,7 +84,7 @@ describe RepositoryArchiveCleanUpService, services: true do
it 'keeps files inside invalid directories' do it 'keeps files inside invalid directories' do
dirname = File.join(path, 'john_doe/sample.git') 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 service.execute
...@@ -101,12 +93,9 @@ describe RepositoryArchiveCleanUpService, services: true do ...@@ -101,12 +93,9 @@ describe RepositoryArchiveCleanUpService, services: true do
end end
end end
def create_temporary_files(dirname, extensions) def create_temporary_files(dirname, extensions, mtime)
FileUtils.mkdir_p(dirname) FileUtils.mkdir_p(dirname)
FileUtils.touch(extensions.map { |ext| File.join(dirname, "sample.#{ext}") }, mtime: Time.now - mtime)
extensions.flat_map do |extension|
FileUtils.touch(File.join(dirname, "sample.#{extension}"))
end
end end
end 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