2018-11-20 20:47:30 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2020-04-08 14:13:33 +05:30
|
|
|
class DeleteContainerRepositoryWorker # rubocop:disable Scalability/IdempotentWorker
|
2018-11-20 20:47:30 +05:30
|
|
|
include ApplicationWorker
|
2021-06-08 01:23:25 +05:30
|
|
|
|
|
|
|
sidekiq_options retry: 3
|
2018-11-20 20:47:30 +05:30
|
|
|
include ExclusiveLeaseGuard
|
|
|
|
|
2019-03-02 22:35:43 +05:30
|
|
|
queue_namespace :container_repository
|
2019-12-21 20:55:43 +05:30
|
|
|
feature_category :container_registry
|
2019-03-02 22:35:43 +05:30
|
|
|
|
2018-11-20 20:47:30 +05:30
|
|
|
LEASE_TIMEOUT = 1.hour
|
|
|
|
|
|
|
|
attr_reader :container_repository
|
|
|
|
|
2018-12-05 23:21:45 +05:30
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2018-11-20 20:47:30 +05:30
|
|
|
def perform(current_user_id, container_repository_id)
|
|
|
|
current_user = User.find_by(id: current_user_id)
|
|
|
|
@container_repository = ContainerRepository.find_by(id: container_repository_id)
|
|
|
|
project = container_repository&.project
|
|
|
|
|
|
|
|
return unless current_user && container_repository && project
|
|
|
|
|
|
|
|
# If a user accidentally attempts to delete the same container registry in quick succession,
|
|
|
|
# this can lead to orphaned tags.
|
|
|
|
try_obtain_lease do
|
|
|
|
Projects::ContainerRepository::DestroyService.new(project, current_user).execute(container_repository)
|
|
|
|
end
|
|
|
|
end
|
2018-12-05 23:21:45 +05:30
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2018-11-20 20:47:30 +05:30
|
|
|
|
|
|
|
# For ExclusiveLeaseGuard concern
|
|
|
|
def lease_key
|
|
|
|
@lease_key ||= "container_repository:delete:#{container_repository.id}"
|
|
|
|
end
|
|
|
|
|
|
|
|
# For ExclusiveLeaseGuard concern
|
|
|
|
def lease_timeout
|
|
|
|
LEASE_TIMEOUT
|
|
|
|
end
|
|
|
|
end
|