Commit 0d5e214f authored by Alex Kalderimis's avatar Alex Kalderimis

Merge branch '325496-idempotent-project-import-schedule-worker' into 'master'

Mark ProjectImportScheduleWorker as idempotent

See merge request gitlab-org/gitlab!59391
parents db16f859 75166431
......@@ -884,12 +884,12 @@
:idempotent:
:tags: []
- :name: project_import_schedule
:feature_category: :importers
:feature_category: :source_code_management
:has_external_dependencies:
:urgency: :low
:resource_boundary: :unknown
:weight: 1
:idempotent:
:idempotent: true
:tags: []
- :name: project_template_export
:feature_category: :templates
......
# frozen_string_literal: true
class ProjectImportScheduleWorker # rubocop:disable Scalability/IdempotentWorker
class ProjectImportScheduleWorker
ImportStateNotFound = Class.new(StandardError)
include ApplicationWorker
prepend WaitableWorker
feature_category :importers
idempotent!
feature_category :source_code_management
sidekiq_options retry: false
loggable_arguments 1 # For the job waiter key
......
......@@ -3,37 +3,45 @@
require 'spec_helper'
RSpec.describe ProjectImportScheduleWorker do
let!(:project) { create(:project) }
describe '#perform' do
it 'does nothing if the database is read-only' do
project = create(:project)
it_behaves_like 'an idempotent worker' do
let!(:import_state) { create(:import_state, :none, project: project) }
allow(Gitlab::Database).to receive(:read_only?).and_return(true)
expect(ProjectImportState).not_to receive(:project_id).with(project_id: project.id)
let(:job_args) { [project.id] }
subject.perform(project.id)
end
before do
allow(Project).to receive(:find_by_id).with(project.id).and_return(project)
allow(project).to receive(:add_import_job)
end
it 'schedules an import for a project' do
import_state = create(:import_state)
it 'does nothing if the database is read-only' do
allow(Gitlab::Database).to receive(:read_only?).and_return(true)
expect(ProjectImportState).not_to receive(:project_id).with(project_id: project.id)
allow_next_instance_of(EE::Project) do |instance|
allow(instance).to receive(:add_import_job).and_return(nil)
subject
end
expect do
subject.perform(import_state.project_id)
end.to change { import_state.reload.status }.from("none").to("scheduled")
it 'schedules an import for a project' do
expect(project).to receive(:add_import_job)
expect(import_state).to be_none
subject
expect(import_state).to be_scheduled
end
end
context 'when project is not found' do
context 'project is not found' do
it 'raises ImportStateNotFound' do
expect { subject.perform(-1) }.to raise_error(described_class::ImportStateNotFound)
end
end
context 'when project does not have import state' do
context 'project does not have import state' do
it 'raises ImportStateNotFound' do
project = create(:project)
expect(project.import_state).to be_nil
expect { subject.perform(project.id) }.to raise_error(described_class::ImportStateNotFound)
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