From 5a44b7df1ecb503294b7504b9f7e8bb6cad7c0a4 Mon Sep 17 00:00:00 2001 From: Philipp Thun Date: Thu, 24 Feb 2022 18:06:54 +0100 Subject: [PATCH] wip --- app/jobs/v3/create_service_instance_job.rb | 44 ++++++++++++---------- 1 file changed, 24 insertions(+), 20 deletions(-) diff --git a/app/jobs/v3/create_service_instance_job.rb b/app/jobs/v3/create_service_instance_job.rb index 21ab632476f..fdae57d1a3e 100644 --- a/app/jobs/v3/create_service_instance_job.rb +++ b/app/jobs/v3/create_service_instance_job.rb @@ -4,6 +4,10 @@ module VCAP::CloudController module V3 class CreateServiceInstanceJob < VCAP::CloudController::Jobs::ReoccurringJob + class ServiceInstanceNotFound < CloudController::Errors::ApiError; end + + class OperationInProgress < CloudController::Errors::ApiError; end + attr_reader :warnings def initialize( @@ -50,9 +54,9 @@ def resource_guid end def perform - not_found! unless service_instance + not_found! unless get_service_instance - raise_if_other_operations_in_progress! + raise_if_other_operation_in_progress! compute_maximum_duration @@ -67,13 +71,13 @@ def perform polling_status = action.poll(service_instance) if polling_status[:finished] - finish + return finish end if polling_status[:retry_after].present? self.polling_interval_seconds = polling_status[:retry_after] end - rescue ServiceInstanceCreateManaged::LastOperationFailedState => e + rescue ServiceInstanceNotFound, OperationInProgress, ServiceInstanceCreateManaged::LastOperationFailedState => e raise e rescue CloudController::Errors::ApiError => e save_failure(e) @@ -115,20 +119,28 @@ def route_services_disabled? private - attr_reader :user_audit_info + def get_service_instance # rubocop:disable Naming/AccessorMethodName + @service_instance = ManagedServiceInstance.first(guid: resource_guid) + end def service_instance - ManagedServiceInstance.first(guid: @service_instance_guid) + @service_instance || get_service_instance end - def raise_if_other_operations_in_progress! - last_operation_type = service_instance.last_operation&.type + def not_found! + raise ServiceInstanceNotFound.new_from_details('ResourceNotFound', "The service instance could not be found: #{resource_guid}.") + end - return if operation_type == 'delete' && last_operation_type == 'create' + def raise_if_other_operation_in_progress! + return unless service_instance.operation_in_progress? - if service_instance.operation_in_progress? && last_operation_type != operation_type - cancelled!(last_operation_type) - end + last_operation_type = service_instance.last_operation.type + return if last_operation_type == operation_type + + # cancelling is allowed + return if last_operation_type == 'create' && operation_type == 'delete' + + raise OperationInProgress.new_from_details('UnableToPerform', operation_type, "#{last_operation_type} in progress") end def compute_maximum_duration @@ -148,14 +160,6 @@ def save_failure(error_message) ) end end - - def not_found! - raise CloudController::Errors::ApiError.new_from_details('ResourceNotFound', "The service instance could not be found: #{@service_instance_guid}.") - end - - def cancelled!(operation_in_progress) - raise CloudController::Errors::ApiError.new_from_details('UnableToPerform', operation_type, "#{operation_in_progress} in progress") - end end end end