diff options
author | James Lopez <james@jameslopez.es> | 2017-05-05 18:01:33 +0200 |
---|---|---|
committer | James Lopez <james@jameslopez.es> | 2017-05-05 18:01:33 +0200 |
commit | 6ecf16b8f70335e1e6868b7c918cd031f5eb2f8d (patch) | |
tree | e57683de43fa63c278929bcba679d1077906a2fe | |
parent | ce418036c763219df8239632f71ef0e9782be7ea (diff) | |
download | gitlab-ce-6ecf16b8f70335e1e6868b7c918cd031f5eb2f8d.tar.gz |
refactor code based on feedback
-rw-r--r-- | app/controllers/admin/services_controller.rb | 2 | ||||
-rw-r--r-- | app/services/projects/propagate_service_template.rb (renamed from app/services/projects/propagate_service.rb) | 21 | ||||
-rw-r--r-- | app/workers/propagate_service_template_worker.rb (renamed from app/workers/propagate_project_service_worker.rb) | 6 | ||||
-rw-r--r-- | config/sidekiq_queues.yml | 2 | ||||
-rw-r--r-- | lib/gitlab/sql/bulk_insert.rb | 21 | ||||
-rw-r--r-- | spec/controllers/admin/services_controller_spec.rb | 8 | ||||
-rw-r--r-- | spec/services/projects/propagate_service_template_spec.rb (renamed from spec/services/projects/propagate_service_spec.rb) | 17 | ||||
-rw-r--r-- | spec/workers/propagate_service_template_worker_spec.rb (renamed from spec/workers/propagate_project_service_worker_spec.rb) | 4 |
8 files changed, 34 insertions, 47 deletions
diff --git a/app/controllers/admin/services_controller.rb b/app/controllers/admin/services_controller.rb index e335fbfffed..a40ce3c2418 100644 --- a/app/controllers/admin/services_controller.rb +++ b/app/controllers/admin/services_controller.rb @@ -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' diff --git a/app/services/projects/propagate_service.rb b/app/services/projects/propagate_service_template.rb index f4fae478609..32ad68673ac 100644 --- a/app/services/projects/propagate_service.rb +++ b/app/services/projects/propagate_service_template.rb @@ -1,5 +1,5 @@ module Projects - class PropagateService + class PropagateServiceTemplate BATCH_SIZE = 100 def self.propagate(*args) @@ -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 @@ -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 @@ -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 diff --git a/app/workers/propagate_project_service_worker.rb b/app/workers/propagate_service_template_worker.rb index 5eabe4eaecd..f1fc7ccb955 100644 --- a/app/workers/propagate_project_service_worker.rb +++ b/app/workers/propagate_service_template_worker.rb @@ -1,5 +1,5 @@ # 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 diff --git a/config/sidekiq_queues.yml b/config/sidekiq_queues.yml index 91ea1c0f779..433381e79d3 100644 --- a/config/sidekiq_queues.yml +++ b/config/sidekiq_queues.yml @@ -53,4 +53,4 @@ - [pages, 1] - [system_hook_push, 1] - [update_user_activity, 1] - - [propagate_project_service, 1] + - [propagate_service_template, 1] diff --git a/lib/gitlab/sql/bulk_insert.rb b/lib/gitlab/sql/bulk_insert.rb deleted file mode 100644 index 097f9ff237b..00000000000 --- a/lib/gitlab/sql/bulk_insert.rb +++ /dev/null @@ -1,21 +0,0 @@ -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 diff --git a/spec/controllers/admin/services_controller_spec.rb b/spec/controllers/admin/services_controller_spec.rb index 808c98edb7f..c94616d8508 100644 --- a/spec/controllers/admin/services_controller_spec.rb +++ b/spec/controllers/admin/services_controller_spec.rb @@ -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: {} } diff --git a/spec/services/projects/propagate_service_spec.rb b/spec/services/projects/propagate_service_template_spec.rb index b8aa4de5bd1..331fb3c5ac5 100644 --- a/spec/services/projects/propagate_service_spec.rb +++ b/spec/services/projects/propagate_service_template_spec.rb @@ -1,7 +1,7 @@ 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 @@ 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', @@ -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 @@ -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 @@ -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) } @@ -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) @@ -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) diff --git a/spec/workers/propagate_project_service_worker_spec.rb b/spec/workers/propagate_service_template_worker_spec.rb index 4c7edbfcd3e..7040d5ef81c 100644 --- a/spec/workers/propagate_project_service_worker_spec.rb +++ b/spec/workers/propagate_service_template_worker_spec.rb @@ -1,6 +1,6 @@ require 'spec_helper' -describe PropagateProjectServiceWorker do +describe PropagateServiceTemplateWorker do let!(:service_template) do PushoverService.create( template: true, @@ -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 |