2018-11-08 19:23:39 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2020-04-08 14:13:33 +05:30
|
|
|
class StuckCiJobsWorker # rubocop:disable Scalability/IdempotentWorker
|
2018-03-17 18:26:18 +05:30
|
|
|
include ApplicationWorker
|
2021-06-08 01:23:25 +05:30
|
|
|
|
|
|
|
sidekiq_options retry: 3
|
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
|
2021-09-04 01:27:46 +05:30
|
|
|
BUILD_LOOKBACK = 5.days
|
2017-08-17 22:00:37 +05:30
|
|
|
|
|
|
|
def perform
|
|
|
|
return unless try_obtain_lease
|
|
|
|
|
2021-01-03 14:25:43 +05:30
|
|
|
Gitlab::AppLogger.info "#{self.class}: Cleaning stuck builds"
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2021-09-04 01:27:46 +05:30
|
|
|
drop(running_timed_out_builds, failure_reason: :stuck_or_timeout_failure)
|
|
|
|
|
|
|
|
drop(
|
|
|
|
Ci::Build.pending.updated_before(lookback: BUILD_LOOKBACK.ago, timeout: BUILD_PENDING_OUTDATED_TIMEOUT.ago),
|
|
|
|
failure_reason: :stuck_or_timeout_failure
|
|
|
|
)
|
|
|
|
|
|
|
|
drop(scheduled_timed_out_builds, failure_reason: :stale_schedule)
|
|
|
|
|
|
|
|
drop_stuck(
|
|
|
|
Ci::Build.pending.updated_before(lookback: BUILD_LOOKBACK.ago, timeout: BUILD_PENDING_STUCK_TIMEOUT.ago),
|
|
|
|
failure_reason: :stuck_or_timeout_failure
|
|
|
|
)
|
2017-08-17 22:00:37 +05:30
|
|
|
|
|
|
|
remove_lease
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2021-09-04 01:27:46 +05:30
|
|
|
def scheduled_timed_out_builds
|
|
|
|
Ci::Build.where(status: :scheduled).where( # rubocop: disable CodeReuse/ActiveRecord
|
|
|
|
'ci_builds.scheduled_at IS NOT NULL AND ci_builds.scheduled_at < ?',
|
|
|
|
BUILD_SCHEDULED_OUTDATED_TIMEOUT.ago
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
def running_timed_out_builds
|
|
|
|
Ci::Build.running.where( # rubocop: disable CodeReuse/ActiveRecord
|
|
|
|
'ci_builds.updated_at < ?',
|
|
|
|
BUILD_RUNNING_OUTDATED_TIMEOUT.ago
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
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
|
|
|
|
|
2021-09-04 01:27:46 +05:30
|
|
|
def drop(builds, failure_reason:)
|
|
|
|
fetch(builds) do |build|
|
|
|
|
drop_build :outdated, build, failure_reason
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-09-04 01:27:46 +05:30
|
|
|
def drop_stuck(builds, failure_reason:)
|
|
|
|
fetch(builds) do |build|
|
2018-10-15 14:42:47 +05:30
|
|
|
break unless build.stuck?
|
2018-03-17 18:26:18 +05:30
|
|
|
|
2021-09-04 01:27:46 +05:30
|
|
|
drop_build :stuck, build, failure_reason
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-12-05 23:21:45 +05:30
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2021-09-04 01:27:46 +05:30
|
|
|
def fetch(builds)
|
2018-03-17 18:26:18 +05:30
|
|
|
loop do
|
2021-09-04 01:27:46 +05:30
|
|
|
jobs = builds.includes(:tags, :runner, project: [:namespace, :route])
|
2018-03-17 18:26:18 +05:30
|
|
|
.limit(100)
|
|
|
|
.to_a
|
2021-09-04 01:27:46 +05:30
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
break if jobs.empty?
|
|
|
|
|
|
|
|
jobs.each do |job|
|
2020-03-13 15:44:24 +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
|
|
|
|
2021-09-04 01:27:46 +05:30
|
|
|
def drop_build(type, build, reason)
|
|
|
|
Gitlab::AppLogger.info "#{self.class}: Dropping #{type} build #{build.id} for runner #{build.runner_id} (status: #{build.status}, failure_reason: #{reason})"
|
2021-04-17 20:07:23 +05:30
|
|
|
Gitlab::OptimisticLocking.retry_lock(build, 3, name: 'stuck_ci_jobs_worker_drop_build') do |b|
|
2018-12-05 23:21:45 +05:30
|
|
|
b.drop(reason)
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
2021-06-08 01:23:25 +05:30
|
|
|
rescue StandardError => ex
|
2019-12-26 22:10:19 +05:30
|
|
|
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
|