diff options
author | James Lopez <james@jameslopez.es> | 2017-05-04 12:13:33 +0200 |
---|---|---|
committer | James Lopez <james@jameslopez.es> | 2017-05-04 12:13:33 +0200 |
commit | f81cf84035213002ce7931af6c3ffa917fe7fcbd (patch) | |
tree | 3393496e46cdacd5db03a2348dda8ed1a58a3d57 /app/services/projects | |
parent | 2f7f1ce4e66db847414e2fc3de09556e75c51eb4 (diff) | |
download | gitlab-ce-f81cf84035213002ce7931af6c3ffa917fe7fcbd.tar.gz |
refactor worker into service
Diffstat (limited to 'app/services/projects')
-rw-r--r-- | app/services/projects/propagate_service.rb | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/app/services/projects/propagate_service.rb b/app/services/projects/propagate_service.rb new file mode 100644 index 00000000000..3c05dcce07c --- /dev/null +++ b/app/services/projects/propagate_service.rb @@ -0,0 +1,47 @@ +module Projects + class PropagateService + BATCH_SIZE = 100 + + def self.propagate!(*args) + new(*args).propagate! + end + + def initialize(template) + @template = template + end + + def propagate! + 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) } + + break if batch.count < BATCH_SIZE + + 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 |