diff options
Diffstat (limited to 'app/workers/concerns/packages/cleanup_artifact_worker.rb')
-rw-r--r-- | app/workers/concerns/packages/cleanup_artifact_worker.rb | 60 |
1 files changed, 60 insertions, 0 deletions
diff --git a/app/workers/concerns/packages/cleanup_artifact_worker.rb b/app/workers/concerns/packages/cleanup_artifact_worker.rb new file mode 100644 index 00000000000..db6c7330ea3 --- /dev/null +++ b/app/workers/concerns/packages/cleanup_artifact_worker.rb @@ -0,0 +1,60 @@ +# frozen_string_literal: true + +module Packages + module CleanupArtifactWorker + extend ActiveSupport::Concern + include LimitedCapacity::Worker + include Gitlab::Utils::StrongMemoize + + def perform_work + return unless artifact + + log_metadata(artifact) + + artifact.destroy! + rescue StandardError + artifact&.error! + end + + def remaining_work_count + artifacts_pending_destruction.limit(max_running_jobs + 1).count + end + + private + + def model + raise NotImplementedError + end + + def log_metadata + raise NotImplementedError + end + + def log_cleanup_item + raise NotImplementedError + end + + def artifact + strong_memoize(:artifact) do + model.transaction do + to_delete = next_item + + if to_delete + to_delete.processing! + log_cleanup_item(to_delete) + end + + to_delete + end + end + end + + def artifacts_pending_destruction + model.pending_destruction + end + + def next_item + model.next_pending_destruction(order_by: :updated_at) + end + end +end |