Class: Packages::MarkPackagesForDestructionService

Inherits:
Object
  • Object
show all
Includes:
BaseServiceUtility
Defined in:
app/services/packages/mark_packages_for_destruction_service.rb

Constant Summary collapse

BATCH_SIZE =
20
UNAUTHORIZED_RESPONSE =
ServiceResponse.error(
  message: "You don't have the permission to perform this action",
  reason: :unauthorized
).freeze
ERROR_RESPONSE =
ServiceResponse.error(
  message: 'Failed to mark the packages as pending destruction'
).freeze
SUCCESS_RESPONSE =
ServiceResponse.success(
  message: 'Packages were successfully marked as pending destruction'
).freeze

Instance Method Summary collapse

Methods included from BaseServiceUtility

#deny_visibility_level, #event_service, #log_error, #log_info, #notification_service, #system_hook_service, #todo_service, #visibility_level

Methods included from Gitlab::Allowable

#can?

Constructor Details

#initialize(packages:, current_user: nil) ⇒ MarkPackagesForDestructionService

Initialize this service with the given packages and user.

  • ‘packages`: must be an ActiveRecord relationship.

  • ‘current_user`: an User object. Could be nil.



26
27
28
29
# File 'app/services/packages/mark_packages_for_destruction_service.rb', line 26

def initialize(packages:, current_user: nil)
  @packages = packages
  @current_user = current_user
end

Instance Method Details

#execute(batch_size: BATCH_SIZE) ⇒ Object



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
# File 'app/services/packages/mark_packages_for_destruction_service.rb', line 31

def execute(batch_size: BATCH_SIZE)
  no_access = false
  min_batch_size = [batch_size, BATCH_SIZE].min
  package_ids = []

  @packages.each_batch(of: min_batch_size) do |batched_packages|
    loaded_packages = batched_packages.including_project_route.to_a
    package_ids = loaded_packages.map(&:id)

    break no_access = true unless can_destroy_packages?(loaded_packages)

    ::Packages::Package.id_in(package_ids)
                       .update_all(status: :pending_destruction)

    (loaded_packages)
    mark_package_files_for_destruction(loaded_packages)
  end

  return UNAUTHORIZED_RESPONSE if no_access

  SUCCESS_RESPONSE
rescue StandardError => e
  track_exception(e, package_ids)
  ERROR_RESPONSE
end