Class: Packages::MarkPackageFilesForDestructionService

Inherits:
Object
  • Object
show all
Defined in:
app/services/packages/mark_package_files_for_destruction_service.rb

Overview

WARNING: ensure that permissions are verified before using this service.

Constant Summary collapse

BATCH_SIZE =
500

Instance Method Summary collapse

Constructor Details

#initialize(package_files) ⇒ MarkPackageFilesForDestructionService

Returns a new instance of MarkPackageFilesForDestructionService.



8
9
10
# File 'app/services/packages/mark_package_files_for_destruction_service.rb', line 8

def initialize(package_files)
  @package_files = package_files
end

Instance Method Details

#execute(batch_deadline: nil, batch_size: BATCH_SIZE) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'app/services/packages/mark_package_files_for_destruction_service.rb', line 12

def execute(batch_deadline: nil, batch_size: BATCH_SIZE)
  timeout = false
  updates_count = 0
  min_batch_size = [batch_size, BATCH_SIZE].min

  @package_files.each_batch(of: min_batch_size) do |batched_package_files|
    if batch_deadline && Time.zone.now > batch_deadline
      timeout = true
      break
    end

    updates_count += batched_package_files.update_all(status: :pending_destruction)
  end

  payload = { marked_package_files_count: updates_count }

  return response_error(payload) if timeout

  response_success(payload)
end