2018-11-08 19:23:39 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
class StuckCiJobsWorker
|
2018-03-17 18:26:18 +05:30
|
|
|
include ApplicationWorker
|
2017-08-17 22:00:37 +05:30
|
|
|
include CronjobQueue
|
|
|
|
|
2019-12-21 20:55:43 +05:30
|
|
|
feature_category :continuous_integration
|
2019-12-26 22:10:19 +05:30
|
|
|
worker_resource_boundary :cpu
|
2019-12-21 20:55:43 +05:30
|
|
|
|
2019-12-04 20:38:33 +05:30
|
|
|
EXCLUSIVE_LEASE_KEY = 'stuck_ci_builds_worker_lease'
|
2017-08-17 22:00:37 +05:30
|
|
|
|
|
|
|
BUILD_RUNNING_OUTDATED_TIMEOUT = 1.hour
|
|
|
|
BUILD_PENDING_OUTDATED_TIMEOUT = 1.day
|
2018-12-05 23:21:45 +05:30
|
|
|
BUILD_SCHEDULED_OUTDATED_TIMEOUT = 1.hour
|
2017-08-17 22:00:37 +05:30
|
|
|
BUILD_PENDING_STUCK_TIMEOUT = 1.hour
|
|
|
|
|
|
|
|
def perform
|
|
|
|
return unless try_obtain_lease
|
|
|
|
|
2019-09-30 21:07:59 +05:30
|
|
|
Rails.logger.info "#{self.class}: Cleaning stuck builds" # rubocop:disable Gitlab/RailsLogger
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2018-12-05 23:21:45 +05:30
|
|
|
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-08-17 22:00:37 +05:30
|
|
|
|
|
|
|
remove_lease
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def try_obtain_lease
|
|
|
|
@uuid = Gitlab::ExclusiveLease.new(EXCLUSIVE_LEASE_KEY, timeout: 30.minutes).try_obtain
|
|
|
|
end
|
|
|
|
|
|
|
|
def remove_lease
|
|
|
|
Gitlab::ExclusiveLease.cancel(EXCLUSIVE_LEASE_KEY, @uuid)
|
|
|
|
end
|
|
|
|
|
2018-12-05 23:21:45 +05:30
|
|
|
def drop(status, timeout, condition, reason)
|
|
|
|
search(status, timeout, condition) do |build|
|
|
|
|
drop_build :outdated, build, status, timeout, reason
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-12-05 23:21:45 +05:30
|
|
|
def drop_stuck(status, timeout, condition, reason)
|
|
|
|
search(status, timeout, condition) do |build|
|
2018-10-15 14:42:47 +05:30
|
|
|
break unless build.stuck?
|
2018-03-17 18:26:18 +05:30
|
|
|
|
2018-12-05 23:21:45 +05:30
|
|
|
drop_build :stuck, build, status, timeout, reason
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-12-05 23:21:45 +05:30
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
|
|
|
def search(status, timeout, condition)
|
2018-03-17 18:26:18 +05:30
|
|
|
loop do
|
|
|
|
jobs = Ci::Build.where(status: status)
|
2018-12-05 23:21:45 +05:30
|
|
|
.where(condition, timeout.ago)
|
2020-03-09 13:42:32 +05:30
|
|
|
.includes(:tags, :runner, project: [:namespace, :route])
|
2018-03-17 18:26:18 +05:30
|
|
|
.limit(100)
|
|
|
|
.to_a
|
|
|
|
break if jobs.empty?
|
|
|
|
|
|
|
|
jobs.each do |job|
|
2020-03-09 13:42:32 +05:30
|
|
|
with_context(project: job.project) { yield(job) }
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
end
|
2018-12-05 23:21:45 +05:30
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2018-12-05 23:21:45 +05:30
|
|
|
def drop_build(type, build, status, timeout, reason)
|
2019-09-30 21:07:59 +05:30
|
|
|
Rails.logger.info "#{self.class}: Dropping #{type} build #{build.id} for runner #{build.runner_id} (status: #{status}, timeout: #{timeout}, reason: #{reason})" # rubocop:disable Gitlab/RailsLogger
|
2017-08-17 22:00:37 +05:30
|
|
|
Gitlab::OptimisticLocking.retry_lock(build, 3) do |b|
|
2018-12-05 23:21:45 +05:30
|
|
|
b.drop(reason)
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
2019-12-26 22:10:19 +05:30
|
|
|
rescue => ex
|
|
|
|
build.doom!
|
|
|
|
|
|
|
|
track_exception_for_build(ex, build)
|
|
|
|
end
|
|
|
|
|
|
|
|
def track_exception_for_build(ex, build)
|
2020-01-01 13:55:28 +05:30
|
|
|
Gitlab::ErrorTracking.track_exception(ex,
|
2019-12-26 22:10:19 +05:30
|
|
|
build_id: build.id,
|
|
|
|
build_name: build.name,
|
|
|
|
build_stage: build.stage,
|
|
|
|
pipeline_id: build.pipeline_id,
|
|
|
|
project_id: build.project_id
|
2020-01-01 13:55:28 +05:30
|
|
|
)
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
end
|