Commit 308971ad authored by Reuben Pereira's avatar Reuben Pereira Committed by Bob Van Landuyt

Sidekiq worker for SelfMonitoring CreateService

* Create a sidekiq worker to execute
SelfMonitoring::Project::CreateService.execute.

* Also remove the SelfMonitoring::Project::CreateService#execute!
method since it is not being used.
parent fc2fc73b
......@@ -187,3 +187,4 @@
- project_daily_statistics
- create_evidence
- 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 @@
- [chaos, 2]
- [create_evidence, 2]
- [group_export, 1]
- [self_monitoring_project_create, 2]
# EE-specific queues
- [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
module Project
class CreateService < ::BaseService
include Stepable
STEPS_ALLOWED_TO_FAIL = [
:validate_application_settings, :validate_project_created, :validate_admins
].freeze
include SelfMonitoring::Helpers
VISIBILITY_LEVEL = Gitlab::VisibilityLevel::INTERNAL
PROJECT_NAME = 'GitLab Instance Administration'
steps :validate_application_settings,
:validate_project_created,
:validate_admins,
:create_group,
:create_project,
:save_project_id,
:add_group_members,
:add_prometheus_manual_configuration
:add_prometheus_manual_configuration,
:track_event
def initialize
super(nil)
end
def execute!
result = 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
def execute
execute_steps
end
private
......@@ -49,13 +37,6 @@ module Gitlab
error(_('No application_settings found'))
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)
unless instance_admins.any?
log_error('No active admin user found')
......@@ -68,7 +49,7 @@ module Gitlab
def create_group(result)
if project_created?
log_info(_('Instance administrators group already exists'))
result[:group] = application_settings.instance_administration_project.owner
result[:group] = self_monitoring_project.owner
return success(result)
end
......@@ -84,7 +65,7 @@ module Gitlab
def create_project(result)
if project_created?
log_info('Instance administration project already exists')
result[:project] = application_settings.instance_administration_project
result[:project] = self_monitoring_project
return success(result)
end
......@@ -99,7 +80,7 @@ module Gitlab
end
def save_project_id(result)
return success if project_created?
return success(result) if project_created?
response = application_settings.update(
instance_administration_project_id: result[:project].id
......@@ -140,12 +121,10 @@ module Gitlab
success(result)
end
def application_settings
@application_settings ||= ApplicationSetting.current_without_cache
end
def track_event(result)
::Gitlab::Tracking.event("self_monitoring", "project_created")
def project_created?
application_settings.instance_administration_project.present?
success(result)
end
def parse_url(uri_string)
......
......@@ -13836,9 +13836,6 @@ msgstr ""
msgid "Project access must be granted explicitly to each user."
msgstr ""
msgid "Project already created"
msgstr ""
msgid "Project already deleted"
msgstr ""
......
......@@ -4,7 +4,7 @@ require 'spec_helper'
describe Gitlab::DatabaseImporters::SelfMonitoring::Project::CreateService do
describe '#execute' do
let(:result) { subject.execute! }
let(:result) { subject.execute }
let(:prometheus_settings) do
{
......@@ -18,10 +18,12 @@ describe Gitlab::DatabaseImporters::SelfMonitoring::Project::CreateService do
end
context 'without application_settings' do
it 'does not fail' do
it 'returns error' do
expect(subject).to receive(:log_error).and_call_original
expect(result).to eq(
status: :success
status: :error,
message: 'No application_settings found',
last_step: :validate_application_settings
)
expect(Project.count).to eq(0)
......@@ -36,10 +38,12 @@ describe Gitlab::DatabaseImporters::SelfMonitoring::Project::CreateService do
allow(ApplicationSetting).to receive(:current_without_cache) { application_setting }
end
it 'does not fail' do
it 'returns error' do
expect(subject).to receive(:log_error).and_call_original
expect(result).to eq(
status: :success
status: :error,
message: 'No active admin user found',
last_step: :validate_admins
)
expect(Project.count).to eq(0)
......@@ -47,7 +51,7 @@ describe Gitlab::DatabaseImporters::SelfMonitoring::Project::CreateService do
end
end
context 'with admin users' do
context 'with application settings and admin users' do
let(:project) { result[:project] }
let(:group) { result[:group] }
let(:application_setting) { Gitlab::CurrentSettings.current_application_settings }
......@@ -73,6 +77,13 @@ describe Gitlab::DatabaseImporters::SelfMonitoring::Project::CreateService do
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
expect(result[:status]).to eq(:success)
expect(group).to be_persisted
......@@ -132,7 +143,11 @@ describe Gitlab::DatabaseImporters::SelfMonitoring::Project::CreateService do
it 'returns error when saving project ID fails' do
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
context 'when project already exists' do
......@@ -149,9 +164,8 @@ describe Gitlab::DatabaseImporters::SelfMonitoring::Project::CreateService do
application_setting.instance_administration_project_id = existing_project.id
end
it 'does not fail' do
expect(subject).to receive(:log_error).and_call_original
expect(result[:status]).to eq(:success)
it 'returns success' do
expect(result).to include(status: :success)
expect(Project.count).to eq(1)
expect(Group.count).to eq(1)
......@@ -250,7 +264,11 @@ describe Gitlab::DatabaseImporters::SelfMonitoring::Project::CreateService do
it 'returns error' do
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
......@@ -261,7 +279,11 @@ describe Gitlab::DatabaseImporters::SelfMonitoring::Project::CreateService do
it 'returns error' do
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
......@@ -275,15 +297,13 @@ describe Gitlab::DatabaseImporters::SelfMonitoring::Project::CreateService do
it 'returns error' do
expect(subject).to receive(:log_error).and_call_original
expect { result }.to raise_error(StandardError, 'Could not save prometheus manual configuration')
end
expect(result).to eq(
status: :error,
message: 'Could not save prometheus manual configuration',
last_step: :add_prometheus_manual_configuration
)
end
end
it "tracks successful install" do
expect(Gitlab::Tracking).to receive(:event).with("self_monitoring", "project_created")
result
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