summaryrefslogtreecommitdiff
path: root/app/workers/delete_container_repository_worker.rb
blob: 42e66513ff1d59b082bac61e0fb150f802aacf8c (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
# frozen_string_literal: true

class DeleteContainerRepositoryWorker
  include ApplicationWorker
  include ExclusiveLeaseGuard

  queue_namespace :container_repository

  LEASE_TIMEOUT = 1.hour

  attr_reader :container_repository

  # rubocop: disable CodeReuse/ActiveRecord
  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
  # rubocop: enable CodeReuse/ActiveRecord

  # 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