Commit 20490e9b authored by Markus Koller's avatar Markus Koller

Merge branch '218991-fj-add-snippet-update-repository-worker' into 'master'

Add snippet repository storage workers

See merge request gitlab-org/gitlab!49227
parents 94c9ea1b 1bc32d09
...@@ -12,7 +12,11 @@ class SnippetRepositoryStorageMove < ApplicationRecord ...@@ -12,7 +12,11 @@ class SnippetRepositoryStorageMove < ApplicationRecord
override :schedule_repository_storage_update_worker override :schedule_repository_storage_update_worker
def schedule_repository_storage_update_worker def schedule_repository_storage_update_worker
# TODO https://gitlab.com/gitlab-org/gitlab/-/issues/218991 SnippetUpdateRepositoryStorageWorker.perform_async(
snippet_id,
destination_storage_name,
id
)
end end
private private
......
...@@ -2103,6 +2103,14 @@ ...@@ -2103,6 +2103,14 @@
:weight: 1 :weight: 1
:idempotent: :idempotent:
:tags: [] :tags: []
- :name: snippet_update_repository_storage
:feature_category: :gitaly
:has_external_dependencies:
:urgency: :throttled
:resource_boundary: :unknown
:weight: 1
:idempotent: true
:tags: []
- :name: system_hook_push - :name: system_hook_push
:feature_category: :source_code_management :feature_category: :source_code_management
:has_external_dependencies: :has_external_dependencies:
......
# frozen_string_literal: true
module UpdateRepositoryStorageWorker
extend ActiveSupport::Concern
include ApplicationWorker
included do
idempotent!
feature_category :gitaly
urgency :throttled
end
def perform(container_id, new_repository_storage_key, repository_storage_move_id = nil)
repository_storage_move =
if repository_storage_move_id
find_repository_storage_move(repository_storage_move_id)
else
# maintain compatibility with workers queued before release
container = find_container(container_id)
container.repository_storage_moves.create!(
source_storage_name: container.repository_storage,
destination_storage_name: new_repository_storage_key
)
end
update_repository_storage(repository_storage_move)
end
private
def find_repository_storage_move(repository_storage_move_id)
raise NotImplementedError
end
def find_container(container_id)
raise NotImplementedError
end
def update_repository_storage(repository_storage_move)
raise NotImplementedError
end
end
# frozen_string_literal: true # frozen_string_literal: true
class ProjectUpdateRepositoryStorageWorker class ProjectUpdateRepositoryStorageWorker # rubocop:disable Scalability/IdempotentWorker
include ApplicationWorker extend ::Gitlab::Utils::Override
include UpdateRepositoryStorageWorker
idempotent! private
feature_category :gitaly
urgency :throttled
def perform(project_id, new_repository_storage_key, repository_storage_move_id = nil) override :find_repository_storage_move
repository_storage_move = def find_repository_storage_move(repository_storage_move_id)
if repository_storage_move_id
ProjectRepositoryStorageMove.find(repository_storage_move_id) ProjectRepositoryStorageMove.find(repository_storage_move_id)
else
# maintain compatibility with workers queued before release
project = Project.find(project_id)
project.repository_storage_moves.create!(
source_storage_name: project.repository_storage,
destination_storage_name: new_repository_storage_key
)
end end
override :find_container
def find_container(container_id)
Project.find(container_id)
end
override :update_repository_storage
def update_repository_storage(repository_storage_move)
::Projects::UpdateRepositoryStorageService.new(repository_storage_move).execute ::Projects::UpdateRepositoryStorageService.new(repository_storage_move).execute
end end
end end
# frozen_string_literal: true
class SnippetUpdateRepositoryStorageWorker # rubocop:disable Scalability/IdempotentWorker
extend ::Gitlab::Utils::Override
include UpdateRepositoryStorageWorker
private
override :find_repository_storage_move
def find_repository_storage_move(repository_storage_move_id)
SnippetRepositoryStorageMove.find(repository_storage_move_id)
end
override :find_container
def find_container(container_id)
Snippet.find(container_id)
end
override :update_repository_storage
def update_repository_storage(repository_storage_move)
::Snippets::UpdateRepositoryStorageService.new(repository_storage_move).execute
end
end
...@@ -320,6 +320,8 @@ ...@@ -320,6 +320,8 @@
- 1 - 1
- - set_user_status_based_on_user_cap_setting - - set_user_status_based_on_user_cap_setting
- 1 - 1
- - snippet_update_repository_storage
- 1
- - status_page_publish - - status_page_publish
- 1 - 1
- - sync_seat_link_request - - sync_seat_link_request
......
...@@ -8,6 +8,6 @@ RSpec.describe SnippetRepositoryStorageMove, type: :model do ...@@ -8,6 +8,6 @@ RSpec.describe SnippetRepositoryStorageMove, type: :model do
let(:repository_storage_factory_key) { :snippet_repository_storage_move } let(:repository_storage_factory_key) { :snippet_repository_storage_move }
let(:error_key) { :snippet } let(:error_key) { :snippet }
let(:repository_storage_worker) { nil } # TODO set to SnippetUpdateRepositoryStorageWorker after https://gitlab.com/gitlab-org/gitlab/-/issues/218991 is implemented let(:repository_storage_worker) { SnippetUpdateRepositoryStorageWorker }
end end
end end
...@@ -63,7 +63,6 @@ RSpec.shared_examples 'handles repository moves' do ...@@ -63,7 +63,6 @@ RSpec.shared_examples 'handles repository moves' do
context 'and transits to scheduled' do context 'and transits to scheduled' do
it 'triggers the corresponding repository storage worker' do it 'triggers the corresponding repository storage worker' do
skip unless repository_storage_worker # TODO remove after https://gitlab.com/gitlab-org/gitlab/-/issues/218991 is implemented
expect(repository_storage_worker).to receive(:perform_async).with(container.id, 'test_second_storage', storage_move.id) expect(repository_storage_worker).to receive(:perform_async).with(container.id, 'test_second_storage', storage_move.id)
storage_move.schedule! storage_move.schedule!
...@@ -72,8 +71,7 @@ RSpec.shared_examples 'handles repository moves' do ...@@ -72,8 +71,7 @@ RSpec.shared_examples 'handles repository moves' do
end end
context 'when the transition fails' do context 'when the transition fails' do
it 'does not trigger ProjectUpdateRepositoryStorageWorker and adds an error' do it 'does not trigger the corresponding repository storage worker and adds an error' do
skip unless repository_storage_worker # TODO remove after https://gitlab.com/gitlab-org/gitlab/-/issues/218991 is implemented
allow(storage_move.container).to receive(:set_repository_read_only!).and_raise(StandardError, 'foobar') allow(storage_move.container).to receive(:set_repository_read_only!).and_raise(StandardError, 'foobar')
expect(repository_storage_worker).not_to receive(:perform_async) expect(repository_storage_worker).not_to receive(:perform_async)
......
# frozen_string_literal: true
RSpec.shared_examples 'an update storage move worker' do
describe '#perform' do
let(:service) { double(:update_repository_storage_service) }
before do
allow(Gitlab.config.repositories.storages).to receive(:keys).and_return(%w[default test_second_storage])
end
context 'without repository storage move' do
it 'calls the update repository storage service' do
expect(service_klass).to receive(:new).and_return(service)
expect(service).to receive(:execute)
expect do
subject.perform(container.id, 'test_second_storage')
end.to change(repository_storage_move_klass, :count).by(1)
storage_move = container.repository_storage_moves.last
expect(storage_move).to have_attributes(
source_storage_name: 'default',
destination_storage_name: 'test_second_storage'
)
end
end
context 'with repository storage move' do
it 'calls the update repository storage service' do
expect(service_klass).to receive(:new).and_return(service)
expect(service).to receive(:execute)
expect do
subject.perform(nil, nil, repository_storage_move.id)
end.not_to change(repository_storage_move_klass, :count)
end
end
end
end
...@@ -3,45 +3,13 @@ ...@@ -3,45 +3,13 @@
require 'spec_helper' require 'spec_helper'
RSpec.describe ProjectUpdateRepositoryStorageWorker do RSpec.describe ProjectUpdateRepositoryStorageWorker do
let(:project) { create(:project, :repository) }
subject { described_class.new } subject { described_class.new }
describe "#perform" do it_behaves_like 'an update storage move worker' do
let(:service) { double(:update_repository_storage_service) } let_it_be_with_refind(:container) { create(:project, :repository) }
let_it_be(:repository_storage_move) { create(:project_repository_storage_move) }
before do
allow(Gitlab.config.repositories.storages).to receive(:keys).and_return(%w[default test_second_storage])
end
context 'without repository storage move' do
it "calls the update repository storage service" do
expect(Projects::UpdateRepositoryStorageService).to receive(:new).and_return(service)
expect(service).to receive(:execute)
expect do
subject.perform(project.id, 'test_second_storage')
end.to change(ProjectRepositoryStorageMove, :count).by(1)
storage_move = project.repository_storage_moves.last
expect(storage_move).to have_attributes(
source_storage_name: "default",
destination_storage_name: "test_second_storage"
)
end
end
context 'with repository storage move' do
let!(:repository_storage_move) { create(:project_repository_storage_move) }
it "calls the update repository storage service" do let(:service_klass) { Projects::UpdateRepositoryStorageService }
expect(Projects::UpdateRepositoryStorageService).to receive(:new).and_return(service) let(:repository_storage_move_klass) { ProjectRepositoryStorageMove }
expect(service).to receive(:execute)
expect do
subject.perform(nil, nil, repository_storage_move.id)
end.not_to change(ProjectRepositoryStorageMove, :count)
end
end
end end
end end
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe SnippetUpdateRepositoryStorageWorker do
subject { described_class.new }
it_behaves_like 'an update storage move worker' do
let_it_be_with_refind(:container) { create(:snippet, :repository) }
let_it_be(:repository_storage_move) { create(:snippet_repository_storage_move) }
let(:service_klass) { Snippets::UpdateRepositoryStorageService }
let(:repository_storage_move_klass) { SnippetRepositoryStorageMove }
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