diff options
-rw-r--r-- | app/workers/all_queues.yml | 9 | ||||
-rw-r--r-- | app/workers/ci/initialize_pipelines_iid_sequence_worker.rb | 19 | ||||
-rw-r--r-- | config/sidekiq_queues.yml | 2 | ||||
-rw-r--r-- | lib/gitlab/event_store.rb | 1 | ||||
-rw-r--r-- | spec/workers/ci/initialize_pipelines_iid_sequence_worker_spec.rb | 38 |
5 files changed, 69 insertions, 0 deletions
diff --git a/app/workers/all_queues.yml b/app/workers/all_queues.yml index 1664add1ac9..60f233b8289 100644 --- a/app/workers/all_queues.yml +++ b/app/workers/all_queues.yml @@ -2541,6 +2541,15 @@ :weight: 1 :idempotent: true :tags: [] +- :name: ci_initialize_pipelines_iid_sequence + :worker_name: Ci::InitializePipelinesIidSequenceWorker + :feature_category: :continuous_integration + :has_external_dependencies: false + :urgency: :low + :resource_boundary: :unknown + :weight: 1 + :idempotent: true + :tags: [] - :name: ci_job_artifacts_expire_project_build_artifacts :worker_name: Ci::JobArtifacts::ExpireProjectBuildArtifactsWorker :feature_category: :build_artifacts diff --git a/app/workers/ci/initialize_pipelines_iid_sequence_worker.rb b/app/workers/ci/initialize_pipelines_iid_sequence_worker.rb new file mode 100644 index 00000000000..95b58814e08 --- /dev/null +++ b/app/workers/ci/initialize_pipelines_iid_sequence_worker.rb @@ -0,0 +1,19 @@ +# frozen_string_literal: true + +module Ci + class InitializePipelinesIidSequenceWorker + include Gitlab::EventStore::Subscriber + + data_consistency :always + feature_category :continuous_integration + idempotent! + + def handle_event(event) + Project.find_by_id(event.data[:project_id]).try do |project| + next if project.internal_ids.ci_pipelines.any? + + ::Ci::Pipeline.track_project_iid!(project, 0) + end + end + end +end diff --git a/config/sidekiq_queues.yml b/config/sidekiq_queues.yml index 260cdeb27d6..d494f8618b1 100644 --- a/config/sidekiq_queues.yml +++ b/config/sidekiq_queues.yml @@ -125,6 +125,8 @@ - 1 - - ci_delete_objects - 1 +- - ci_initialize_pipelines_iid_sequence + - 1 - - ci_job_artifacts_expire_project_build_artifacts - 1 - - ci_llm_generate_config diff --git a/lib/gitlab/event_store.rb b/lib/gitlab/event_store.rb index ce71ee594f2..474aac8073e 100644 --- a/lib/gitlab/event_store.rb +++ b/lib/gitlab/event_store.rb @@ -64,6 +64,7 @@ module Gitlab store.subscribe ::Ml::ExperimentTracking::AssociateMlCandidateToPackageWorker, to: ::Packages::PackageCreatedEvent, if: -> (event) { ::Ml::ExperimentTracking::AssociateMlCandidateToPackageWorker.handles_event?(event) } + store.subscribe ::Ci::InitializePipelinesIidSequenceWorker, to: ::Projects::ProjectCreatedEvent end private_class_method :configure! end diff --git a/spec/workers/ci/initialize_pipelines_iid_sequence_worker_spec.rb b/spec/workers/ci/initialize_pipelines_iid_sequence_worker_spec.rb new file mode 100644 index 00000000000..fcf57e21ac9 --- /dev/null +++ b/spec/workers/ci/initialize_pipelines_iid_sequence_worker_spec.rb @@ -0,0 +1,38 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe Ci::InitializePipelinesIidSequenceWorker, feature_category: :continuous_integration do + let_it_be_with_refind(:project) { create(:project) } + + let(:project_created_event) do + Projects::ProjectCreatedEvent.new( + data: { + project_id: project.id, + namespace_id: project.namespace_id, + root_namespace_id: project.root_namespace.id + }) + end + + it_behaves_like 'subscribes to event' do + let(:event) { project_created_event } + end + + it 'creates an internal_ids sequence for ci_pipelines' do + consume_event(subscriber: described_class, event: project_created_event) + + expect(project.internal_ids.ci_pipelines).to be_any + expect(project.internal_ids.ci_pipelines).to all be_persisted + end + + context 'when the internal_ids sequence is already initialized' do + before do + create_list(:ci_pipeline, 2, project: project) + end + + it 'does not reset the sequence' do + expect { consume_event(subscriber: described_class, event: project_created_event) } + .not_to change { project.internal_ids.ci_pipelines.pluck(:last_value) } + end + end +end |