2018-06-27 03:23:28 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2020-02-19 13:09:10 -05:00
|
|
|
class StuckCiJobsWorker # rubocop:disable Scalability/IdempotentWorker
|
2017-11-28 11:08:30 -05:00
|
|
|
include ApplicationWorker
|
2020-02-10 13:09:00 -05:00
|
|
|
include CronjobQueue
|
2015-10-21 05:29:47 -04:00
|
|
|
|
2019-10-18 07:11:44 -04:00
|
|
|
feature_category :continuous_integration
|
2019-10-30 11:14:17 -04:00
|
|
|
worker_resource_boundary :cpu
|
2019-10-18 07:11:44 -04:00
|
|
|
|
2019-09-04 14:10:43 -04:00
|
|
|
EXCLUSIVE_LEASE_KEY = 'stuck_ci_builds_worker_lease'
|
2017-02-08 21:29:38 -05:00
|
|
|
|
2017-02-07 08:00:46 -05:00
|
|
|
BUILD_RUNNING_OUTDATED_TIMEOUT = 1.hour
|
|
|
|
BUILD_PENDING_OUTDATED_TIMEOUT = 1.day
|
2018-09-25 02:18:02 -04:00
|
|
|
BUILD_SCHEDULED_OUTDATED_TIMEOUT = 1.hour
|
2017-02-07 08:00:46 -05:00
|
|
|
BUILD_PENDING_STUCK_TIMEOUT = 1.hour
|
2015-10-21 05:29:47 -04:00
|
|
|
|
|
|
|
def perform
|
2017-02-07 17:06:16 -05:00
|
|
|
return unless try_obtain_lease
|
|
|
|
|
2020-10-08 17:08:52 -04:00
|
|
|
Gitlab::AppLogger.info "#{self.class}: Cleaning stuck builds"
|
2015-10-21 05:29:47 -04:00
|
|
|
|
2018-10-03 05:52:18 -04:00
|
|
|
drop :running, BUILD_RUNNING_OUTDATED_TIMEOUT, 'ci_builds.updated_at < ?', :stuck_or_timeout_failure
|
|
|
|
drop :pending, BUILD_PENDING_OUTDATED_TIMEOUT, 'ci_builds.updated_at < ?', :stuck_or_timeout_failure
|
|
|
|
drop :scheduled, BUILD_SCHEDULED_OUTDATED_TIMEOUT, 'scheduled_at IS NOT NULL AND scheduled_at < ?', :stale_schedule
|
|
|
|
drop_stuck :pending, BUILD_PENDING_STUCK_TIMEOUT, 'ci_builds.updated_at < ?', :stuck_or_timeout_failure
|
2017-02-08 21:29:38 -05:00
|
|
|
|
|
|
|
remove_lease
|
2017-02-07 08:00:46 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2017-02-07 17:06:16 -05:00
|
|
|
def try_obtain_lease
|
2017-03-01 07:56:54 -05:00
|
|
|
@uuid = Gitlab::ExclusiveLease.new(EXCLUSIVE_LEASE_KEY, timeout: 30.minutes).try_obtain
|
2017-02-07 17:06:16 -05:00
|
|
|
end
|
|
|
|
|
2017-02-08 21:29:38 -05:00
|
|
|
def remove_lease
|
|
|
|
Gitlab::ExclusiveLease.cancel(EXCLUSIVE_LEASE_KEY, @uuid)
|
|
|
|
end
|
|
|
|
|
2018-10-03 05:52:18 -04:00
|
|
|
def drop(status, timeout, condition, reason)
|
|
|
|
search(status, timeout, condition) do |build|
|
|
|
|
drop_build :outdated, build, status, timeout, reason
|
2017-02-07 08:00:46 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-10-03 05:52:18 -04:00
|
|
|
def drop_stuck(status, timeout, condition, reason)
|
|
|
|
search(status, timeout, condition) do |build|
|
2018-04-18 05:19:40 -04:00
|
|
|
break unless build.stuck?
|
2017-11-14 04:02:39 -05:00
|
|
|
|
2018-10-03 05:52:18 -04:00
|
|
|
drop_build :stuck, build, status, timeout, reason
|
2015-10-21 05:29:47 -04:00
|
|
|
end
|
2017-02-07 08:00:46 -05:00
|
|
|
end
|
|
|
|
|
2018-09-25 05:44:08 -04:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2018-10-03 05:52:18 -04:00
|
|
|
def search(status, timeout, condition)
|
2017-11-21 13:49:10 -05:00
|
|
|
loop do
|
|
|
|
jobs = Ci::Build.where(status: status)
|
2018-10-03 05:52:18 -04:00
|
|
|
.where(condition, timeout.ago)
|
2020-02-10 13:09:00 -05:00
|
|
|
.includes(:tags, :runner, project: [:namespace, :route])
|
2017-11-21 13:49:10 -05:00
|
|
|
.limit(100)
|
|
|
|
.to_a
|
|
|
|
break if jobs.empty?
|
|
|
|
|
|
|
|
jobs.each do |job|
|
2020-02-10 13:09:00 -05:00
|
|
|
with_context(project: job.project) { yield(job) }
|
2017-11-21 12:44:52 -05:00
|
|
|
end
|
2017-02-07 08:00:46 -05:00
|
|
|
end
|
|
|
|
end
|
2018-09-27 01:02:24 -04:00
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
|
|
|
|
|
|
|
def drop_build(type, build, status, timeout, reason)
|
2020-09-08 05:08:31 -04:00
|
|
|
Gitlab::AppLogger.info "#{self.class}: Dropping #{type} build #{build.id} for runner #{build.runner_id} (status: #{status}, timeout: #{timeout}, reason: #{reason})"
|
2021-03-02 10:10:57 -05:00
|
|
|
Gitlab::OptimisticLocking.retry_lock(build, 3, name: 'stuck_ci_jobs_worker_drop_build') do |b|
|
2018-09-27 01:02:24 -04:00
|
|
|
b.drop(reason)
|
|
|
|
end
|
2019-10-31 20:06:02 -04:00
|
|
|
rescue => ex
|
|
|
|
build.doom!
|
|
|
|
|
|
|
|
track_exception_for_build(ex, build)
|
|
|
|
end
|
|
|
|
|
|
|
|
def track_exception_for_build(ex, build)
|
2019-12-16 07:07:43 -05:00
|
|
|
Gitlab::ErrorTracking.track_exception(ex,
|
2019-10-31 20:06:02 -04:00
|
|
|
build_id: build.id,
|
|
|
|
build_name: build.name,
|
|
|
|
build_stage: build.stage,
|
|
|
|
pipeline_id: build.pipeline_id,
|
|
|
|
project_id: build.project_id
|
2019-12-13 07:07:41 -05:00
|
|
|
)
|
2018-09-27 01:02:24 -04:00
|
|
|
end
|
2015-10-21 05:29:47 -04:00
|
|
|
end
|