2020-10-26 18:08:27 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module ContainerExpirationPolicies
|
|
|
|
class CleanupService
|
|
|
|
attr_reader :repository
|
|
|
|
|
2021-09-21 15:12:11 +00:00
|
|
|
SERVICE_RESULT_FIELDS = %i[original_size before_truncate_size after_truncate_size before_delete_size deleted_size cached_tags_count].freeze
|
2021-01-11 15:10:32 +00:00
|
|
|
|
2020-10-26 18:08:27 +00:00
|
|
|
def initialize(repository)
|
|
|
|
@repository = repository
|
|
|
|
end
|
|
|
|
|
|
|
|
def execute
|
|
|
|
return ServiceResponse.error(message: 'no repository') unless repository
|
|
|
|
|
2021-05-12 12:10:24 +00:00
|
|
|
unless policy.valid?
|
|
|
|
disable_policy!
|
|
|
|
|
|
|
|
return ServiceResponse.error(message: 'invalid policy')
|
|
|
|
end
|
|
|
|
|
2020-10-26 18:08:27 +00:00
|
|
|
repository.start_expiration_policy!
|
2021-05-12 12:10:24 +00:00
|
|
|
schedule_next_run_if_needed
|
2020-10-26 18:08:27 +00:00
|
|
|
|
2021-02-12 03:08:53 +00:00
|
|
|
begin
|
|
|
|
service_result = Projects::ContainerRepository::CleanupTagsService
|
2021-10-07 12:12:09 +00:00
|
|
|
.new(repository, nil, policy_params.merge('container_expiration_policy' => true))
|
|
|
|
.execute
|
2021-04-26 12:09:44 +00:00
|
|
|
rescue StandardError
|
2021-02-12 03:08:53 +00:00
|
|
|
repository.cleanup_unfinished!
|
|
|
|
|
|
|
|
raise
|
|
|
|
end
|
2020-10-26 18:08:27 +00:00
|
|
|
|
2021-01-11 15:10:32 +00:00
|
|
|
if service_result[:status] == :success
|
2020-10-26 18:08:27 +00:00
|
|
|
repository.update!(
|
|
|
|
expiration_policy_cleanup_status: :cleanup_unscheduled,
|
2020-12-15 12:10:17 +00:00
|
|
|
expiration_policy_completed_at: Time.zone.now
|
2020-10-26 18:08:27 +00:00
|
|
|
)
|
2021-02-12 03:08:53 +00:00
|
|
|
|
2021-01-11 15:10:32 +00:00
|
|
|
success(:finished, service_result)
|
2020-10-26 18:08:27 +00:00
|
|
|
else
|
|
|
|
repository.cleanup_unfinished!
|
|
|
|
|
2021-01-11 15:10:32 +00:00
|
|
|
success(:unfinished, service_result)
|
2020-10-26 18:08:27 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2021-05-12 12:10:24 +00:00
|
|
|
def schedule_next_run_if_needed
|
|
|
|
return if policy.next_run_at.future?
|
|
|
|
|
|
|
|
repos_before_next_run = ::ContainerRepository.for_project_id(policy.project_id)
|
|
|
|
.expiration_policy_started_at_nil_or_before(policy.next_run_at)
|
|
|
|
return if repos_before_next_run.exists?
|
|
|
|
|
|
|
|
policy.schedule_next_run!
|
|
|
|
end
|
|
|
|
|
|
|
|
def disable_policy!
|
|
|
|
policy.disable!
|
|
|
|
repository.cleanup_unscheduled!
|
|
|
|
|
|
|
|
Gitlab::ErrorTracking.log_exception(
|
|
|
|
::ContainerExpirationPolicyWorker::InvalidPolicyError.new,
|
|
|
|
container_expiration_policy_id: policy.id
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2021-01-11 15:10:32 +00:00
|
|
|
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)
|
2020-10-26 18:08:27 +00:00
|
|
|
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
|