summaryrefslogtreecommitdiff
path: root/app/workers/stuck_ci_jobs_worker.rb
blob: 5d3b73f5247e167924aad7d496a9bb72b9e3913a (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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
class StuckCiJobsWorker
  include ApplicationWorker
  include CronjobQueue
  include ExclusiveLeaseGuard

  EXCLUSIVE_LEASE_KEY = 'stuck_ci_builds_worker_lease'.freeze

  BUILD_RUNNING_OUTDATED_TIMEOUT = 1.hour
  BUILD_PENDING_OUTDATED_TIMEOUT = 1.day
  BUILD_PENDING_STUCK_TIMEOUT = 1.hour

  def perform
    try_obtain_lease do
      Rails.logger.info "#{self.class}: Cleaning stuck builds"

      drop :running, BUILD_RUNNING_OUTDATED_TIMEOUT
      drop :pending, BUILD_PENDING_OUTDATED_TIMEOUT
      drop_stuck :pending, BUILD_PENDING_STUCK_TIMEOUT
    end
  end

  private

  def lease_key
    EXCLUSIVE_LEASE_KEY
  end

  def lease_timeout
    30.minutes
  end

  def drop(status, timeout)
    search(status, timeout) do |build|
      drop_build :outdated, build, status, timeout
    end
  end

  def drop_stuck(status, timeout)
    search(status, timeout) do |build|
      return unless build.stuck?

      drop_build :stuck, build, status, timeout
    end
  end

  def search(status, timeout)
    loop do
      jobs = Ci::Build.where(status: status)
        .where('ci_builds.updated_at < ?', timeout.ago)
        .includes(:tags, :runner, project: :namespace)
        .limit(100)
        .to_a
      break if jobs.empty?

      jobs.each do |job|
        yield(job)
      end
    end
  end

  def drop_build(type, build, status, timeout)
    Rails.logger.info "#{self.class}: Dropping #{type} build #{build.id} for runner #{build.runner_id} (status: #{status}, timeout: #{timeout})"
    Gitlab::OptimisticLocking.retry_lock(build, 3) do |b|
      b.drop(:stuck_or_timeout_failure)
    end
  end
end