2017-03-02 23:43:39 +00:00
|
|
|
class StuckCiJobsWorker
|
2015-10-21 09:29:47 +00:00
|
|
|
include Sidekiq::Worker
|
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
|
|
|
|
BUILD_PENDING_STUCK_TIMEOUT = 1.hour
|
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
|
|
|
|
2017-02-09 02:29:38 +00:00
|
|
|
drop :running, BUILD_RUNNING_OUTDATED_TIMEOUT
|
|
|
|
drop :pending, BUILD_PENDING_OUTDATED_TIMEOUT
|
2017-02-07 13:00:46 +00:00
|
|
|
drop_stuck :pending, BUILD_PENDING_STUCK_TIMEOUT
|
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
|
|
|
|
|
2017-02-07 13:00:46 +00:00
|
|
|
def drop(status, timeout)
|
|
|
|
search(status, timeout) do |build|
|
|
|
|
drop_build :outdated, build, status, timeout
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def drop_stuck(status, timeout)
|
|
|
|
search(status, timeout) do |build|
|
|
|
|
return unless build.stuck?
|
2017-11-14 09:02:39 +00:00
|
|
|
|
2017-02-07 13:00:46 +00:00
|
|
|
drop_build :stuck, build, status, timeout
|
2015-10-21 09:29:47 +00:00
|
|
|
end
|
2017-02-07 13:00:46 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def search(status, timeout)
|
2017-11-21 17:44:52 +00:00
|
|
|
Ci::Build.where(status: status).in_batches(of: 1000) do |batch|
|
|
|
|
batch = batch.where('ci_builds.updated_at < ?', timeout.ago)
|
|
|
|
.joins(:project).merge(Project.without_deleted)
|
|
|
|
.includes(:tags, :runner, project: :namespace)
|
|
|
|
batch.each do |build|
|
|
|
|
yield(build)
|
|
|
|
end
|
2017-02-07 13:00:46 +00:00
|
|
|
end
|
|
|
|
end
|
2015-11-23 12:49:40 +00:00
|
|
|
|
2017-02-07 13:00:46 +00:00
|
|
|
def drop_build(type, build, status, timeout)
|
2017-02-07 22:06:16 +00:00
|
|
|
Rails.logger.info "#{self.class}: Dropping #{type} build #{build.id} for runner #{build.runner_id} (status: #{status}, timeout: #{timeout})"
|
2017-02-07 22:08:02 +00:00
|
|
|
Gitlab::OptimisticLocking.retry_lock(build, 3) do |b|
|
2017-08-31 13:03:41 +00:00
|
|
|
b.drop(:stuck_or_timeout_failure)
|
2017-02-07 22:08:02 +00:00
|
|
|
end
|
2015-10-21 09:29:47 +00:00
|
|
|
end
|
|
|
|
end
|
2017-11-21 17:44:52 +00:00
|
|
|
|