diff options
author | James Lopez <james@jameslopez.es> | 2017-05-03 17:20:12 +0200 |
---|---|---|
committer | James Lopez <james@jameslopez.es> | 2017-05-03 17:20:12 +0200 |
commit | 264bf229277caf1c1eaca4e83921ca1b305d5401 (patch) | |
tree | 610c4b0427f8303a5484c06e6f4779b7c9e1669d /app | |
parent | e81ea165ba738fedab07d5e20423856e004e2594 (diff) | |
download | gitlab-ce-264bf229277caf1c1eaca4e83921ca1b305d5401.tar.gz |
add propagate service worker and updated spec and controller
Diffstat (limited to 'app')
-rw-r--r-- | app/controllers/admin/services_controller.rb | 2 | ||||
-rw-r--r-- | app/models/service.rb | 10 | ||||
-rw-r--r-- | app/workers/propagate_project_service_worker.rb | 49 |
3 files changed, 60 insertions, 1 deletions
diff --git a/app/controllers/admin/services_controller.rb b/app/controllers/admin/services_controller.rb index 37a1a23178e..2b6f335cb2b 100644 --- a/app/controllers/admin/services_controller.rb +++ b/app/controllers/admin/services_controller.rb @@ -15,7 +15,7 @@ class Admin::ServicesController < Admin::ApplicationController end def update - if service.update_attributes(service_params[:service]) + if service.update_and_propagate(service_params[:service]) redirect_to admin_application_settings_services_path, notice: 'Application settings saved successfully' else diff --git a/app/models/service.rb b/app/models/service.rb index c71a7d169ec..dea22fd96a7 100644 --- a/app/models/service.rb +++ b/app/models/service.rb @@ -254,6 +254,16 @@ class Service < ActiveRecord::Base service end + def update_and_propagate(service_params) + return false unless update_attributes(service_params) + + if service_params[:active] == 1 + PropagateProjectServiceWorker.perform_async(service_params[:id]) + end + + true + end + private def cache_project_has_external_issue_tracker diff --git a/app/workers/propagate_project_service_worker.rb b/app/workers/propagate_project_service_worker.rb new file mode 100644 index 00000000000..53551770968 --- /dev/null +++ b/app/workers/propagate_project_service_worker.rb @@ -0,0 +1,49 @@ +# Worker for updating any project specific caches. +class PropagateProjectServiceWorker + include Sidekiq::Worker + include DedicatedSidekiqQueue + + LEASE_TIMEOUT = 30.minutes.to_i + + def perform(template_id) + template = Service.find_by(id: template_id) + + return unless template&.active + return unless try_obtain_lease_for(template.id) + + Rails.logger.info("Propagating services for template #{template.id}") + + project_ids_for_template(template) do |project_id| + Service.build_from_template(project_id, template).save! + end + end + + private + + def project_ids_for_template(template) + limit = 100 + offset = 0 + + loop do + batch = project_ids_batch(limit, offset, template.type) + + batch.each { |project_id| yield(project_id) } + + break if batch.count < limit + + offset += limit + end + end + + def project_ids_batch(limit, offset, template_type) + Project.joins('LEFT JOIN services ON services.project_id = projects.id'). + where('services.type != ? OR services.id IS NULL', template_type). + limit(limit).offset(offset).pluck(:id) + end + + def try_obtain_lease_for(template_id) + Gitlab::ExclusiveLease. + new("propagate_project_service_worker:#{template_id}", timeout: LEASE_TIMEOUT). + try_obtain + end +end |