Module: Ci::StuckBuilds::DropHelpers

Included in:
DropPendingService, DropRunningService, DropScheduledService
Defined in:
app/services/ci/stuck_builds/drop_helpers.rb

Instance Method Summary collapse

Instance Method Details

#drop(builds, failure_reason:) ⇒ Object



6
7
8
9
10
# File 'app/services/ci/stuck_builds/drop_helpers.rb', line 6

def drop(builds, failure_reason:)
  fetch(builds) do |build|
    drop_build :outdated, build, failure_reason
  end
end

#drop_build(type, build, reason) ⇒ Object

rubocop: enable CodeReuse/ActiveRecord



36
37
38
39
40
41
42
43
44
45
# File 'app/services/ci/stuck_builds/drop_helpers.rb', line 36

def drop_build(type, build, reason)
  log_dropping_message(type, build, reason)
  Gitlab::OptimisticLocking.retry_lock(build, 3, name: 'stuck_ci_jobs_worker_drop_build') do |b|
    b.drop(reason)
  end
rescue StandardError => ex
  build.doom!

  track_exception_for_build(ex, build)
end

#drop_stuck(builds, failure_reason:) ⇒ Object



12
13
14
15
16
17
18
# File 'app/services/ci/stuck_builds/drop_helpers.rb', line 12

def drop_stuck(builds, failure_reason:)
  fetch(builds) do |build|
    break unless build.stuck?

    drop_build :stuck, build, failure_reason
  end
end

#fetch(builds) ⇒ Object

rubocop: disable CodeReuse/ActiveRecord



21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'app/services/ci/stuck_builds/drop_helpers.rb', line 21

def fetch(builds)
  loop do
    jobs = builds.includes(:tags, :runner, project: [:namespace, :route])
      .limit(100)
      .to_a

    break if jobs.empty?

    jobs.each do |job|
      Gitlab::ApplicationContext.with_context(project: job.project) { yield(job) }
    end
  end
end

#log_dropping_message(type, build, reason) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
# File 'app/services/ci/stuck_builds/drop_helpers.rb', line 58

def log_dropping_message(type, build, reason)
  Gitlab::AppLogger.info(
    class: self.class.name,
    message: "Dropping #{type} build",
    build_stuck_type: type,
    build_id: build.id,
    runner_id: build.runner_id,
    build_status: build.status,
    build_failure_reason: reason
  )
end

#track_exception_for_build(ex, build) ⇒ Object



47
48
49
50
51
52
53
54
55
56
# File 'app/services/ci/stuck_builds/drop_helpers.rb', line 47

def track_exception_for_build(ex, build)
  Gitlab::ErrorTracking.track_exception(
    ex,
    build_id: build.id,
    build_name: build.name,
    build_stage: build.stage_name,
    pipeline_id: build.pipeline_id,
    project_id: build.project_id
  )
end