debian-mirror-gitlab/app/workers/concerns/packages/cleanup_artifact_worker.rb

69 lines
1.2 KiB
Ruby
Raw Normal View History

2021-11-18 22:05:49 +05:30
# frozen_string_literal: true
2022-03-02 08:16:31 +05:30
module Packages
module CleanupArtifactWorker
2021-11-18 22:05:49 +05:30
extend ActiveSupport::Concern
2022-03-02 08:16:31 +05:30
include LimitedCapacity::Worker
2021-11-18 22:05:49 +05:30
include Gitlab::Utils::StrongMemoize
def perform_work
return unless artifact
2022-03-02 08:16:31 +05:30
artifact.transaction do
log_metadata(artifact)
2021-11-18 22:05:49 +05:30
2022-03-02 08:16:31 +05:30
artifact.destroy!
rescue StandardError
artifact&.error!
end
2021-11-18 22:05:49 +05:30
2022-03-02 08:16:31 +05:30
after_destroy
2021-11-18 22:05:49 +05:30
end
def remaining_work_count
2022-03-02 08:16:31 +05:30
artifacts_pending_destruction.limit(max_running_jobs + 1).count
2021-11-18 22:05:49 +05:30
end
private
def model
raise NotImplementedError
end
def log_metadata
raise NotImplementedError
end
def log_cleanup_item
raise NotImplementedError
end
2022-03-02 08:16:31 +05:30
def after_destroy
# no op
end
2021-11-18 22:05:49 +05:30
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
2022-03-02 08:16:31 +05:30
def artifacts_pending_destruction
model.pending_destruction
2021-11-18 22:05:49 +05:30
end
def next_item
2022-03-02 08:16:31 +05:30
model.next_pending_destruction(order_by: :updated_at)
2021-11-18 22:05:49 +05:30
end
end
end