Commit 0347681c authored by Bob Van Landuyt's avatar Bob Van Landuyt

Merge branch 'pages-transfer-worker-guard' into 'master'

Only schedule PagesTransferWorker when project or namespace has pages

See merge request gitlab-org/gitlab!40603
parents 95a8b847 9fb8a54d
...@@ -25,8 +25,10 @@ module Storage ...@@ -25,8 +25,10 @@ module Storage
Gitlab::UploadsTransfer.new.move_namespace(path, former_parent_full_path, parent_full_path) Gitlab::UploadsTransfer.new.move_namespace(path, former_parent_full_path, parent_full_path)
if ::Feature.enabled?(:async_pages_move_namespace_transfer, self) if ::Feature.enabled?(:async_pages_move_namespace_transfer, self)
run_after_commit do if any_project_with_pages_deployed?
Gitlab::PagesTransfer.new.async.move_namespace(path, former_parent_full_path, parent_full_path) run_after_commit do
Gitlab::PagesTransfer.new.async.move_namespace(path, former_parent_full_path, parent_full_path)
end
end end
else else
Gitlab::PagesTransfer.new.move_namespace(path, former_parent_full_path, parent_full_path) Gitlab::PagesTransfer.new.move_namespace(path, former_parent_full_path, parent_full_path)
...@@ -35,10 +37,12 @@ module Storage ...@@ -35,10 +37,12 @@ module Storage
Gitlab::UploadsTransfer.new.rename_namespace(full_path_before_last_save, full_path) Gitlab::UploadsTransfer.new.rename_namespace(full_path_before_last_save, full_path)
if ::Feature.enabled?(:async_pages_move_namespace_rename, self) if ::Feature.enabled?(:async_pages_move_namespace_rename, self)
full_path_was = full_path_before_last_save if any_project_with_pages_deployed?
full_path_was = full_path_before_last_save
run_after_commit do run_after_commit do
Gitlab::PagesTransfer.new.async.rename_namespace(full_path_was, full_path) Gitlab::PagesTransfer.new.async.rename_namespace(full_path_was, full_path)
end
end end
else else
Gitlab::PagesTransfer.new.rename_namespace(full_path_before_last_save, full_path) Gitlab::PagesTransfer.new.rename_namespace(full_path_before_last_save, full_path)
......
...@@ -353,6 +353,10 @@ class Namespace < ApplicationRecord ...@@ -353,6 +353,10 @@ class Namespace < ApplicationRecord
) )
end end
def any_project_with_pages_deployed?
all_projects.with_pages_deployed.any?
end
def closest_setting(name) def closest_setting(name)
self_and_ancestors(hierarchy_order: :asc) self_and_ancestors(hierarchy_order: :asc)
.find { |n| !n.read_attribute(name).nil? } .find { |n| !n.read_attribute(name).nil? }
......
...@@ -97,16 +97,18 @@ module Projects ...@@ -97,16 +97,18 @@ module Projects
end end
if ::Feature.enabled?(:async_pages_move_project_rename, project) if ::Feature.enabled?(:async_pages_move_project_rename, project)
# Block will be evaluated in the context of project so we need if project.pages_deployed?
# to bind to a local variable to capture it, as the instance # Block will be evaluated in the context of project so we need
# variable and method aren't available on Project # to bind to a local variable to capture it, as the instance
path_before_local = @path_before # variable and method aren't available on Project
path_before_local = @path_before
project.run_after_commit_or_now do
Gitlab::PagesTransfer project.run_after_commit_or_now do
.new Gitlab::PagesTransfer
.async .new
.rename_project(path_before_local, path, namespace.full_path) .async
.rename_project(path_before_local, path, namespace.full_path)
end
end end
else else
Gitlab::PagesTransfer Gitlab::PagesTransfer
......
...@@ -391,14 +391,14 @@ RSpec.describe Namespace do ...@@ -391,14 +391,14 @@ RSpec.describe Namespace do
let(:uploads_dir) { FileUploader.root } let(:uploads_dir) { FileUploader.root }
let(:pages_dir) { File.join(TestEnv.pages_path) } let(:pages_dir) { File.join(TestEnv.pages_path) }
def expect_project_directories_at(namespace_path) def expect_project_directories_at(namespace_path, with_pages: true)
expected_repository_path = File.join(TestEnv.repos_path, namespace_path, 'the-project.git') expected_repository_path = File.join(TestEnv.repos_path, namespace_path, 'the-project.git')
expected_upload_path = File.join(uploads_dir, namespace_path, 'the-project') expected_upload_path = File.join(uploads_dir, namespace_path, 'the-project')
expected_pages_path = File.join(pages_dir, namespace_path, 'the-project') expected_pages_path = File.join(pages_dir, namespace_path, 'the-project')
expect(File.directory?(expected_repository_path)).to be_truthy expect(File.directory?(expected_repository_path)).to be_truthy
expect(File.directory?(expected_upload_path)).to be_truthy expect(File.directory?(expected_upload_path)).to be_truthy
expect(File.directory?(expected_pages_path)).to be_truthy expect(File.directory?(expected_pages_path)).to be(with_pages)
end end
before do before do
...@@ -412,7 +412,7 @@ RSpec.describe Namespace do ...@@ -412,7 +412,7 @@ RSpec.describe Namespace do
FileUtils.remove_entry(File.join(TestEnv.repos_path, new_parent.full_path), true) FileUtils.remove_entry(File.join(TestEnv.repos_path, new_parent.full_path), true)
FileUtils.remove_entry(File.join(TestEnv.repos_path, child.full_path), true) FileUtils.remove_entry(File.join(TestEnv.repos_path, child.full_path), true)
FileUtils.remove_entry(File.join(uploads_dir, project.full_path), true) FileUtils.remove_entry(File.join(uploads_dir, project.full_path), true)
FileUtils.remove_entry(File.join(pages_dir, project.full_path), true) FileUtils.remove_entry(pages_dir, true)
end end
context 'renaming child' do context 'renaming child' do
...@@ -426,10 +426,22 @@ RSpec.describe Namespace do ...@@ -426,10 +426,22 @@ RSpec.describe Namespace do
end end
end end
it 'correctly moves the repository, uploads and pages', :sidekiq_inline do context 'when no projects have pages deployed' do
child.update!(path: 'renamed') it 'moves the repository and uploads', :sidekiq_inline do
project.pages_metadatum.update!(deployed: false)
child.update!(path: 'renamed')
expect_project_directories_at('parent/renamed', with_pages: false)
end
end
context 'when the project has pages deployed' do
it 'correctly moves the repository, uploads and pages', :sidekiq_inline do
project.pages_metadatum.update!(deployed: true)
child.update!(path: 'renamed')
expect_project_directories_at('parent/renamed') expect_project_directories_at('parent/renamed')
end
end end
end end
...@@ -444,10 +456,22 @@ RSpec.describe Namespace do ...@@ -444,10 +456,22 @@ RSpec.describe Namespace do
end end
end end
it 'correctly moves the repository, uploads and pages', :sidekiq_inline do context 'when no projects have pages deployed' do
parent.update!(path: 'renamed') it 'moves the repository and uploads', :sidekiq_inline do
project.pages_metadatum.update!(deployed: false)
parent.update!(path: 'renamed')
expect_project_directories_at('renamed/child', with_pages: false)
end
end
context 'when the project has pages deployed' do
it 'correctly moves the repository, uploads and pages', :sidekiq_inline do
project.pages_metadatum.update!(deployed: true)
parent.update!(path: 'renamed')
expect_project_directories_at('renamed/child') expect_project_directories_at('renamed/child')
end
end end
end end
...@@ -462,10 +486,22 @@ RSpec.describe Namespace do ...@@ -462,10 +486,22 @@ RSpec.describe Namespace do
end end
end end
it 'correctly moves the repository, uploads and pages', :sidekiq_inline do context 'when no projects have pages deployed' do
child.update!(parent: new_parent) it 'moves the repository and uploads', :sidekiq_inline do
project.pages_metadatum.update!(deployed: false)
child.update!(parent: new_parent)
expect_project_directories_at('new_parent/child', with_pages: false)
end
end
context 'when the project has pages deployed' do
it 'correctly moves the repository, uploads and pages', :sidekiq_inline do
project.pages_metadatum.update!(deployed: true)
child.update!(parent: new_parent)
expect_project_directories_at('new_parent/child') expect_project_directories_at('new_parent/child')
end
end end
end end
...@@ -480,10 +516,22 @@ RSpec.describe Namespace do ...@@ -480,10 +516,22 @@ RSpec.describe Namespace do
end end
end end
it 'correctly moves the repository, uploads and pages', :sidekiq_inline do context 'when no projects have pages deployed' do
child.update!(parent: nil) it 'moves the repository and uploads', :sidekiq_inline do
project.pages_metadatum.update!(deployed: false)
child.update!(parent: nil)
expect_project_directories_at('child', with_pages: false)
end
end
context 'when the project has pages deployed' do
it 'correctly moves the repository, uploads and pages', :sidekiq_inline do
project.pages_metadatum.update!(deployed: true)
child.update!(parent: nil)
expect_project_directories_at('child') expect_project_directories_at('child')
end
end end
end end
...@@ -498,10 +546,22 @@ RSpec.describe Namespace do ...@@ -498,10 +546,22 @@ RSpec.describe Namespace do
end end
end end
it 'correctly moves the repository, uploads and pages', :sidekiq_inline do context 'when no projects have pages deployed' do
parent.update!(parent: new_parent) it 'moves the repository and uploads', :sidekiq_inline do
project.pages_metadatum.update!(deployed: false)
parent.update!(parent: new_parent)
expect_project_directories_at('new_parent/parent/child') expect_project_directories_at('new_parent/parent/child', with_pages: false)
end
end
context 'when the project has pages deployed' do
it 'correctly moves the repository, uploads and pages', :sidekiq_inline do
project.pages_metadatum.update!(deployed: true)
parent.update!(parent: new_parent)
expect_project_directories_at('new_parent/parent/child')
end
end end
end end
end end
...@@ -1174,6 +1234,27 @@ RSpec.describe Namespace do ...@@ -1174,6 +1234,27 @@ RSpec.describe Namespace do
end end
end end
describe '#any_project_with_pages_deployed?' do
it 'returns true if any project nested under the group has pages deployed' do
parent_1 = create(:group) # Three projects, one with pages
child_1_1 = create(:group, parent: parent_1) # Two projects, one with pages
child_1_2 = create(:group, parent: parent_1) # One project, no pages
parent_2 = create(:group) # No projects
create(:project, group: child_1_1).tap do |project|
project.pages_metadatum.update!(deployed: true)
end
create(:project, group: child_1_1)
create(:project, group: child_1_2)
expect(parent_1.any_project_with_pages_deployed?).to be(true)
expect(child_1_1.any_project_with_pages_deployed?).to be(true)
expect(child_1_2.any_project_with_pages_deployed?).to be(false)
expect(parent_2.any_project_with_pages_deployed?).to be(false)
end
end
describe '#has_parent?' do describe '#has_parent?' do
it 'returns true when the group has a parent' do it 'returns true when the group has a parent' do
group = create(:group, :nested) group = create(:group, :nested)
......
...@@ -75,10 +75,25 @@ RSpec.describe Projects::AfterRenameService do ...@@ -75,10 +75,25 @@ RSpec.describe Projects::AfterRenameService do
end end
end end
it 'schedules a move of the pages directory' do context 'when the project has pages deployed' do
expect(PagesTransferWorker).to receive(:perform_async).with('rename_project', anything) it 'schedules a move of the pages directory' do
allow(project).to receive(:pages_deployed?).and_return(true)
service_execute expect(PagesTransferWorker).to receive(:perform_async).with('rename_project', anything)
service_execute
end
end
context 'when the project does not have pages deployed' do
it 'does nothing with the pages directory' do
allow(project).to receive(:pages_deployed?).and_return(false)
expect(PagesTransferWorker).not_to receive(:perform_async)
expect(Gitlab::PagesTransfer).not_to receive(:new)
service_execute
end
end end
end end
...@@ -180,10 +195,25 @@ RSpec.describe Projects::AfterRenameService do ...@@ -180,10 +195,25 @@ RSpec.describe Projects::AfterRenameService do
end end
end end
it 'schedules a move of the pages directory' do context 'when the project has pages deployed' do
expect(PagesTransferWorker).to receive(:perform_async).with('rename_project', anything) it 'schedules a move of the pages directory' do
allow(project).to receive(:pages_deployed?).and_return(true)
service_execute expect(PagesTransferWorker).to receive(:perform_async).with('rename_project', anything)
service_execute
end
end
context 'when the project does not have pages deployed' do
it 'does nothing with the pages directory' do
allow(project).to receive(:pages_deployed?).and_return(false)
expect(PagesTransferWorker).not_to receive(:perform_async)
expect(Gitlab::PagesTransfer).not_to receive(:new)
service_execute
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