summaryrefslogtreecommitdiff
path: root/app/workers/container_expiration_policy_worker.rb
blob: dec13485d137873de6bd584ada1be01b7ceb2723 (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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# frozen_string_literal: true

class ContainerExpirationPolicyWorker # rubocop:disable Scalability/IdempotentWorker
  include ApplicationWorker

  sidekiq_options retry: 3
  include CronjobQueue
  include ExclusiveLeaseGuard

  feature_category :container_registry

  InvalidPolicyError = Class.new(StandardError)

  BATCH_SIZE = 1000

  def perform
    throttling_enabled? ? perform_throttled : perform_unthrottled
  end

  private

  def perform_unthrottled
    with_runnable_policy(preloaded: true) do |policy|
      with_context(project: policy.project,
                   user: policy.project.owner) do |project:, user:|
        ContainerExpirationPolicyService.new(project, user)
                                        .execute(policy)
      end
    end
  end

  def perform_throttled
    try_obtain_lease do
      unless loopless_enabled?
        with_runnable_policy do |policy|
          ContainerExpirationPolicy.transaction do
            policy.schedule_next_run!
            ContainerRepository.for_project_id(policy.id)
                               .each_batch do |relation|
                                 relation.update_all(expiration_policy_cleanup_status: :cleanup_scheduled)
                               end
          end
        end
      end

      ContainerExpirationPolicies::CleanupContainerRepositoryWorker.perform_with_capacity
    end
  end

  # TODO : remove the preload option when cleaning FF container_registry_expiration_policies_throttling
  def with_runnable_policy(preloaded: false)
    ContainerExpirationPolicy.runnable_schedules.each_batch(of: BATCH_SIZE) do |policies|
      # rubocop: disable CodeReuse/ActiveRecord
      cte = Gitlab::SQL::CTE.new(:batched_policies, policies.limit(BATCH_SIZE))
      # rubocop: enable CodeReuse/ActiveRecord
      scope = cte.apply_to(ContainerExpirationPolicy.all).with_container_repositories

      scope = scope.preloaded if preloaded

      scope.each do |policy|
        if policy.valid?
          yield policy
        else
          disable_invalid_policy!(policy)
        end
      end
    end
  end

  def disable_invalid_policy!(policy)
    policy.disable!
    Gitlab::ErrorTracking.log_exception(
      ::ContainerExpirationPolicyWorker::InvalidPolicyError.new,
      container_expiration_policy_id: policy.id
    )
  end

  def throttling_enabled?
    Feature.enabled?(:container_registry_expiration_policies_throttling)
  end

  def loopless_enabled?
    Feature.enabled?(:container_registry_expiration_policies_loopless)
  end

  def lease_timeout
    5.hours
  end
end