|
| 1 | +require 'jobs/v2/services/asynchronous_operations' |
| 2 | +require 'jobs/cc_job' |
| 3 | + |
| 4 | +module VCAP::CloudController |
| 5 | + module V3 |
| 6 | + class FetchLastOperationJob < VCAP::CloudController::Jobs::CCJob |
| 7 | + include VCAP::CloudController::Jobs::Services::AsynchronousOperations |
| 8 | + |
| 9 | + attr_accessor :service_instance_guid, :request_attrs, :poll_interval, :end_timestamp |
| 10 | + |
| 11 | + def initialize(service_instance_guid:, request_attrs:, end_timestamp: nil, pollable_job_guid:) |
| 12 | + @service_instance_guid = service_instance_guid |
| 13 | + @request_attrs = request_attrs |
| 14 | + @end_timestamp = end_timestamp || new_end_timestamp |
| 15 | + @pollable_job_guid = pollable_job_guid |
| 16 | + update_polling_interval |
| 17 | + end |
| 18 | + |
| 19 | + def success(_) |
| 20 | + service_instance = ManagedServiceInstance.first(guid: service_instance_guid) |
| 21 | + |
| 22 | + if service_instance.terminal_state? |
| 23 | + pollable_job.update(state: PollableJobModel::COMPLETE_STATE) |
| 24 | + else |
| 25 | + try_again |
| 26 | + end |
| 27 | + end |
| 28 | + |
| 29 | + def error(job, exception) |
| 30 | + wrapper = VCAP::CloudController::Jobs::PollableJobWrapper.new(job) |
| 31 | + wrapper.failure(job) |
| 32 | + wrapper.error(job, exception) |
| 33 | + end |
| 34 | + |
| 35 | + def perform |
| 36 | + service_instance = ManagedServiceInstance.first(guid: service_instance_guid) |
| 37 | + gone! if service_instance.nil? |
| 38 | + |
| 39 | + intended_operation = service_instance.last_operation |
| 40 | + aborted! if intended_operation.type != 'create' |
| 41 | + |
| 42 | + client = VCAP::Services::ServiceClientProvider.provide(instance: service_instance) |
| 43 | + |
| 44 | + last_operation_result = client.fetch_service_instance_last_operation(service_instance) |
| 45 | + update_with_attributes(last_operation_result[:last_operation], service_instance, intended_operation) |
| 46 | + |
| 47 | + @retry_after = last_operation_result[:retry_after] |
| 48 | + rescue HttpRequestError, HttpResponseError, Sequel::Error => e |
| 49 | + logger = Steno.logger('cc-background') |
| 50 | + logger.error("There was an error while fetching the service instance operation state: #{e}") |
| 51 | + @retry_after = nil |
| 52 | + try_again |
| 53 | + end |
| 54 | + |
| 55 | + def max_attempts |
| 56 | + 1 |
| 57 | + end |
| 58 | + |
| 59 | + private |
| 60 | + |
| 61 | + def gone! |
| 62 | + raise CloudController::Errors::ApiError.new_from_details('ServiceInstanceNotFound', @service_instance_guid) |
| 63 | + end |
| 64 | + |
| 65 | + def aborted! |
| 66 | + raise CloudController::Errors::ApiError.new_from_details('UnableToPerform', 'Create', 'delete in progress') |
| 67 | + end |
| 68 | + |
| 69 | + def pollable_job |
| 70 | + PollableJobModel.where(guid: @pollable_job_guid) |
| 71 | + end |
| 72 | + |
| 73 | + def try_again |
| 74 | + delayed_job = retry_job(retry_after_header: @retry_interval) |
| 75 | + pollable_job.update( |
| 76 | + state: PollableJobModel::POLLING_STATE, |
| 77 | + delayed_job_guid: delayed_job.guid |
| 78 | + ) |
| 79 | + end |
| 80 | + |
| 81 | + def update_with_attributes(last_operation, service_instance, intended_operation) |
| 82 | + ServiceInstance.db.transaction do |
| 83 | + service_instance.lock! |
| 84 | + return unless intended_operation == service_instance.last_operation |
| 85 | + |
| 86 | + service_instance.save_and_update_operation( |
| 87 | + last_operation: last_operation.slice(:state, :description) |
| 88 | + ) |
| 89 | + |
| 90 | + if service_instance.last_operation.state == 'succeeded' |
| 91 | + apply_proposed_changes(service_instance) |
| 92 | + end |
| 93 | + end |
| 94 | + end |
| 95 | + |
| 96 | + def end_timestamp_reached |
| 97 | + ManagedServiceInstance.first(guid: service_instance_guid).save_and_update_operation( |
| 98 | + last_operation: { |
| 99 | + state: 'failed', |
| 100 | + description: 'Service Broker failed to provision within the required time.', |
| 101 | + } |
| 102 | + ) |
| 103 | + end |
| 104 | + |
| 105 | + def apply_proposed_changes(service_instance) |
| 106 | + if service_instance.last_operation.type == 'delete' |
| 107 | + service_instance.last_operation.destroy |
| 108 | + service_instance.destroy |
| 109 | + else |
| 110 | + service_instance.save_and_update_operation(service_instance.last_operation.proposed_changes) |
| 111 | + end |
| 112 | + end |
| 113 | + |
| 114 | + def service_plan |
| 115 | + ManagedServiceInstance.first(guid: service_instance_guid).try(:service_plan) |
| 116 | + rescue Sequel::Error => e |
| 117 | + Steno.logger('cc-background').error("There was an error while fetching the service instance: #{e}") |
| 118 | + nil |
| 119 | + end |
| 120 | + end |
| 121 | + |
| 122 | + class ServiceInstanceGoneError < StandardError |
| 123 | + end |
| 124 | + end |
| 125 | +end |
0 commit comments