debian-mirror-gitlab/app/workers/container_expiration_policy_worker.rb

88 lines
2.5 KiB
Ruby
Raw Normal View History

2020-03-13 15:44:24 +05:30
# frozen_string_literal: true
2020-04-08 14:13:33 +05:30
class ContainerExpirationPolicyWorker # rubocop:disable Scalability/IdempotentWorker
2020-03-13 15:44:24 +05:30
include ApplicationWorker
include CronjobQueue
2021-01-29 00:20:46 +05:30
include ExclusiveLeaseGuard
2020-03-13 15:44:24 +05:30
feature_category :container_registry
2021-01-29 00:20:46 +05:30
InvalidPolicyError = Class.new(StandardError)
2021-04-29 21:17:54 +05:30
BATCH_SIZE = 1000
2021-01-29 00:20:46 +05:30
2020-03-13 15:44:24 +05:30
def perform
2021-01-29 00:20:46 +05:30
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
2021-04-29 21:17:54 +05:30
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
2021-01-03 14:25:43 +05:30
end
2020-03-13 15:44:24 +05:30
end
2021-01-29 00:20:46 +05:30
ContainerExpirationPolicies::CleanupContainerRepositoryWorker.perform_with_capacity
2020-03-13 15:44:24 +05:30
end
end
2021-01-29 00:20:46 +05:30
# 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
2021-04-29 21:17:54 +05:30
def loopless_enabled?
Feature.enabled?(:container_registry_expiration_policies_loopless)
end
2021-01-29 00:20:46 +05:30
def lease_timeout
5.hours
end
2020-03-13 15:44:24 +05:30
end