Class: Gitlab::Ci::Pipeline::Preloader

Inherits:
Object
  • Object
show all
Defined in:
lib/gitlab/ci/pipeline/preloader.rb

Overview

Class for preloading data associated with pipelines such as commit authors.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pipeline) ⇒ Preloader

Returns a new instance of Preloader.


27
28
29
# File 'lib/gitlab/ci/pipeline/preloader.rb', line 27

def initialize(pipeline)
  @pipeline = pipeline
end

Class Method Details

.preload!(pipelines) ⇒ Object


9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/gitlab/ci/pipeline/preloader.rb', line 9

def self.preload!(pipelines)
  ##
  # This preloads all commits at once, because `Ci::Pipeline#commit` is
  # using a lazy batch loading, what results in only one batched Gitaly
  # call.
  #
  pipelines.each(&:commit)

  pipelines.each do |pipeline|
    self.new(pipeline).tap do |preloader|
      preloader.preload_commit_authors
      preloader.preload_ref_commits
      preloader.preload_pipeline_warnings
      preloader.preload_stages_warnings
    end
  end
end

Instance Method Details

#preload_commit_authorsObject

This also preloads the author of every commit. We're using “lazy_author” here since “author” immediately loads the data on the first call.


33
34
35
# File 'lib/gitlab/ci/pipeline/preloader.rb', line 33

def preload_commit_authors
  @pipeline.commit.try(:lazy_author)
end

#preload_pipeline_warningsObject


44
45
46
47
48
49
# File 'lib/gitlab/ci/pipeline/preloader.rb', line 44

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
end

#preload_ref_commitsObject

This preloads latest commits for given refs and therefore makes it much less expensive to check if a pipeline is a latest one for given branch.


40
41
42
# File 'lib/gitlab/ci/pipeline/preloader.rb', line 40

def preload_ref_commits
  @pipeline.lazy_ref_commit
end

#preload_stages_warningsObject

This preloads the number of warnings for every stage, ensuring that Ci::Stage#has_warnings? doesn't execute any additional queries.


54
55
56
# File 'lib/gitlab/ci/pipeline/preloader.rb', line 54

def preload_stages_warnings
  @pipeline.stages.each { |stage| stage.number_of_warnings }
end