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

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

    def execute(pipeline)
      @pipeline = pipeline

10
11
      update_retried

Kamil Trzcinski's avatar
Kamil Trzcinski committed
12
      new_builds =
13
        stage_indexes_of_created_processables.map do |index|
Kamil Trzcinski's avatar
Kamil Trzcinski committed
14
15
          process_stage(index)
        end
16

Kamil Trzcinski's avatar
Kamil Trzcinski committed
17
      @pipeline.update_status
18

Kamil Trzcinski's avatar
Kamil Trzcinski committed
19
      new_builds.flatten.any?
20
21
22
23
24
25
26
    end

    private

    def process_stage(index)
      current_status = status_for_prior_stages(index)

27
      return if HasStatus::BLOCKED_STATUS.include?(current_status)
28

Kamil Trzcinski's avatar
Kamil Trzcinski committed
29
      if HasStatus::COMPLETED_STATUSES.include?(current_status)
30
        created_processables_in_stage(index).select do |build|
31
          Gitlab::OptimisticLocking.retry_lock(build) do |subject|
32
33
            Ci::ProcessBuildService.new(project, @user)
              .execute(build, current_status)
Kamil Trzcinski's avatar
Kamil Trzcinski committed
34
          end
35
        end
36
37
38
      end
    end

39
    # rubocop: disable CodeReuse/ActiveRecord
40
41
42
    def status_for_prior_stages(index)
      pipeline.builds.where('stage_idx < ?', index).latest.status || 'success'
    end
43
    # rubocop: enable CodeReuse/ActiveRecord
44

45
    # rubocop: disable CodeReuse/ActiveRecord
46
47
    def stage_indexes_of_created_processables
      created_processables.order(:stage_idx).pluck('distinct stage_idx')
48
    end
49
    # rubocop: enable CodeReuse/ActiveRecord
50

51
    # rubocop: disable CodeReuse/ActiveRecord
52
53
    def created_processables_in_stage(index)
      created_processables.where(stage_idx: index)
54
    end
55
    # rubocop: enable CodeReuse/ActiveRecord
56

57
    def created_processables
58
      pipeline.processables.created
59
    end
60
61
62
63
64

    # 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
65
    # rubocop: disable CodeReuse/ActiveRecord
66
67
68
69
70
71
72
73
74
75
76
77
78
    def update_retried
      # find the latest builds for each name
      latest_statuses = pipeline.statuses.latest
        .group(:name)
        .having('count(*) > 1')
        .pluck('max(id)', 'name')

      # 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
79
    # rubocop: enable CodeReuse/ActiveRecord
80
81
  end
end