Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
philippthun committed Feb 24, 2022
1 parent 8f3b181 commit 5a44b7d
Showing 1 changed file with 24 additions and 20 deletions.
44 changes: 24 additions & 20 deletions app/jobs/v3/create_service_instance_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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

Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand All @@ -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

0 comments on commit 5a44b7d

Please sign in to comment.