summaryrefslogtreecommitdiff
path: root/app/services/ci/ensure_stage_service.rb
blob: b8c7be2d3508709ab44794e3122646cd050655af (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
module Ci
  ##
  # We call this service everytime we persist a CI/CD job.
  #
  # In most cases a job should already have a stage assigned,  but in cases it
  # doesn't have we need to either find existing one or create a brand new
  # stage.
  #
  class EnsureStageService < BaseService
    EnsureStageError = Class.new(StandardError)

    def execute(build)
      @build = build

      return if build.stage_id.present?
      return if build.invalid?

      ensure_stage.tap do |stage|
        build.stage_id = stage.id

        yield stage if block_given?
      end
    end

    private

    def ensure_stage(attempts: 2)
      find_stage || create_stage
    rescue ActiveRecord::RecordNotUnique
      retry if (attempts -= 1) > 0

      raise EnsureStageError, <<~EOS
        We failed to find or create a unique pipeline stage after 2 retries.
        This should never happen and is most likely the result of a bug in
        the database load balancing code.
      EOS
    end

    def find_stage
      @build.pipeline.stages.find_by(name: @build.stage)
    end

    def create_stage
      Ci::Stage.create!(name: @build.stage,
                        position: @build.stage_idx,
                        pipeline: @build.pipeline,
                        project: @build.project)
    end
  end
end