process_pipeline_service.rb 4.1 KB
Newer Older
1 2
# frozen_string_literal: true

3 4 5 6
module Ci
  class ProcessPipelineService < BaseService
    attr_reader :pipeline

7
    def execute(pipeline, trigger_build_ids = nil)
8 9
      @pipeline = pipeline

10 11
      update_retried

12
      success = process_stages_without_needs
K
Kamil Trzciński 已提交
13 14 15

      # we evaluate dependent needs,
      # only when the another job has finished
16
      success = process_builds_with_needs(trigger_build_ids) || success
17

K
Kamil Trzcinski 已提交
18
      @pipeline.update_status
19

K
Kamil Trzciński 已提交
20
      success
21 22 23 24
    end

    private

25 26 27 28 29 30 31
    def process_stages_without_needs
      stage_indexes_of_created_processables_without_needs.flat_map do |index|
        process_stage_without_needs(index)
      end.any?
    end

    def process_stage_without_needs(index)
32 33
      current_status = status_for_prior_stages(index)

34
      return unless HasStatus::COMPLETED_STATUSES.include?(current_status)
35

36 37
      created_processables_in_stage_without_needs(index).select do |build|
        process_build(build, current_status)
38 39 40
      end
    end

41 42
    def process_builds_with_needs(trigger_build_ids)
      return false unless trigger_build_ids.present?
K
Kamil Trzciński 已提交
43
      return false unless Feature.enabled?(:ci_dag_support, project, default_enabled: true)
K
Kamil Trzciński 已提交
44

45 46 47 48 49 50
      # rubocop: disable CodeReuse/ActiveRecord
      trigger_build_names = pipeline.statuses
        .where(id: trigger_build_ids)
        .select(:name)
      # rubocop: enable CodeReuse/ActiveRecord

K
Kamil Trzciński 已提交
51
      created_processables
52
        .with_needs(trigger_build_names)
K
Kamil Trzciński 已提交
53 54 55 56 57 58 59 60 61 62
        .find_each
        .map(&method(:process_build_with_needs))
        .any?
    end

    def process_build_with_needs(build)
      current_status = status_for_build_needs(build.needs.map(&:name))

      return unless HasStatus::COMPLETED_STATUSES.include?(current_status)

63 64 65 66
      process_build(build, current_status)
    end

    def process_build(build, current_status)
K
Kamil Trzciński 已提交
67 68 69 70 71 72
      Gitlab::OptimisticLocking.retry_lock(build) do |subject|
        Ci::ProcessBuildService.new(project, @user)
          .execute(subject, current_status)
      end
    end

73
    # rubocop: disable CodeReuse/ActiveRecord
74 75 76
    def status_for_prior_stages(index)
      pipeline.builds.where('stage_idx < ?', index).latest.status || 'success'
    end
77
    # rubocop: enable CodeReuse/ActiveRecord
78

K
Kamil Trzciński 已提交
79 80 81 82 83 84
    # rubocop: disable CodeReuse/ActiveRecord
    def status_for_build_needs(needs)
      pipeline.builds.where(name: needs).latest.status || 'success'
    end
    # rubocop: enable CodeReuse/ActiveRecord

85
    # rubocop: disable CodeReuse/ActiveRecord
86 87 88
    def stage_indexes_of_created_processables_without_needs
      created_processables_without_needs.order(:stage_idx)
        .pluck(Arel.sql('DISTINCT stage_idx'))
89
    end
90
    # rubocop: enable CodeReuse/ActiveRecord
91

92
    # rubocop: disable CodeReuse/ActiveRecord
93 94 95
    def created_processables_in_stage_without_needs(index)
      created_processables_without_needs
        .where(stage_idx: index)
96
    end
97
    # rubocop: enable CodeReuse/ActiveRecord
98

99
    def created_processables_without_needs
K
Kamil Trzciński 已提交
100
      if Feature.enabled?(:ci_dag_support, project, default_enabled: true)
101 102 103 104 105 106
        pipeline.processables.created.without_needs
      else
        pipeline.processables.created
      end
    end

107
    def created_processables
108
      pipeline.processables.created
109
    end
110 111 112 113 114

    # This method is for compatibility and data consistency and should be removed with 9.3 version of GitLab
    # This replicates what is db/post_migrate/20170416103934_upate_retried_for_ci_build.rb
    # and ensures that functionality will not be broken before migration is run
    # this updates only when there are data that needs to be updated, there are two groups with no retried flag
115
    # rubocop: disable CodeReuse/ActiveRecord
116 117 118 119 120
    def update_retried
      # find the latest builds for each name
      latest_statuses = pipeline.statuses.latest
        .group(:name)
        .having('count(*) > 1')
H
Heinrich Lee Yu 已提交
121
        .pluck(Arel.sql('MAX(id)'), 'name')
122 123 124 125 126 127 128

      # mark builds that are retried
      pipeline.statuses.latest
        .where(name: latest_statuses.map(&:second))
        .where.not(id: latest_statuses.map(&:first))
        .update_all(retried: true) if latest_statuses.any?
    end
129
    # rubocop: enable CodeReuse/ActiveRecord
130 131
  end
end