This instance will be upgraded to Heptapod 0.31.0 (final) on 2022-05-24 at 14:00 UTC+2 (a few minutes of down time)

Commit 6ecf16b8 authored by James Lopez's avatar James Lopez
Browse files

refactor code based on feedback

parent f2242d7bf315
......@@ -16,7 +16,7 @@ def edit
def update
if service.update_attributes(service_params[:service])
PropagateProjectServiceWorker.perform_async(service.id) if service.active?
PropagateServiceTemplateWorker.perform_async(service.id) if service.active?
redirect_to admin_application_settings_services_path,
notice: 'Application settings saved successfully'
......
module Projects
class PropagateService
class PropagateServiceTemplate
BATCH_SIZE = 100
def self.propagate(*args)
......@@ -36,9 +36,7 @@ def bulk_create_from_template(batch)
end
Project.transaction do
Gitlab::SQL::BulkInsert.new(service_hash.keys + ['project_id'],
service_list,
'services').execute
bulk_insert_services(service_hash.keys + ['project_id'], service_list)
run_callbacks(batch)
end
end
......@@ -54,11 +52,22 @@ def project_ids_batch
WHERE services.project_id = projects.id
AND services.type = '#{@template.type}'
)
AND projects.pending_delete = false
AND projects.archived = false
LIMIT #{BATCH_SIZE}
SQL
)
end
def bulk_insert_services(columns, values_array)
ActiveRecord::Base.connection.execute(
<<-SQL.strip_heredoc
INSERT INTO services (#{columns.join(', ')})
VALUES #{values_array.map { |tuple| "(#{tuple.join(', ')})" }.join(', ')}
SQL
)
end
def service_hash
@service_hash ||=
begin
......@@ -84,11 +93,11 @@ def run_callbacks(batch)
end
def active_external_issue_tracker?
@template['category'] == 'issue_tracker' && @template['active'] && !@template['default']
@template.category == :issue_tracker && !@template.default
end
def active_external_wiki?
@template['type'] == 'ExternalWikiService' && @template['active']
@template.type == 'ExternalWikiService'
end
end
end
# Worker for updating any project specific caches.
class PropagateProjectServiceWorker
class PropagateServiceTemplateWorker
include Sidekiq::Worker
include DedicatedSidekiqQueue
......@@ -10,14 +10,14 @@ class PropagateProjectServiceWorker
def perform(template_id)
return unless try_obtain_lease_for(template_id)
Projects::PropagateService.propagate(Service.find_by(id: template_id))
Projects::PropagateServiceTemplate.propagate(Service.find_by(id: template_id))
end
private
def try_obtain_lease_for(template_id)
Gitlab::ExclusiveLease.
new("propagate_project_service_worker:#{template_id}", timeout: LEASE_TIMEOUT).
new("propagate_service_template_worker:#{template_id}", timeout: LEASE_TIMEOUT).
try_obtain
end
end
......@@ -53,4 +53,4 @@
- [pages, 1]
- [system_hook_push, 1]
- [update_user_activity, 1]
- [propagate_project_service, 1]
- [propagate_service_template, 1]
module Gitlab
module SQL
# Class for building SQL bulk inserts
class BulkInsert
def initialize(columns, values_array, table)
@columns = columns
@values_array = values_array
@table = table
end
def execute
ActiveRecord::Base.connection.execute(
<<-SQL.strip_heredoc
INSERT INTO #{@table} (#{@columns.join(', ')})
VALUES #{@values_array.map { |tuple| "(#{tuple.join(', ')})" }.join(', ')}
SQL
)
end
end
end
end
......@@ -39,16 +39,16 @@
)
end
it 'updates the service params successfully and calls the propagation worker' do
expect(PropagateProjectServiceWorker).to receive(:perform_async).with(service.id)
it 'calls the propagation worker when service is active' do
expect(PropagateServiceTemplateWorker).to receive(:perform_async).with(service.id)
put :update, id: service.id, service: { active: true }
expect(response).to have_http_status(302)
end
it 'updates the service params successfully' do
expect(PropagateProjectServiceWorker).not_to receive(:perform_async)
it 'does not call the propagation worker when service is not active' do
expect(PropagateServiceTemplateWorker).not_to receive(:perform_async)
put :update, id: service.id, service: { properties: {} }
......
require 'spec_helper'
describe Projects::PropagateService, services: true do
describe '.propagate!' do
describe Projects::PropagateServiceTemplate, services: true do
describe '.propagate' do
let!(:service_template) do
PushoverService.create(
template: true,
......@@ -23,9 +23,10 @@
end
it 'creates services for a project that has another service' do
other_service = BambooService.create(
BambooService.create(
template: true,
active: true,
project: project,
properties: {
bamboo_url: 'http://gitlab.com',
username: 'mic',
......@@ -34,8 +35,6 @@
}
)
Service.build_from_template(project.id, other_service).save!
expect { described_class.propagate(service_template) }.
to change { Service.count }.by(1)
end
......@@ -62,7 +61,7 @@
it 'creates the service containing the template attributes' do
described_class.propagate(service_template)
service = Service.find_by(type: service_template.type, template: false)
service = Service.find_by!(type: service_template.type, template: false)
expect(service.properties).to eq(service_template.properties)
end
......@@ -70,7 +69,7 @@
describe 'bulk update' do
it 'creates services for all projects' do
project_total = 5
stub_const 'Projects::PropagateService::BATCH_SIZE', 3
stub_const 'Projects::PropagateServiceTemplate::BATCH_SIZE', 3
project_total.times { create(:empty_project) }
......@@ -81,7 +80,7 @@
describe 'external tracker' do
it 'updates the project external tracker' do
service_template.update(category: 'issue_tracker', default: false)
service_template.update!(category: 'issue_tracker', default: false)
expect { described_class.propagate(service_template) }.
to change { project.reload.has_external_issue_tracker }.to(true)
......@@ -90,7 +89,7 @@
describe 'external wiki' do
it 'updates the project external tracker' do
service_template.update(type: 'ExternalWikiService')
service_template.update!(type: 'ExternalWikiService')
expect { described_class.propagate(service_template) }.
to change { project.reload.has_external_wiki }.to(true)
......
require 'spec_helper'
describe PropagateProjectServiceWorker do
describe PropagateServiceTemplateWorker do
let!(:service_template) do
PushoverService.create(
template: true,
......@@ -21,7 +21,7 @@
describe '#perform' do
it 'calls the propagate service with the template' do
expect(Projects::PropagateService).to receive(:propagate).with(service_template)
expect(Projects::PropagateServiceTemplate).to receive(:propagate).with(service_template)
subject.perform(service_template.id)
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