summaryrefslogtreecommitdiff
path: root/lib/gitlab/ci/pipeline/chain/create.rb
blob: 71dfc1a676cac623be70f57e075de3f9c52090b8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# frozen_string_literal: true

module Gitlab
  module Ci
    module Pipeline
      module Chain
        class Create < Chain::Base
          include Chain::Helpers
          include Gitlab::Utils::StrongMemoize

          def perform!
            logger.instrument_with_sql(:pipeline_save) do
              BulkInsertableAssociations.with_bulk_insert do
                with_bulk_insert_tags do
                  pipeline.transaction do
                    pipeline.save!
                    CommitStatus.bulk_insert_tags!(statuses)
                  end
                end
              end
            end
          rescue ActiveRecord::RecordInvalid => e
            error("Failed to persist the pipeline: #{e}")
          end

          def break?
            !pipeline.persisted?
          end

          private

          def with_bulk_insert_tags
            previous = Thread.current['ci_bulk_insert_tags']
            Thread.current['ci_bulk_insert_tags'] = true
            yield
          ensure
            Thread.current['ci_bulk_insert_tags'] = previous
          end

          def statuses
            strong_memoize(:statuses) do
              pipeline
                .stages
                .flat_map(&:statuses)
                .select { |status| status.respond_to?(:tag_list) }
            end
          end
        end
      end
    end
  end
end