Class: ContainerExpirationPolicies::CleanupService

Inherits:
Object
  • Object
show all
Defined in:
app/services/container_expiration_policies/cleanup_service.rb

Constant Summary collapse

SERVICE_RESULT_FIELDS =
%i[original_size before_truncate_size after_truncate_size before_delete_size deleted_size cached_tags_count].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(repository) ⇒ CleanupService

Returns a new instance of CleanupService.


9
10
11
# File 'app/services/container_expiration_policies/cleanup_service.rb', line 9

def initialize(repository)
  @repository = repository
end

Instance Attribute Details

#repositoryObject (readonly)

Returns the value of attribute repository.


5
6
7
# File 'app/services/container_expiration_policies/cleanup_service.rb', line 5

def repository
  @repository
end

Instance Method Details

#executeObject


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
39
40
41
42
43
44
45
46
47
48
# File 'app/services/container_expiration_policies/cleanup_service.rb', line 13

def execute
  return ServiceResponse.error(message: 'no repository') unless repository

  unless policy.valid?
    disable_policy!

    return ServiceResponse.error(message: 'invalid policy')
  end

  repository.start_expiration_policy!
  schedule_next_run_if_needed

  begin
    service_result = Projects::ContainerRepository::CleanupTagsService
                       .new(repository, nil, policy_params.merge('container_expiration_policy' => true))
                       .execute
  rescue StandardError
    repository.cleanup_unfinished!

    raise
  end

  if service_result[:status] == :success
    repository.update!(
      expiration_policy_cleanup_status: :cleanup_unscheduled,
      expiration_policy_completed_at: Time.zone.now,
      last_cleanup_deleted_tags_count: service_result[:deleted_size]
    )

    success(:finished, service_result)
  else
    repository.cleanup_unfinished!

    success(:unfinished, service_result)
  end
end