Skip to content

Commit

Permalink
adding retry_attempted flag so query_dimensions only retries once
Browse files Browse the repository at this point in the history
  • Loading branch information
davidcam-src committed Mar 12, 2024
1 parent 866bc45 commit 1dcfb10
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
7 changes: 6 additions & 1 deletion app/services/tasks/dimensions_query_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ def query_dimensions(with_doi: true, page_size: 100)
token = retrieve_token
doi_clause = with_doi ? 'where doi is not empty' : 'where doi is empty'
cursor = 0
# Flag to track if retry has been attempted after token refresh
retry_attempted = false

loop do
begin
Expand Down Expand Up @@ -47,11 +49,14 @@ def query_dimensions(with_doi: true, page_size: 100)
cursor += page_size

break if cursor >= total_count
elsif response.code == 403
elsif response.code == 403 && !retry_attempted
# If the token has expired, retrieve a new token and try the query again
Rails.logger.warn('Received 403 Forbidden error. Retrying after token refresh.')
token = retrieve_token
redo
elsif response.code != 200 && retry_attempted
# If the token has expired and retry has already been attempted, raise a specific error
raise DimensionsPublicationQueryError, 'Retry attempted after token refresh failed with 403 Forbidden error'
else
raise DimensionsPublicationQueryError, "Failed to retrieve UNC affiliated articles from dimensions. Status code #{response.code}, response body: #{response.body}"
end
Expand Down
18 changes: 18 additions & 0 deletions spec/services/tasks/dimensions_query_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,24 @@
expect(Rails.logger).to have_received(:error).with("HTTParty error during Dimensions API query: Failed to retrieve UNC affiliated articles from dimensions. Status code #{response_status}, response body: #{response_body}")
end

# Checks that the function only retries once to prevent infinite loops
it 'raises and logs an error if the query returns a non 200 status code after a token refresh' do
allow(Rails.logger).to receive(:error)
allow(Rails.logger).to receive(:warn)

unauthorized_status = 403
server_error_status = 500
stub_request(:post, 'https://app.dimensions.ai/api/dsl')
.to_return({status: unauthorized_status, body: 'Unauthorized'}, {status: server_error_status, body: 'Internal Server Error'})

expect { service.query_dimensions }.to raise_error(Tasks::DimensionsQueryService::DimensionsPublicationQueryError)
expect(WebMock).to have_requested(:post, 'https://app.dimensions.ai/api/auth').times(2)
expect(Rails.logger).to have_received(:warn).with('Received 403 Forbidden error. Retrying after token refresh.').once

# Check if the error message has been logged
expect(Rails.logger).to have_received(:error).with("HTTParty error during Dimensions API query: Failed to retrieve UNC affiliated articles from dimensions. Status code #{server_error_status}, response body: Internal Server Error")
end

# Simulating token reretrieval and retry after expiration during query
it 'refreshes the token and retries if query returns a 403' do
allow(Rails.logger).to receive(:warn)
Expand Down

0 comments on commit 1dcfb10

Please sign in to comment.