Commit 8eaeb5ef authored by Michael Kozono's avatar Michael Kozono

Merge branch '20759_extract_prometheus_worker' into 'master'

Move prometheus service creation to the background job [RUN ALL RSPEC] [RUN AS-IF-FOSS]

See merge request gitlab-org/gitlab!58119
parents a7905879 30833cd2
......@@ -110,7 +110,12 @@ module Projects
setup_authorizations
current_user.invalidate_personal_projects_count
create_prometheus_service
if Feature.enabled?(:projects_post_creation_worker, current_user, default_enabled: :yaml)
Projects::PostCreationWorker.perform_async(@project.id)
else
create_prometheus_service
end
create_readme if @initialize_with_readme
end
......@@ -193,6 +198,7 @@ module Projects
@project
end
# Deprecated: https://gitlab.com/gitlab-org/gitlab/-/issues/326665
def create_prometheus_service
service = @project.find_or_initialize_service(::PrometheusService.to_param)
......
......@@ -2112,6 +2112,14 @@
:weight: 1
:idempotent:
:tags: []
- :name: projects_post_creation
:feature_category: :source_code_management
:has_external_dependencies:
:urgency: :low
:resource_boundary: :unknown
:weight: 1
:idempotent: true
:tags: []
- :name: projects_schedule_bulk_repository_shard_moves
:feature_category: :gitaly
:has_external_dependencies:
......
# frozen_string_literal: true
module Projects
class PostCreationWorker
include ApplicationWorker
feature_category :source_code_management
idempotent!
def perform(project_id)
project = Project.find_by_id(project_id)
return unless project
create_prometheus_service(project)
end
private
def create_prometheus_service(project)
service = project.find_or_initialize_service(::PrometheusService.to_param)
# If the service has already been inserted in the database, that
# means it came from a template, and there's nothing more to do.
return if service.persisted?
return unless service.prometheus_available?
service.save!
rescue ActiveRecord::RecordInvalid => e
Gitlab::ErrorTracking.track_exception(e, extra: { project_id: project.id })
end
end
end
---
title: Extract creation of prometheus service from Projects::CreateService
merge_request:
author:
type: added
---
name: projects_post_creation_worker
introduced_by_url: https://gitlab.com/gitlab-org/gitlab/-/merge_requests/58119
rollout_issue_url: https://gitlab.com/gitlab-org/gitlab/-/issues/326665
milestone: '13.11'
type: development
group: group::source code
default_enabled: false
......@@ -298,6 +298,8 @@
- 1
- - projects_git_garbage_collect
- 1
- - projects_post_creation
- 1
- - projects_schedule_bulk_repository_shard_moves
- 1
- - projects_update_repository_storage
......
......@@ -673,7 +673,17 @@ RSpec.describe Projects::CreateService, '#execute' do
expect(rugged.config['gitlab.fullpath']).to eq project.full_path
end
it 'triggers PostCreationWorker' do
expect(Projects::PostCreationWorker).to receive(:perform_async).with(a_kind_of(Integer))
create_project(user, opts)
end
context 'when project has access to shared service' do
before do
stub_feature_flags(projects_post_creation_worker: false)
end
context 'Prometheus application is shared via group cluster' do
let(:cluster) { create(:cluster, :group, groups: [group]) }
let(:group) do
......
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Projects::PostCreationWorker do
let_it_be(:user) { create :user }
let(:worker) { described_class.new }
let(:project) { create(:project) }
subject { described_class.new.perform(project.id) }
it_behaves_like 'an idempotent worker' do
let(:job_args) { [project.id] }
describe 'Prometheus service' do
context 'project is nil' do
let(:job_args) { [nil] }
it 'does not create prometheus service' do
expect { subject }.not_to change { Service.count }
end
end
context 'when project has access to shared service' do
context 'Prometheus application is shared via group cluster' do
let(:project) { create(:project, group: group) }
let(:cluster) { create(:cluster, :group, groups: [group]) }
let(:group) do
create(:group).tap do |group|
group.add_owner(user)
end
end
before do
create(:clusters_applications_prometheus, :installed, cluster: cluster)
end
it 'creates PrometheusService record', :aggregate_failures do
subject
service = project.prometheus_service
expect(service.active).to be true
expect(service.manual_configuration?).to be false
expect(service.persisted?).to be true
end
end
context 'Prometheus application is shared via instance cluster' do
let(:cluster) { create(:cluster, :instance) }
before do
create(:clusters_applications_prometheus, :installed, cluster: cluster)
end
it 'creates PrometheusService record', :aggregate_failures do
subject
service = project.prometheus_service
expect(service.active).to be true
expect(service.manual_configuration?).to be false
expect(service.persisted?).to be true
end
it 'cleans invalid record and logs warning', :aggregate_failures do
invalid_service_record = build(:prometheus_service, properties: { api_url: nil, manual_configuration: true }.to_json)
allow_next_found_instance_of(Project) do |instance|
allow(instance).to receive(:build_prometheus_service).and_return(invalid_service_record)
end
expect(Gitlab::ErrorTracking).to receive(:track_exception).with(an_instance_of(ActiveRecord::RecordInvalid), include(extra: { project_id: a_kind_of(Integer) })).twice
subject
expect(project.prometheus_service).to be_nil
end
end
context 'shared Prometheus application is not available' do
it 'does not persist PrometheusService record', :aggregate_failures do
subject
expect(project.prometheus_service).to be_nil
end
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