debian-mirror-gitlab/app/services/ci/destroy_expired_job_artifacts_service.rb

52 lines
1.5 KiB
Ruby
Raw Normal View History

2019-03-02 22:35:43 +05:30
# frozen_string_literal: true
module Ci
class DestroyExpiredJobArtifactsService
include ::Gitlab::ExclusiveLeaseHelpers
include ::Gitlab::LoopHelpers
BATCH_SIZE = 100
LOOP_TIMEOUT = 45.minutes
LOOP_LIMIT = 1000
EXCLUSIVE_LOCK_KEY = 'expired_job_artifacts:destroy:lock'
LOCK_TIMEOUT = 50.minutes
##
# Destroy expired job artifacts on GitLab instance
#
# This destroy process cannot run for more than 45 minutes. This is for
# preventing multiple `ExpireBuildArtifactsWorker` CRON jobs run concurrently,
# which is scheduled at every hour.
def execute
in_lock(EXCLUSIVE_LOCK_KEY, ttl: LOCK_TIMEOUT, retries: 1) do
loop_until(timeout: LOOP_TIMEOUT, limit: LOOP_LIMIT) do
2020-11-24 15:15:51 +05:30
destroy_batch(Ci::JobArtifact) || destroy_batch(Ci::PipelineArtifact)
2019-03-02 22:35:43 +05:30
end
end
end
private
2020-11-24 15:15:51 +05:30
def destroy_batch(klass)
artifact_batch = if klass == Ci::JobArtifact
klass.expired(BATCH_SIZE).unlocked
2020-05-24 23:13:21 +05:30
else
2020-11-24 15:15:51 +05:30
klass.expired(BATCH_SIZE)
2020-05-24 23:13:21 +05:30
end
artifacts = artifact_batch.to_a
2019-03-02 22:35:43 +05:30
return false if artifacts.empty?
artifacts.each(&:destroy!)
2021-01-03 14:25:43 +05:30
run_after_destroy(artifacts)
true # This is required because of the design of `loop_until` method.
2019-03-02 22:35:43 +05:30
end
2021-01-03 14:25:43 +05:30
def run_after_destroy(artifacts); end
2019-03-02 22:35:43 +05:30
end
end
2021-01-03 14:25:43 +05:30
Ci::DestroyExpiredJobArtifactsService.prepend_if_ee('EE::Ci::DestroyExpiredJobArtifactsService')