debian-mirror-gitlab/app/services/container_expiration_policies/cleanup_service.rb

73 lines
1.8 KiB
Ruby
Raw Normal View History

2021-01-29 00:20:46 +05:30
# frozen_string_literal: true
module ContainerExpirationPolicies
class CleanupService
attr_reader :repository
2021-03-11 19:13:27 +05:30
SERVICE_RESULT_FIELDS = %i[original_size before_truncate_size after_truncate_size before_delete_size deleted_size].freeze
2021-03-08 18:12:59 +05:30
2021-01-29 00:20:46 +05:30
def initialize(repository)
@repository = repository
end
def execute
return ServiceResponse.error(message: 'no repository') unless repository
repository.start_expiration_policy!
2021-03-11 19:13:27 +05:30
begin
service_result = Projects::ContainerRepository::CleanupTagsService
.new(project, nil, policy_params.merge('container_expiration_policy' => true))
.execute(repository)
rescue
repository.cleanup_unfinished!
raise
end
2021-01-29 00:20:46 +05:30
2021-03-08 18:12:59 +05:30
if service_result[:status] == :success
2021-01-29 00:20:46 +05:30
repository.update!(
expiration_policy_cleanup_status: :cleanup_unscheduled,
2021-02-22 17:27:13 +05:30
expiration_policy_started_at: nil,
expiration_policy_completed_at: Time.zone.now
2021-01-29 00:20:46 +05:30
)
2021-03-11 19:13:27 +05:30
2021-03-08 18:12:59 +05:30
success(:finished, service_result)
2021-01-29 00:20:46 +05:30
else
repository.cleanup_unfinished!
2021-03-08 18:12:59 +05:30
success(:unfinished, service_result)
2021-01-29 00:20:46 +05:30
end
end
private
2021-03-08 18:12:59 +05:30
def success(cleanup_status, service_result)
payload = {
cleanup_status: cleanup_status,
container_repository_id: repository.id
}
SERVICE_RESULT_FIELDS.each do |field|
payload["cleanup_tags_service_#{field}".to_sym] = service_result[field]
end
ServiceResponse.success(message: "cleanup #{cleanup_status}", payload: payload)
2021-01-29 00:20:46 +05:30
end
def policy_params
return {} unless policy
policy.policy_params
end
def policy
project.container_expiration_policy
end
def project
repository&.project
end
end
end