Welcome to mirror list, hosted at ThFree Co, Russian Federation.

gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'app/services/ci/pipeline_artifacts/destroy_expired_artifacts_service.rb')
-rw-r--r--app/services/ci/pipeline_artifacts/destroy_expired_artifacts_service.rb52
1 files changed, 52 insertions, 0 deletions
diff --git a/app/services/ci/pipeline_artifacts/destroy_expired_artifacts_service.rb b/app/services/ci/pipeline_artifacts/destroy_expired_artifacts_service.rb
new file mode 100644
index 00000000000..0dbabe178da
--- /dev/null
+++ b/app/services/ci/pipeline_artifacts/destroy_expired_artifacts_service.rb
@@ -0,0 +1,52 @@
+# frozen_string_literal: true
+
+module Ci
+ module PipelineArtifacts
+ class DestroyExpiredArtifactsService
+ include ::Gitlab::LoopHelpers
+ include ::Gitlab::Utils::StrongMemoize
+
+ BATCH_SIZE = 100
+ LOOP_TIMEOUT = 5.minutes
+ LOOP_LIMIT = 1000
+
+ def initialize
+ @removed_artifacts_count = 0
+ end
+
+ def execute
+ loop_until(timeout: LOOP_TIMEOUT, limit: LOOP_LIMIT) do
+ destroy_artifacts_batch
+ end
+
+ @removed_artifacts_count
+ end
+
+ private
+
+ def destroy_artifacts_batch
+ artifacts = ::Ci::PipelineArtifact.expired(BATCH_SIZE).to_a
+ return false if artifacts.empty?
+
+ artifacts.each(&:destroy!)
+ increment_stats(artifacts.size)
+
+ true
+ end
+
+ def increment_stats(size)
+ destroyed_artifacts_counter.increment({}, size)
+ @removed_artifacts_count += size
+ end
+
+ def destroyed_artifacts_counter
+ strong_memoize(:destroyed_artifacts_counter) do
+ name = :destroyed_pipeline_artifacts_count_total
+ comment = 'Counter of destroyed expired pipeline artifacts'
+
+ ::Gitlab::Metrics.counter(name, comment)
+ end
+ end
+ end
+ end
+end