Skip to content
Snippets Groups Projects
Commit 6ecf16b8 authored by James Lopez's avatar James Lopez
Browse files

refactor code based on feedback

parent ce418036
No related branches found
No related tags found
No related merge requests found
Loading
Loading
@@ -16,7 +16,7 @@ class Admin::ServicesController < Admin::ApplicationController
 
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'
Loading
Loading
module Projects
class PropagateService
class PropagateServiceTemplate
BATCH_SIZE = 100
 
def self.propagate(*args)
Loading
Loading
@@ -36,9 +36,7 @@ module Projects
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
Loading
Loading
@@ -54,11 +52,22 @@ module Projects
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
Loading
Loading
@@ -84,11 +93,11 @@ module Projects
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
 
Loading
Loading
@@ -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
Loading
Loading
@@ -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
Loading
Loading
@@ -39,16 +39,16 @@ describe Admin::ServicesController do
)
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: {} }
 
Loading
Loading
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,
Loading
Loading
@@ -23,9 +23,10 @@ describe Projects::PropagateService, services: true do
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',
Loading
Loading
@@ -34,8 +35,6 @@ describe Projects::PropagateService, services: true do
}
)
 
Service.build_from_template(project.id, other_service).save!
expect { described_class.propagate(service_template) }.
to change { Service.count }.by(1)
end
Loading
Loading
@@ -62,7 +61,7 @@ describe Projects::PropagateService, services: true do
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
Loading
Loading
@@ -70,7 +69,7 @@ describe Projects::PropagateService, services: true do
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) }
 
Loading
Loading
@@ -81,7 +80,7 @@ describe Projects::PropagateService, services: true do
 
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)
Loading
Loading
@@ -90,7 +89,7 @@ describe Projects::PropagateService, services: true do
 
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)
Loading
Loading
require 'spec_helper'
 
describe PropagateProjectServiceWorker do
describe PropagateServiceTemplateWorker do
let!(:service_template) do
PushoverService.create(
template: true,
Loading
Loading
@@ -21,7 +21,7 @@ describe PropagateProjectServiceWorker do
 
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
Loading
Loading
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment