summaryrefslogtreecommitdiff
path: root/app/workers/concerns/dependency_proxy/cleanup_worker.rb
blob: b668634f233f8226de72c272a5d9d303b6327c7b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# frozen_string_literal: true

module DependencyProxy
  module CleanupWorker
    extend ActiveSupport::Concern
    include Gitlab::Utils::StrongMemoize

    def perform_work
      return unless artifact

      log_metadata(artifact)

      artifact.destroy!
    rescue StandardError
      artifact&.error!
    end

    def max_running_jobs
      ::Gitlab::CurrentSettings.dependency_proxy_ttl_group_policy_worker_capacity
    end

    def remaining_work_count
      expired_artifacts.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 expired_artifacts
      model.expired
    end

    def next_item
      expired_artifacts.lock_next_by(:updated_at).first
    end
  end
end