gitlab-org--gitlab-foss/app/workers/stuck_ci_jobs_worker.rb

77 lines
2.2 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
class StuckCiJobsWorker
include ApplicationWorker
include CronjobQueue
2015-10-21 09:29:47 +00:00
2017-03-01 12:56:54 +00:00
EXCLUSIVE_LEASE_KEY = 'stuck_ci_builds_worker_lease'.freeze
2017-02-09 02:29:38 +00:00
BUILD_RUNNING_OUTDATED_TIMEOUT = 1.hour
BUILD_PENDING_OUTDATED_TIMEOUT = 1.day
BUILD_SCHEDULED_OUTDATED_TIMEOUT = 1.hour
BUILD_PENDING_STUCK_TIMEOUT = 1.hour
2015-10-21 09:29:47 +00:00
def perform
return unless try_obtain_lease
Rails.logger.info "#{self.class}: Cleaning stuck builds"
2015-10-21 09:29:47 +00:00
drop :running, :updated_at, BUILD_RUNNING_OUTDATED_TIMEOUT, :stuck_or_timeout_failure
drop :pending, :updated_at, BUILD_PENDING_OUTDATED_TIMEOUT, :stuck_or_timeout_failure
drop :scheduled, :scheduled_at, BUILD_SCHEDULED_OUTDATED_TIMEOUT, :schedule_expired
drop_stuck :pending, :updated_at, BUILD_PENDING_STUCK_TIMEOUT, :stuck_or_timeout_failure
2017-02-09 02:29:38 +00:00
remove_lease
end
private
def try_obtain_lease
2017-03-01 12:56:54 +00:00
@uuid = Gitlab::ExclusiveLease.new(EXCLUSIVE_LEASE_KEY, timeout: 30.minutes).try_obtain
end
2017-02-09 02:29:38 +00:00
def remove_lease
Gitlab::ExclusiveLease.cancel(EXCLUSIVE_LEASE_KEY, @uuid)
end
def drop(status, column, timeout, reason)
search(status, column, timeout) do |build|
drop_build :outdated, build, status, timeout, reason
end
end
def drop_stuck(status, column, timeout, reason)
search(status, column, timeout) do |build|
break unless build.stuck?
drop_build :stuck, build, status, timeout, reason
2015-10-21 09:29:47 +00:00
end
end
# rubocop: disable CodeReuse/ActiveRecord
def search(status, column, timeout)
quoted_column = ActiveRecord::Base.connection.quote_column_name(column)
2017-11-21 18:49:10 +00:00
loop do
jobs = Ci::Build.where(status: status)
.where("#{quoted_column} < ?", timeout.ago)
2017-11-21 17:44:52 +00:00
.includes(:tags, :runner, project: :namespace)
2017-11-21 18:49:10 +00:00
.limit(100)
.to_a
break if jobs.empty?
jobs.each do |job|
yield(job)
2017-11-21 17:44:52 +00:00
end
end
end
# rubocop: enable CodeReuse/ActiveRecord
def drop_build(type, build, status, timeout, reason)
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(reason)
end
2015-10-21 09:29:47 +00:00
end
end