Commit 932d3251 authored by Lin Jen-Shin's avatar Lin Jen-Shin

Move to Projects::HousecleaningService

parent 5c31c720
...@@ -372,7 +372,7 @@ class Project < ActiveRecord::Base ...@@ -372,7 +372,7 @@ class Project < ActiveRecord::Base
if Gitlab::ImportSources.importer_names.include?(project.import_type) && project.repo_exists? if Gitlab::ImportSources.importer_names.include?(project.import_type) && project.repo_exists?
project.run_after_commit do project.run_after_commit do
begin begin
Projects::ImportExport::CleanupService.new(project).execute Projects::HousecleaningService.new(project).execute
rescue Projects::HousekeepingService::LeaseTaken => e rescue Projects::HousekeepingService::LeaseTaken => e
Rails.logger.info("Could not perform housekeeping for project #{project.full_path} (#{project.id}): #{e}") Rails.logger.info("Could not perform housekeeping for project #{project.full_path} (#{project.id}): #{e}")
end end
......
module Projects
class HousecleaningService
def self.reserved_refs_names
%w[heads tags merge-requests keep-around environments]
end
def self.reserved_refs_regexp
names = reserved_refs_names.map(&Regexp.method(:escape)).join('|')
%r{\Arefs/(?:#{names})/}
end
def initialize(project)
@project = project
end
# This could raise Projects::HousekeepingService::LeaseTaken
def execute
Projects::HousekeepingService.new(@project).execute do
garbage_refs.each(&rugged.references.method(:delete))
end
end
private
def garbage_refs
@garbage_refs ||= begin
reserved_refs_regexp = self.class.reserved_refs_regexp
rugged.references.reject do |ref|
ref.name =~ reserved_refs_regexp
end
end
end
def rugged
@rugged ||= @project.repository.rugged
end
end
end
module Projects
module ImportExport
class CleanupService
def self.reserved_refs_names
%w[heads tags merge-requests keep-around environments]
end
def self.reserved_refs_regexp
names = reserved_refs_names.map(&Regexp.method(:escape)).join('|')
%r{\Arefs/(?:#{names})/}
end
def initialize(project)
@project = project
end
# This could raise Projects::HousekeepingService::LeaseTaken
def execute
Projects::HousekeepingService.new(@project).execute do
garbage_refs.each(&rugged.references.method(:delete))
end
end
private
def garbage_refs
@garbage_refs ||= begin
reserved_refs_regexp = self.class.reserved_refs_regexp
rugged.references.reject do |ref|
ref.name =~ reserved_refs_regexp
end
end
end
def rugged
@rugged ||= @project.repository.rugged
end
end
end
end
...@@ -1563,14 +1563,14 @@ describe Project do ...@@ -1563,14 +1563,14 @@ describe Project do
describe 'project import state transitions' do describe 'project import state transitions' do
context 'state transition: [:started] => [:finished]' do context 'state transition: [:started] => [:finished]' do
let(:cleanup_service) { spy(:cleanup_service) } let(:housecleaning_service) { spy(:housecleaning_service) }
let(:housekeeping_service) { spy(:housekeeping_service) } let(:housekeeping_service) { spy(:housekeeping_service) }
before do before do
allow(Projects::ImportExport::CleanupService) allow(Projects::HousecleaningService)
.to receive(:new) { cleanup_service } .to receive(:new) { housecleaning_service }
allow(cleanup_service) allow(housecleaning_service)
.to receive(:execute) { housekeeping_service.execute } .to receive(:execute) { housekeeping_service.execute }
allow(Projects::HousekeepingService) allow(Projects::HousekeepingService)
...@@ -1589,7 +1589,7 @@ describe Project do ...@@ -1589,7 +1589,7 @@ describe Project do
project.import_finish project.import_finish
expect(cleanup_service).to have_received(:execute) expect(housecleaning_service).to have_received(:execute)
expect(housekeeping_service).to have_received(:execute) expect(housekeeping_service).to have_received(:execute)
end end
......
require 'spec_helper' require 'spec_helper'
describe Projects::ImportExport::CleanupService do describe Projects::HousecleaningService do
subject { described_class.new(project) } subject { described_class.new(project) }
let(:project) { create(:project, :repository) } let(:project) { create(:project, :repository) }
......
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