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
|
|
|
|
offset = 0
|
|
|
|
|
|
|
|
loop do
|
|
|
|
batch = project_ids_batch(offset)
|
|
|
|
|
|
|
|
batch.each { |project_id| create_from_template(project_id) }
|
|
|
|
|
2017-05-04 16:11:28 +00:00
|
|
|
break if batch.size < BATCH_SIZE
|
2017-05-04 10:13:33 +00:00
|
|
|
|
|
|
|
offset += BATCH_SIZE
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def create_from_template(project_id)
|
|
|
|
Service.build_from_template(project_id, @template).save!
|
|
|
|
end
|
|
|
|
|
|
|
|
def project_ids_batch(offset)
|
|
|
|
Project.joins('LEFT JOIN services ON services.project_id = projects.id').
|
|
|
|
where('services.type != ? OR services.id IS NULL', @template.type).
|
|
|
|
limit(BATCH_SIZE).offset(offset).pluck(:id)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|