Commit 2a58cfc3 authored by Bob Van Landuyt's avatar Bob Van Landuyt

Merge branch '32351-create-api' into 'master'

Sidekiq worker to create self monitoring project

See merge request gitlab-org/gitlab!21235
parents fc2fc73b 308971ad
...@@ -187,3 +187,4 @@ ...@@ -187,3 +187,4 @@
- project_daily_statistics - project_daily_statistics
- create_evidence - create_evidence
- group_export - group_export
- self_monitoring_project_create
# frozen_string_literal: true
class SelfMonitoringProjectCreateWorker
include ApplicationWorker
include ExclusiveLeaseGuard
# This worker falls under Self-monitoring with Monitor::APM group. However,
# self-monitoring is not classified as a feature category but rather as
# Other Functionality. Metrics seems to be the closest feature_category for
# this worker.
feature_category :metrics
LEASE_TIMEOUT = 15.minutes.to_i
EXCLUSIVE_LEASE_KEY = 'self_monitoring_service_creation_deletion'
def perform
try_obtain_lease do
Gitlab::DatabaseImporters::SelfMonitoring::Project::CreateService.new.execute
end
end
# @param job_id [String]
# Job ID that is used to construct the cache keys.
# @return [Hash]
# Returns true if the job is enqueued or in progress and false otherwise.
def self.in_progress?(job_id)
Gitlab::SidekiqStatus.job_status(Array.wrap(job_id)).first
end
private
def lease_key
EXCLUSIVE_LEASE_KEY
end
def lease_timeout
LEASE_TIMEOUT
end
end
...@@ -99,6 +99,7 @@ ...@@ -99,6 +99,7 @@
- [chaos, 2] - [chaos, 2]
- [create_evidence, 2] - [create_evidence, 2]
- [group_export, 1] - [group_export, 1]
- [self_monitoring_project_create, 2]
# EE-specific queues # EE-specific queues
- [analytics, 1] - [analytics, 1]
......
# frozen_string_literal: true
module Gitlab
module DatabaseImporters
module SelfMonitoring
module Helpers
def application_settings
@application_settings ||= ApplicationSetting.current_without_cache
end
def project_created?
self_monitoring_project.present?
end
def self_monitoring_project
application_settings.instance_administration_project
end
def self_monitoring_project_id
application_settings.instance_administration_project_id
end
end
end
end
end
...@@ -6,38 +6,26 @@ module Gitlab ...@@ -6,38 +6,26 @@ module Gitlab
module Project module Project
class CreateService < ::BaseService class CreateService < ::BaseService
include Stepable include Stepable
include SelfMonitoring::Helpers
STEPS_ALLOWED_TO_FAIL = [
:validate_application_settings, :validate_project_created, :validate_admins
].freeze
VISIBILITY_LEVEL = Gitlab::VisibilityLevel::INTERNAL VISIBILITY_LEVEL = Gitlab::VisibilityLevel::INTERNAL
PROJECT_NAME = 'GitLab Instance Administration' PROJECT_NAME = 'GitLab Instance Administration'
steps :validate_application_settings, steps :validate_application_settings,
:validate_project_created,
:validate_admins, :validate_admins,
:create_group, :create_group,
:create_project, :create_project,
:save_project_id, :save_project_id,
:add_group_members, :add_group_members,
:add_prometheus_manual_configuration :add_prometheus_manual_configuration,
:track_event
def initialize def initialize
super(nil) super(nil)
end end
def execute! def execute
result = execute_steps execute_steps
if result[:status] == :success
::Gitlab::Tracking.event("self_monitoring", "project_created")
result
elsif STEPS_ALLOWED_TO_FAIL.include?(result[:last_step])
::Gitlab::Tracking.event("self_monitoring", "project_created")
success
else
raise StandardError, result[:message]
end
end end
private private
...@@ -49,13 +37,6 @@ module Gitlab ...@@ -49,13 +37,6 @@ module Gitlab
error(_('No application_settings found')) error(_('No application_settings found'))
end end
def validate_project_created(result)
return success(result) unless project_created?
log_error('Project already created')
error(_('Project already created'))
end
def validate_admins(result) def validate_admins(result)
unless instance_admins.any? unless instance_admins.any?
log_error('No active admin user found') log_error('No active admin user found')
...@@ -68,7 +49,7 @@ module Gitlab ...@@ -68,7 +49,7 @@ module Gitlab
def create_group(result) def create_group(result)
if project_created? if project_created?
log_info(_('Instance administrators group already exists')) log_info(_('Instance administrators group already exists'))
result[:group] = application_settings.instance_administration_project.owner result[:group] = self_monitoring_project.owner
return success(result) return success(result)
end end
...@@ -84,7 +65,7 @@ module Gitlab ...@@ -84,7 +65,7 @@ module Gitlab
def create_project(result) def create_project(result)
if project_created? if project_created?
log_info('Instance administration project already exists') log_info('Instance administration project already exists')
result[:project] = application_settings.instance_administration_project result[:project] = self_monitoring_project
return success(result) return success(result)
end end
...@@ -99,7 +80,7 @@ module Gitlab ...@@ -99,7 +80,7 @@ module Gitlab
end end
def save_project_id(result) def save_project_id(result)
return success if project_created? return success(result) if project_created?
response = application_settings.update( response = application_settings.update(
instance_administration_project_id: result[:project].id instance_administration_project_id: result[:project].id
...@@ -140,12 +121,10 @@ module Gitlab ...@@ -140,12 +121,10 @@ module Gitlab
success(result) success(result)
end end
def application_settings def track_event(result)
@application_settings ||= ApplicationSetting.current_without_cache ::Gitlab::Tracking.event("self_monitoring", "project_created")
end
def project_created? success(result)
application_settings.instance_administration_project.present?
end end
def parse_url(uri_string) def parse_url(uri_string)
......
...@@ -13836,9 +13836,6 @@ msgstr "" ...@@ -13836,9 +13836,6 @@ msgstr ""
msgid "Project access must be granted explicitly to each user." msgid "Project access must be granted explicitly to each user."
msgstr "" msgstr ""
msgid "Project already created"
msgstr ""
msgid "Project already deleted" msgid "Project already deleted"
msgstr "" msgstr ""
......
...@@ -4,7 +4,7 @@ require 'spec_helper' ...@@ -4,7 +4,7 @@ require 'spec_helper'
describe Gitlab::DatabaseImporters::SelfMonitoring::Project::CreateService do describe Gitlab::DatabaseImporters::SelfMonitoring::Project::CreateService do
describe '#execute' do describe '#execute' do
let(:result) { subject.execute! } let(:result) { subject.execute }
let(:prometheus_settings) do let(:prometheus_settings) do
{ {
...@@ -18,10 +18,12 @@ describe Gitlab::DatabaseImporters::SelfMonitoring::Project::CreateService do ...@@ -18,10 +18,12 @@ describe Gitlab::DatabaseImporters::SelfMonitoring::Project::CreateService do
end end
context 'without application_settings' do context 'without application_settings' do
it 'does not fail' do it 'returns error' do
expect(subject).to receive(:log_error).and_call_original expect(subject).to receive(:log_error).and_call_original
expect(result).to eq( expect(result).to eq(
status: :success status: :error,
message: 'No application_settings found',
last_step: :validate_application_settings
) )
expect(Project.count).to eq(0) expect(Project.count).to eq(0)
...@@ -36,10 +38,12 @@ describe Gitlab::DatabaseImporters::SelfMonitoring::Project::CreateService do ...@@ -36,10 +38,12 @@ describe Gitlab::DatabaseImporters::SelfMonitoring::Project::CreateService do
allow(ApplicationSetting).to receive(:current_without_cache) { application_setting } allow(ApplicationSetting).to receive(:current_without_cache) { application_setting }
end end
it 'does not fail' do it 'returns error' do
expect(subject).to receive(:log_error).and_call_original expect(subject).to receive(:log_error).and_call_original
expect(result).to eq( expect(result).to eq(
status: :success status: :error,
message: 'No active admin user found',
last_step: :validate_admins
) )
expect(Project.count).to eq(0) expect(Project.count).to eq(0)
...@@ -47,7 +51,7 @@ describe Gitlab::DatabaseImporters::SelfMonitoring::Project::CreateService do ...@@ -47,7 +51,7 @@ describe Gitlab::DatabaseImporters::SelfMonitoring::Project::CreateService do
end end
end end
context 'with admin users' do context 'with application settings and admin users' do
let(:project) { result[:project] } let(:project) { result[:project] }
let(:group) { result[:group] } let(:group) { result[:group] }
let(:application_setting) { Gitlab::CurrentSettings.current_application_settings } let(:application_setting) { Gitlab::CurrentSettings.current_application_settings }
...@@ -73,6 +77,13 @@ describe Gitlab::DatabaseImporters::SelfMonitoring::Project::CreateService do ...@@ -73,6 +77,13 @@ describe Gitlab::DatabaseImporters::SelfMonitoring::Project::CreateService do
it_behaves_like 'has prometheus service', 'http://localhost:9090' it_behaves_like 'has prometheus service', 'http://localhost:9090'
it "tracks successful install" do
expect(::Gitlab::Tracking).to receive(:event)
expect(::Gitlab::Tracking).to receive(:event).with("self_monitoring", "project_created")
result
end
it 'creates group' do it 'creates group' do
expect(result[:status]).to eq(:success) expect(result[:status]).to eq(:success)
expect(group).to be_persisted expect(group).to be_persisted
...@@ -132,7 +143,11 @@ describe Gitlab::DatabaseImporters::SelfMonitoring::Project::CreateService do ...@@ -132,7 +143,11 @@ describe Gitlab::DatabaseImporters::SelfMonitoring::Project::CreateService do
it 'returns error when saving project ID fails' do it 'returns error when saving project ID fails' do
allow(application_setting).to receive(:save) { false } allow(application_setting).to receive(:save) { false }
expect { result }.to raise_error(StandardError, 'Could not save project ID') expect(result).to eq(
status: :error,
message: 'Could not save project ID',
last_step: :save_project_id
)
end end
context 'when project already exists' do context 'when project already exists' do
...@@ -149,9 +164,8 @@ describe Gitlab::DatabaseImporters::SelfMonitoring::Project::CreateService do ...@@ -149,9 +164,8 @@ describe Gitlab::DatabaseImporters::SelfMonitoring::Project::CreateService do
application_setting.instance_administration_project_id = existing_project.id application_setting.instance_administration_project_id = existing_project.id
end end
it 'does not fail' do it 'returns success' do
expect(subject).to receive(:log_error).and_call_original expect(result).to include(status: :success)
expect(result[:status]).to eq(:success)
expect(Project.count).to eq(1) expect(Project.count).to eq(1)
expect(Group.count).to eq(1) expect(Group.count).to eq(1)
...@@ -250,7 +264,11 @@ describe Gitlab::DatabaseImporters::SelfMonitoring::Project::CreateService do ...@@ -250,7 +264,11 @@ describe Gitlab::DatabaseImporters::SelfMonitoring::Project::CreateService do
it 'returns error' do it 'returns error' do
expect(subject).to receive(:log_error).and_call_original expect(subject).to receive(:log_error).and_call_original
expect { result }.to raise_error(StandardError, 'Could not create project') expect(result).to eq(
status: :error,
message: 'Could not create project',
last_step: :create_project
)
end end
end end
...@@ -261,7 +279,11 @@ describe Gitlab::DatabaseImporters::SelfMonitoring::Project::CreateService do ...@@ -261,7 +279,11 @@ describe Gitlab::DatabaseImporters::SelfMonitoring::Project::CreateService do
it 'returns error' do it 'returns error' do
expect(subject).to receive(:log_error).and_call_original expect(subject).to receive(:log_error).and_call_original
expect { result }.to raise_error(StandardError, 'Could not add admins as members') expect(result).to eq(
status: :error,
message: 'Could not add admins as members',
last_step: :add_group_members
)
end end
end end
...@@ -275,15 +297,13 @@ describe Gitlab::DatabaseImporters::SelfMonitoring::Project::CreateService do ...@@ -275,15 +297,13 @@ describe Gitlab::DatabaseImporters::SelfMonitoring::Project::CreateService do
it 'returns error' do it 'returns error' do
expect(subject).to receive(:log_error).and_call_original expect(subject).to receive(:log_error).and_call_original
expect { result }.to raise_error(StandardError, 'Could not save prometheus manual configuration') expect(result).to eq(
status: :error,
message: 'Could not save prometheus manual configuration',
last_step: :add_prometheus_manual_configuration
)
end end
end end
end end
it "tracks successful install" do
expect(Gitlab::Tracking).to receive(:event).with("self_monitoring", "project_created")
result
end
end end
end end
# frozen_string_literal: true
require 'spec_helper'
describe SelfMonitoringProjectCreateWorker do
describe '#perform' do
let(:service_class) { Gitlab::DatabaseImporters::SelfMonitoring::Project::CreateService }
let(:service) { instance_double(service_class) }
before do
allow(service_class).to receive(:new) { service }
end
it 'runs the SelfMonitoring::Project::CreateService' do
expect(service).to receive(:execute)
subject.perform
end
end
describe '.in_progress?', :clean_gitlab_redis_shared_state do
it 'returns in_progress when job is enqueued' do
jid = described_class.perform_async
expect(described_class.in_progress?(jid)).to eq(true)
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