From 4d4e99a2f163408de44d39ea98131a4231667c24 Mon Sep 17 00:00:00 2001 From: Tomasz Maczukin Date: Fri, 3 Mar 2017 00:43:39 +0100 Subject: Renable StuckCiBuildsWorker to StucjCiJobsWorker --- app/workers/stuck_ci_builds_worker.rb | 59 ----------------------------------- app/workers/stuck_ci_jobs_worker.rb | 59 +++++++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+), 59 deletions(-) delete mode 100644 app/workers/stuck_ci_builds_worker.rb create mode 100644 app/workers/stuck_ci_jobs_worker.rb (limited to 'app/workers') diff --git a/app/workers/stuck_ci_builds_worker.rb b/app/workers/stuck_ci_builds_worker.rb deleted file mode 100644 index 0c51c34a47f..00000000000 --- a/app/workers/stuck_ci_builds_worker.rb +++ /dev/null @@ -1,59 +0,0 @@ -class StuckCiBuildsWorker - include Sidekiq::Worker - include CronjobQueue - - EXCLUSIVE_LEASE_KEY = 'stuck_ci_builds_worker_lease'.freeze - - BUILD_RUNNING_OUTDATED_TIMEOUT = 1.hour - BUILD_PENDING_OUTDATED_TIMEOUT = 1.day - BUILD_PENDING_STUCK_TIMEOUT = 1.hour - - def perform - return unless try_obtain_lease - - Rails.logger.info "#{self.class}: Cleaning stuck builds" - - drop :running, BUILD_RUNNING_OUTDATED_TIMEOUT - drop :pending, BUILD_PENDING_OUTDATED_TIMEOUT - drop_stuck :pending, BUILD_PENDING_STUCK_TIMEOUT - - 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 - - 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? - drop_build :stuck, build, status, timeout - end - end - - def search(status, timeout) - builds = Ci::Build.where(status: status).where('ci_builds.updated_at < ?', timeout.ago) - builds.joins(:project).includes(:tags, :runner, project: :namespace).find_each(batch_size: 50).each do |build| - yield(build) - end - end - - def drop_build(type, build, status, timeout) - Rails.logger.info "#{self.class}: Dropping #{type} build #{build.id} for runner #{build.runner_id} (status: #{status}, timeout: #{timeout})" - Gitlab::OptimisticLocking.retry_lock(build, 3) do |b| - b.drop - end - end -end diff --git a/app/workers/stuck_ci_jobs_worker.rb b/app/workers/stuck_ci_jobs_worker.rb new file mode 100644 index 00000000000..ae8c980c9e4 --- /dev/null +++ b/app/workers/stuck_ci_jobs_worker.rb @@ -0,0 +1,59 @@ +class StuckCiJobsWorker + include Sidekiq::Worker + include CronjobQueue + + EXCLUSIVE_LEASE_KEY = 'stuck_ci_builds_worker_lease'.freeze + + BUILD_RUNNING_OUTDATED_TIMEOUT = 1.hour + BUILD_PENDING_OUTDATED_TIMEOUT = 1.day + BUILD_PENDING_STUCK_TIMEOUT = 1.hour + + def perform + return unless try_obtain_lease + + Rails.logger.info "#{self.class}: Cleaning stuck builds" + + drop :running, BUILD_RUNNING_OUTDATED_TIMEOUT + drop :pending, BUILD_PENDING_OUTDATED_TIMEOUT + drop_stuck :pending, BUILD_PENDING_STUCK_TIMEOUT + + 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 + + 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? + drop_build :stuck, build, status, timeout + end + end + + def search(status, timeout) + builds = Ci::Build.where(status: status).where('ci_builds.updated_at < ?', timeout.ago) + builds.joins(:project).includes(:tags, :runner, project: :namespace).find_each(batch_size: 50).each do |build| + yield(build) + end + end + + def drop_build(type, build, status, timeout) + Rails.logger.info "#{self.class}: Dropping #{type} build #{build.id} for runner #{build.runner_id} (status: #{status}, timeout: #{timeout})" + Gitlab::OptimisticLocking.retry_lock(build, 3) do |b| + b.drop + end + end +end -- cgit v1.2.1