2018-06-27 07:23:28 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-03-02 23:43:39 +00:00
|
|
|
class StuckCiJobsWorker
|
2017-11-28 16:08:30 +00:00
|
|
|
include ApplicationWorker
|
2016-10-21 16:13:41 +00:00
|
|
|
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
|
|
|
|
2017-02-07 13:00:46 +00:00
|
|
|
BUILD_RUNNING_OUTDATED_TIMEOUT = 1.hour
|
|
|
|
BUILD_PENDING_OUTDATED_TIMEOUT = 1.day
|
2018-09-25 06:18:02 +00:00
|
|
|
BUILD_SCHEDULED_OUTDATED_TIMEOUT = 1.hour
|
2017-02-07 13:00:46 +00:00
|
|
|
BUILD_PENDING_STUCK_TIMEOUT = 1.hour
|
2018-10-01 04:10:30 +00:00
|
|
|
BUILD_SCHEDULED_OUTDATED_BATCH_SIZE = 100
|
2015-10-21 09:29:47 +00:00
|
|
|
|
|
|
|
def perform
|
2017-02-07 22:06:16 +00:00
|
|
|
return unless try_obtain_lease
|
|
|
|
|
|
|
|
Rails.logger.info "#{self.class}: Cleaning stuck builds"
|
2015-10-21 09:29:47 +00:00
|
|
|
|
2018-09-26 05:28:22 +00:00
|
|
|
drop :running, BUILD_RUNNING_OUTDATED_TIMEOUT
|
|
|
|
drop :pending, BUILD_PENDING_OUTDATED_TIMEOUT
|
|
|
|
drop_stuck :pending, BUILD_PENDING_STUCK_TIMEOUT
|
|
|
|
drop_stale_scheduled_builds
|
2017-02-09 02:29:38 +00:00
|
|
|
|
|
|
|
remove_lease
|
2017-02-07 13:00:46 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2017-02-07 22:06:16 +00:00
|
|
|
def try_obtain_lease
|
2017-03-01 12:56:54 +00:00
|
|
|
@uuid = Gitlab::ExclusiveLease.new(EXCLUSIVE_LEASE_KEY, timeout: 30.minutes).try_obtain
|
2017-02-07 22:06:16 +00:00
|
|
|
end
|
|
|
|
|
2017-02-09 02:29:38 +00:00
|
|
|
def remove_lease
|
|
|
|
Gitlab::ExclusiveLease.cancel(EXCLUSIVE_LEASE_KEY, @uuid)
|
|
|
|
end
|
|
|
|
|
2018-09-26 05:28:22 +00:00
|
|
|
def drop(status, timeout)
|
|
|
|
search(status, timeout) do |build|
|
|
|
|
drop_build :outdated, build, status, timeout, :stuck_or_timeout_failure
|
2017-02-07 13:00:46 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-09-26 05:28:22 +00:00
|
|
|
def drop_stuck(status, timeout)
|
|
|
|
search(status, timeout) do |build|
|
2018-04-18 09:19:40 +00:00
|
|
|
break unless build.stuck?
|
2017-11-14 09:02:39 +00:00
|
|
|
|
2018-09-26 05:28:22 +00:00
|
|
|
drop_build :stuck, build, status, timeout, :stuck_or_timeout_failure
|
2015-10-21 09:29:47 +00:00
|
|
|
end
|
2017-02-07 13:00:46 +00:00
|
|
|
end
|
|
|
|
|
2018-09-25 09:44:08 +00:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2018-09-26 05:28:22 +00:00
|
|
|
def search(status, timeout)
|
2017-11-21 18:49:10 +00:00
|
|
|
loop do
|
|
|
|
jobs = Ci::Build.where(status: status)
|
2018-09-26 05:28:22 +00:00
|
|
|
.where('ci_builds.updated_at < ?', 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
|
2017-02-07 13:00:46 +00:00
|
|
|
end
|
|
|
|
end
|
2018-09-26 05:28:22 +00:00
|
|
|
|
|
|
|
def drop_stale_scheduled_builds
|
|
|
|
# `ci_builds` table has a partial index on `id` with `scheduled_at <> NULL` condition.
|
|
|
|
# Therefore this query's first step uses Index Search, and the following expensive
|
|
|
|
# filter `scheduled_at < ?` will only perform on a small subset (max: 100 rows)
|
2018-10-01 04:10:30 +00:00
|
|
|
Ci::Build.include(EachBatch)
|
|
|
|
.where('scheduled_at IS NOT NULL')
|
|
|
|
.each_batch(of: BUILD_SCHEDULED_OUTDATED_BATCH_SIZE) do |relation|
|
|
|
|
relation
|
|
|
|
.where('scheduled_at < ?', BUILD_SCHEDULED_OUTDATED_TIMEOUT.ago)
|
|
|
|
.find_each(batch_size: BUILD_SCHEDULED_OUTDATED_BATCH_SIZE) do |build|
|
2018-10-02 04:32:35 +00:00
|
|
|
drop_build(:outdated, build, :scheduled, BUILD_SCHEDULED_OUTDATED_TIMEOUT, :stale_schedule)
|
2018-09-26 05:28:22 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2018-09-27 05:02:24 +00:00
|
|
|
# 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}, reason: #{reason})"
|
|
|
|
Gitlab::OptimisticLocking.retry_lock(build, 3) do |b|
|
|
|
|
b.drop(reason)
|
|
|
|
end
|
|
|
|
end
|
2015-10-21 09:29:47 +00:00
|
|
|
end
|