diff options
author | Grzegorz Bizon <grzesiek.bizon@gmail.com> | 2018-05-22 14:55:05 +0300 |
---|---|---|
committer | Grzegorz Bizon <grzesiek.bizon@gmail.com> | 2018-05-22 14:55:05 +0300 |
commit | ea35fd05bb175cf921e94d8b1c5e6bc5f3e217c9 (patch) | |
tree | d47f9f986364fe1ed954126bf38207fbcf82a48a /lib/gitlab/ci/pipeline/preloader.rb | |
parent | 76a7157c76c47fd4f835a27eeeda7b42012936be (diff) |
Refactor pipeline preloader to split reponsibilities better
Diffstat (limited to 'lib/gitlab/ci/pipeline/preloader.rb')
-rw-r--r-- | lib/gitlab/ci/pipeline/preloader.rb | 40 |
1 files changed, 30 insertions, 10 deletions
diff --git a/lib/gitlab/ci/pipeline/preloader.rb b/lib/gitlab/ci/pipeline/preloader.rb index 2f792062124..668c1b7189c 100644 --- a/lib/gitlab/ci/pipeline/preloader.rb +++ b/lib/gitlab/ci/pipeline/preloader.rb @@ -5,26 +5,46 @@ module Gitlab module Pipeline # Class for preloading data associated with pipelines such as commit # authors. - module Preloader - def self.preload(pipelines) - # This ensures that all the pipeline commits are eager loaded before we - # start using them. - pipelines.each(&:commit) + class Preloader + def initialize(pipelines) + @pipelines = pipelines + end + + def preload! + @pipelines.each do |pipeline| + Pipeline::Preloader::Instance.new(pipeline) + .preload_commits + .preload_pipeline_warnings + .preload_stages_warnings + end + end - pipelines.each do |pipeline| - # This preloads the author of every commit. We're using "lazy_author" + class Instance + def initialize(pipeline) + @pipeline = pipeline + end + + def preload_commits + # This ensures that all the pipeline commits are eager loaded before we + # start using them. + # + # This also preloads the author of every commit. We're using "lazy_author" # here since "author" immediately loads the data on the first call. - pipeline.commit.try(:lazy_author) + tap { @pipeline.commit.try(:lazy_author) } + end + def preload_pipeline_warnings # This preloads the number of warnings for every pipeline, ensuring # that Ci::Pipeline#has_warnings? doesn't execute any additional # queries. - pipeline.number_of_warnings + tap { @pipeline.number_of_warnings } + end + def preload_stages_warnings # This preloads the number of warnings for every stage, ensuring # that Ci::Stage#has_warnings? doesn't execute any additional # queries. - pipeline.stages.each { |stage| stage.number_of_warnings } + tap { @pipeline.stages.each { |stage| stage.number_of_warnings } } end end end |