gitlab-org--gitlab-foss/app/services/projects/propagate_service.rb

64 lines
1.3 KiB
Ruby
Raw Normal View History

2017-05-04 10:13:33 +00:00
module Projects
class PropagateService
BATCH_SIZE = 100
def self.propagate(*args)
new(*args).propagate
2017-05-04 10:13:33 +00:00
end
def initialize(template)
@template = template
end
def propagate
2017-05-04 10:13:33 +00:00
return unless @template&.active
Rails.logger.info("Propagating services for template #{@template.id}")
propagate_projects_with_template
end
private
def propagate_projects_with_template
loop do
batch = project_ids_batch
2017-05-04 10:13:33 +00:00
bulk_create_from_template(batch)
2017-05-04 10:13:33 +00:00
break if batch.size < BATCH_SIZE
2017-05-04 10:13:33 +00:00
end
end
def bulk_create_from_template(batch)
service_hash_list = batch.map do |project_id|
service_hash.merge('project_id' => project_id)
end
Project.transaction do
Service.create!(service_hash_list)
end
2017-05-04 10:13:33 +00:00
end
def project_ids_batch
2017-05-05 08:51:25 +00:00
Project.connection.select_values(
<<-SQL
SELECT id
FROM projects
WHERE NOT EXISTS (
SELECT true
FROM services
WHERE services.project_id = projects.id
AND services.type = '#{@template.type}'
)
LIMIT #{BATCH_SIZE}
SQL
2017-05-05 08:51:25 +00:00
)
end
def service_hash
@service_hash ||= @template.as_json(methods: :type).except('id', 'template')
2017-05-04 10:13:33 +00:00
end
end
end