2017-05-04 10:13:33 +00:00
|
|
|
module Projects
|
|
|
|
class PropagateService
|
|
|
|
BATCH_SIZE = 100
|
|
|
|
|
2017-05-04 16:11:28 +00:00
|
|
|
def self.propagate(*args)
|
|
|
|
new(*args).propagate
|
2017-05-04 10:13:33 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def initialize(template)
|
|
|
|
@template = template
|
|
|
|
end
|
|
|
|
|
2017-05-04 16:11:28 +00:00
|
|
|
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
|
2017-05-05 08:43:56 +00:00
|
|
|
batch = project_ids_batch
|
2017-05-04 10:13:33 +00:00
|
|
|
|
2017-05-05 12:43:22 +00:00
|
|
|
bulk_create_from_template(batch) unless batch.empty?
|
2017-05-04 10:13:33 +00:00
|
|
|
|
2017-05-04 16:11:28 +00:00
|
|
|
break if batch.size < BATCH_SIZE
|
2017-05-04 10:13:33 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-05-05 07:40:44 +00:00
|
|
|
def bulk_create_from_template(batch)
|
2017-05-05 12:43:22 +00:00
|
|
|
service_list = batch.map do |project_id|
|
|
|
|
service_hash.merge('project_id' => project_id).values
|
2017-05-05 07:40:44 +00:00
|
|
|
end
|
|
|
|
|
2017-05-05 12:43:22 +00:00
|
|
|
# Project.transaction do
|
|
|
|
# Service.create!(service_hash_list)
|
|
|
|
# end
|
|
|
|
Gitlab::SQL::BulkInsert.new(service_hash.keys + ['project_id'],
|
|
|
|
service_list,
|
|
|
|
'services').execute
|
2017-05-04 10:13:33 +00:00
|
|
|
end
|
|
|
|
|
2017-05-05 08:43:56 +00:00
|
|
|
def project_ids_batch
|
2017-05-05 08:51:25 +00:00
|
|
|
Project.connection.select_values(
|
2017-05-05 07:40:44 +00:00
|
|
|
<<-SQL
|
|
|
|
SELECT id
|
|
|
|
FROM projects
|
|
|
|
WHERE NOT EXISTS (
|
|
|
|
SELECT true
|
|
|
|
FROM services
|
|
|
|
WHERE services.project_id = projects.id
|
|
|
|
AND services.type = '#{@template.type}'
|
|
|
|
)
|
2017-05-05 08:43:56 +00:00
|
|
|
LIMIT #{BATCH_SIZE}
|
2017-05-05 07:40:44 +00:00
|
|
|
SQL
|
2017-05-05 08:51:25 +00:00
|
|
|
)
|
2017-05-05 07:40:44 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def service_hash
|
2017-05-05 12:43:22 +00:00
|
|
|
@service_hash ||=
|
|
|
|
begin
|
|
|
|
template_hash = @template.as_json(methods: :type).except('id', 'template', 'project_id')
|
|
|
|
|
|
|
|
template_hash.each_with_object({}) do |(key, value), service_hash|
|
|
|
|
value = value.is_a?(Hash) ? value.to_json : value
|
|
|
|
key = Gitlab::Database.postgresql? ? "\"#{key}\"" : "`#{key}`"
|
|
|
|
|
|
|
|
service_hash[key] = ActiveRecord::Base.sanitize(value)
|
|
|
|
end
|
|
|
|
end
|
2017-05-04 10:13:33 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|