Skip to content
This repository was archived by the owner on Jun 2, 2021. It is now read-only.

Commit 7220f9c

Browse files
author
Derik Evangelista
authored
v3(services): fix job state when deleting orphans (cloudfoundry#1749)
Co-authored-by: Brian Butz <bbutz@pivotal.io> [finishes #171727934](https://www.pivotaltracker.com/story/show/171727934)
1 parent 515d6c0 commit 7220f9c

5 files changed

Lines changed: 58 additions & 5 deletions

File tree

app/jobs/pollable_job_wrapper.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,10 @@ def initialize(handler, existing_guid: nil)
1313
# use custom hook as Job does not have the guid field populated during the normal `enqueue` hook
1414
def after_enqueue(job)
1515
if existing_guid && (existing = PollableJobModel.find(guid: existing_guid))
16+
state = @handler.try(:pollable_job_state) || PollableJobModel::POLLING_STATE
1617
existing.update(
1718
delayed_job_guid: job.guid,
18-
state: PollableJobModel::POLLING_STATE,
19+
state: state,
1920
operation: @handler.display_name,
2021
resource_guid: @handler.resource_guid,
2122
resource_type: @handler.resource_type

app/jobs/v3/delete_service_instance_job.rb

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,17 @@ class DeprovisionBadResponse < StandardError
77

88
class DeleteServiceInstanceJob < ServiceInstanceAsyncJob
99
def initialize(guid, audit_info)
10-
super(guid, audit_info)
10+
super
1111
end
1212

1313
def send_broker_request(client)
14-
client.deprovision(service_instance, { accepts_incomplete: true })
14+
deprovision_response = client.deprovision(service_instance, { accepts_incomplete: true })
15+
16+
@request_failed = false
17+
18+
deprovision_response
1519
rescue VCAP::Services::ServiceBrokers::V2::Errors::ServiceBrokerBadResponse => err
20+
@request_failed = true
1621
raise DeprovisionBadResponse.new(err.message)
1722
end
1823

@@ -40,6 +45,12 @@ def restart_on_failure?
4045
true
4146
end
4247

48+
def pollable_job_state
49+
return PollableJobModel::PROCESSING_STATE if @request_failed
50+
51+
PollableJobModel::POLLING_STATE
52+
end
53+
4354
def restart_job(msg)
4455
super
4556
logger.info("could not complete the operation: #{msg}. Triggering orphan mitigation")

spec/request/service_instances_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2831,7 +2831,7 @@ def check_filtered_instances(*instances)
28312831
api_call.call(admin_headers)
28322832
execute_all_jobs(expected_successes: 1, expected_failures: 0)
28332833

2834-
expect(job.state).to eq(VCAP::CloudController::PollableJobModel::POLLING_STATE)
2834+
expect(job.state).to eq(VCAP::CloudController::PollableJobModel::PROCESSING_STATE)
28352835
end
28362836

28372837
it 'retries the deprovision' do
@@ -2875,7 +2875,7 @@ def check_filtered_instances(*instances)
28752875
api_call.call(admin_headers)
28762876
execute_all_jobs(expected_successes: 1, expected_failures: 0)
28772877

2878-
expect(job.state).to eq(VCAP::CloudController::PollableJobModel::POLLING_STATE)
2878+
expect(job.state).to eq(VCAP::CloudController::PollableJobModel::PROCESSING_STATE)
28792879
end
28802880

28812881
it 'retries the deprovision' do

spec/unit/jobs/pollable_job_wrapper_spec.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,20 @@ class BigException < StandardError
5959
expect(job_record.resource_type).to eq('droplet')
6060
expect(job_record.cf_api_error).to be_nil
6161
end
62+
63+
context 'when the job defines its state' do
64+
before do
65+
job.define_singleton_method(:pollable_job_state) do
66+
'ABRACADABRA'
67+
end
68+
end
69+
70+
it 'updates the existing job state accordingly' do
71+
enqueued_job = VCAP::CloudController::Jobs::Enqueuer.new(pollable_job).enqueue
72+
job_record = VCAP::CloudController::PollableJobModel.find(delayed_job_guid: enqueued_job.guid)
73+
expect(job_record.state).to eq('ABRACADABRA')
74+
end
75+
end
6276
end
6377

6478
context 'when the job fails' do

spec/unit/jobs/v3/delete_service_instance_job_spec.rb

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,22 @@ module V3
2828
allow(VCAP::Services::ServiceClientProvider).to receive(:provide).and_return(client)
2929
end
3030

31+
context 'when the client succeeds' do
32+
let(:r) do
33+
VCAP::Services::ServiceBrokers::V2::HttpResponse.new(code: '204', body: 'all good')
34+
end
35+
36+
before do
37+
allow(client).to receive(:deprovision).and_return(r)
38+
end
39+
40+
it 'the pollable job state is set to polling' do
41+
subject.perform
42+
43+
expect(subject.pollable_job_state).to eq(PollableJobModel::POLLING_STATE)
44+
end
45+
end
46+
3147
context 'when the client raises a ServiceBrokerBadResponse' do
3248
let(:r) do
3349
VCAP::Services::ServiceBrokers::V2::HttpResponse.new(code: '204', body: 'unexpected failure!')
@@ -48,6 +64,12 @@ module V3
4864
expect(subject.instance_variable_get(:@first_time)).to eq(true)
4965
end
5066

67+
it 'the pollable job state is set to processing' do
68+
subject.perform
69+
70+
expect(subject.pollable_job_state).to eq(PollableJobModel::PROCESSING_STATE)
71+
end
72+
5173
it 'does not modify the service instance operation' do
5274
service_instance.save_with_new_operation(
5375
{},
@@ -126,6 +148,11 @@ module V3
126148
expect(response).to eq('some response')
127149
end
128150

151+
it 'sets the @request_failed to false' do
152+
subject.send_broker_request(client)
153+
expect(subject.instance_variable_get(:@request_failed)).to eq(false)
154+
end
155+
129156
context 'when the client raises a ServiceBrokerBadResponse' do
130157
it 'raises a DeprovisionBadResponse error' do
131158
r = VCAP::Services::ServiceBrokers::V2::HttpResponse.new(code: '204', body: 'unexpected failure!')

0 commit comments

Comments
 (0)