summaryrefslogtreecommitdiff
path: root/app/workers/packages/cleanup/execute_policy_worker.rb
blob: 59f0f0250c89eb033bc34629477f08d5f78ed029 (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
64
65
66
67
68
69
70
71
72
# frozen_string_literal: true

module Packages
  module Cleanup
    class ExecutePolicyWorker
      include ApplicationWorker
      include LimitedCapacity::Worker
      include Gitlab::Utils::StrongMemoize

      data_consistency :always
      queue_namespace :package_cleanup
      feature_category :package_registry
      urgency :low
      worker_resource_boundary :unknown
      idempotent!

      COUNTS_KEYS = %i[
        marked_package_files_total_count
        unique_package_id_and_file_name_total_count
      ].freeze

      def perform_work
        return unless next_policy

        log_extra_metadata_on_done(:project_id, next_policy.project_id)
        result = ::Packages::Cleanup::ExecutePolicyService.new(next_policy).execute

        if result.success?
          timeout = !!result.payload[:timeout]
          counts = result.payload[:counts]
          log_extra_metadata_on_done(:execution_timeout, timeout)
          COUNTS_KEYS.each do |count_key|
            log_extra_metadata_on_done(count_key, counts[count_key])
          end
        end
      end

      def remaining_work_count
        ::Packages::Cleanup::Policy.runnable
                                   .limit(max_running_jobs + 1)
                                   .count
      end

      def max_running_jobs
        ::Gitlab::CurrentSettings.package_registry_cleanup_policies_worker_capacity
      end

      private

      def next_policy
        strong_memoize(:next_policy) do
          ::Packages::Cleanup::Policy.transaction do
            # the #lock call is specific to this worker
            # rubocop: disable CodeReuse/ActiveRecord
            policy = ::Packages::Cleanup::Policy.runnable
                                                .limit(1)
                                                .lock('FOR UPDATE SKIP LOCKED')
                                                .first
            # rubocop: enable CodeReuse/ActiveRecord

            next nil unless policy

            policy.set_next_run_at
            policy.save!

            policy
          end
        end
      end
    end
  end
end