Skip to content

Commit f78409c

Browse files
committed
Prevent broker stuck in state SYNCHRONIZING when db is down before the job starts
1 parent fdf1cfb commit f78409c

File tree

3 files changed

+35
-6
lines changed

3 files changed

+35
-6
lines changed

app/actions/v3/service_broker_update.rb

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,6 @@ def enqueue_update
4545
pollable_job = nil
4646
previous_broker_state = broker.state
4747
ServiceBrokerUpdateRequest.db.transaction do
48-
broker.update(state: ServiceBrokerStateEnum::SYNCHRONIZING)
49-
5048
update_request = ServiceBrokerUpdateRequest.create(params)
5149
MetadataUpdate.update(update_request, message, destroy_nil: false)
5250

app/jobs/v3/services/update_broker_job.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ class Perform
4747
def initialize(update_request_guid, previous_broker_state, user_audit_info:)
4848
@update_request_guid = update_request_guid
4949
@update_request = ServiceBrokerUpdateRequest.find(guid: update_request_guid)
50+
5051
raise 'Service broker update request not found' if @update_request.nil?
5152

5253
@broker = ServiceBroker.find(id: @update_request.service_broker_id)
@@ -57,6 +58,8 @@ def initialize(update_request_guid, previous_broker_state, user_audit_info:)
5758
def perform
5859
raise CloudController::Errors::V3::ApiError.new_from_details('ServiceBrokerGone') if broker.nil?
5960

61+
broker.update(state: ServiceBrokerStateEnum::SYNCHRONIZING)
62+
6063
ServiceBroker.db.transaction do
6164
broker.update(update_params)
6265

spec/unit/actions/v3/service_broker_update_spec.rb

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -166,10 +166,24 @@ module CloudController
166166
).once
167167
end
168168

169-
it 'sets the state to SYNCHRONIZING' do
169+
it 'does not immediately set the state to SYNCHRONIZING' do
170170
action.enqueue_update
171171

172-
expect(existing_service_broker.state).to eq(ServiceBrokerStateEnum::SYNCHRONIZING)
172+
expect(existing_service_broker.state).to eq(ServiceBrokerStateEnum::AVAILABLE)
173+
end
174+
175+
it 'sets the state to SYNCHRONIZING when the job runs' do
176+
action.enqueue_update
177+
178+
broker_state_during_execution = nil
179+
allow_any_instance_of(VCAP::CloudController::V3::ServiceBrokerCatalogUpdater).to receive(:refresh) do
180+
broker_state_during_execution = existing_service_broker.reload.state
181+
nil
182+
end
183+
184+
execute_all_jobs(expected_successes: 1, expected_failures: 0)
185+
expect(broker_state_during_execution).to eq(ServiceBrokerStateEnum::SYNCHRONIZING)
186+
expect(existing_service_broker.reload.state).to eq(ServiceBrokerStateEnum::AVAILABLE)
173187
end
174188

175189
it 'creates an audit event' do
@@ -237,10 +251,24 @@ module CloudController
237251
context 'legacy service brokers' do
238252
let!(:state) { '' } # broker creating from V2 endpoint will not have a state
239253

240-
it 'sets the state to SYNCHRONIZING' do
254+
it 'does not immediately set the state to SYNCHRONIZING' do
241255
action.enqueue_update
242256

243-
expect(existing_service_broker.reload.state).to eq(ServiceBrokerStateEnum::SYNCHRONIZING)
257+
expect(existing_service_broker.reload.state).to eq('')
258+
end
259+
260+
it 'sets the state to SYNCHRONIZING when the job runs' do
261+
action.enqueue_update
262+
263+
broker_state_during_execution = nil
264+
allow_any_instance_of(VCAP::CloudController::V3::ServiceBrokerCatalogUpdater).to receive(:refresh) do
265+
broker_state_during_execution = existing_service_broker.reload.state
266+
nil
267+
end
268+
269+
execute_all_jobs(expected_successes: 1, expected_failures: 0)
270+
expect(broker_state_during_execution).to eq(ServiceBrokerStateEnum::SYNCHRONIZING)
271+
expect(existing_service_broker.reload.state).to eq(ServiceBrokerStateEnum::AVAILABLE)
244272
end
245273

246274
it 'creates and returns a synchronization job' do

0 commit comments

Comments
 (0)