process_pipeline_service.rb 2.55 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
13
14
15
      new_builds =
        stage_indexes_of_created_builds.map do |index|
          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
28
      return if HasStatus::BLOCKED_STATUS == current_status

Kamil Trzcinski's avatar
Kamil Trzcinski committed
29
30
      if HasStatus::COMPLETED_STATUSES.include?(current_status)
        created_builds_in_stage(index).select do |build|
31
32
          Gitlab::OptimisticLocking.retry_lock(build) do |subject|
            process_build(subject, current_status)
Kamil Trzcinski's avatar
Kamil Trzcinski committed
33
          end
34
        end
35
36
37
38
39
      end
    end

    def process_build(build, current_status)
      if valid_statuses_for_when(build.when).include?(current_status)
40
        build.action? ? build.actionize : build.enqueue
41
42
        true
      else
Stan Hu's avatar
Stan Hu committed
43
        build.skip
44
45
46
47
48
49
50
        false
      end
    end

    def valid_statuses_for_when(value)
      case value
      when 'on_success'
51
        %w[success skipped]
52
53
54
      when 'on_failure'
        %w[failed]
      when 'always'
55
        %w[success failed skipped]
56
      when 'manual'
57
        %w[success skipped]
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
      else
        []
      end
    end

    def status_for_prior_stages(index)
      pipeline.builds.where('stage_idx < ?', index).latest.status || 'success'
    end

    def stage_indexes_of_created_builds
      created_builds.order(:stage_idx).pluck('distinct stage_idx')
    end

    def created_builds_in_stage(index)
      created_builds.where(stage_idx: index)
    end

    def created_builds
      pipeline.builds.created
    end
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95

    # 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
    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
96
97
  end
end