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

57 lines
1.7 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
2021-01-29 00:20:46 +05:30
LOOP_TIMEOUT = 5.minutes
2019-03-02 22:35:43 +05:30
LOOP_LIMIT = 1000
EXCLUSIVE_LOCK_KEY = 'expired_job_artifacts:destroy:lock'
2021-01-29 00:20:46 +05:30
LOCK_TIMEOUT = 6.minutes
2019-03-02 22:35:43 +05:30
2021-03-08 18:12:59 +05:30
def initialize
@removed_artifacts_count = 0
end
2019-03-02 22:35:43 +05:30
##
# Destroy expired job artifacts on GitLab instance
#
2021-01-29 00:20:46 +05:30
# This destroy process cannot run for more than 6 minutes. This is for
2019-03-02 22:35:43 +05:30
# preventing multiple `ExpireBuildArtifactsWorker` CRON jobs run concurrently,
2021-01-29 00:20:46 +05:30
# which is scheduled every 7 minutes.
2019-03-02 22:35:43 +05:30
def execute
in_lock(EXCLUSIVE_LOCK_KEY, ttl: LOCK_TIMEOUT, retries: 1) do
2021-03-08 18:12:59 +05:30
destroy_job_artifacts_with_slow_iteration(Time.current)
2019-03-02 22:35:43 +05:30
end
2021-03-08 18:12:59 +05:30
@removed_artifacts_count
2021-01-29 00:20:46 +05:30
end
2021-03-08 18:12:59 +05:30
private
2019-03-02 22:35:43 +05:30
2021-03-08 18:12:59 +05:30
def destroy_job_artifacts_with_slow_iteration(start_at)
Ci::JobArtifact.expired_before(start_at).each_batch(of: BATCH_SIZE, column: :expire_at, order: :desc) do |relation, index|
2021-04-17 20:07:23 +05:30
# For performance reasons, join with ci_pipelines after the batch is queried.
# See: https://gitlab.com/gitlab-org/gitlab/-/merge_requests/47496
artifacts = relation.unlocked
service_response = destroy_batch_async(artifacts)
@removed_artifacts_count += service_response[:destroyed_artifacts_count]
2021-01-03 14:25:43 +05:30
2021-03-08 18:12:59 +05:30
break if loop_timeout?(start_at)
break if index >= LOOP_LIMIT
end
2021-01-29 00:20:46 +05:30
end
2021-04-17 20:07:23 +05:30
def destroy_batch_async(artifacts)
Ci::JobArtifactsDestroyBatchService.new(artifacts).execute
2021-01-29 00:20:46 +05:30
end
2021-03-08 18:12:59 +05:30
def loop_timeout?(start_at)
Time.current > start_at + LOOP_TIMEOUT
end
2019-03-02 22:35:43 +05:30
end
end