Skip to content

Commit

Permalink
CV2-4233 add retry looper for alegre requests that intermittently fail
Browse files Browse the repository at this point in the history
  • Loading branch information
DGaffney committed Jan 29, 2024
1 parent f5debd5 commit 53b82d7
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
12 changes: 11 additions & 1 deletion app/models/concerns/alegre_v2.rb
Original file line number Diff line number Diff line change
Expand Up @@ -231,14 +231,24 @@ def parse_similarity_results(project_media, field, results, relationship_type)
}.reject{ |k,_| k == project_media.id }]
end

def safe_get_sync(project_media, field, threshold)
response = get_sync(project_media, field, threshold)
retries = 0
while (response == nil or response["result"] == nil) and retries < 3
response = get_sync(project_media, field, threshold)
retries += 1
end
response
end

def get_items(project_media, field, confirmed=false)
relationship_type = confirmed ? Relationship.confirmed_type : Relationship.suggested_type
type = get_type(project_media)
threshold = get_per_model_threshold(project_media, Bot::Alegre.get_threshold_for_query(type, project_media, confirmed))
parse_similarity_results(
project_media,
field,
get_sync(project_media, field, threshold)["result"],
safe_get_sync(project_media, field, threshold)["result"],
relationship_type
)
end
Expand Down
7 changes: 7 additions & 0 deletions test/models/bot/alegre_v2_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,13 @@ def teardown
assert_equal JSON.parse(Bot::Alegre.get_sync(pm1).to_json), JSON.parse(response.to_json)
end

test "should safe_get_sync" do
pm1 = create_project_media team: @team, media: create_uploaded_audio
response = {}
WebMock.stub_request(:post, "#{CheckConfig.get('alegre_host')}/similarity/sync/audio").with(body: {:doc_id=>Bot::Alegre.item_doc_id(pm1), :context=>{:team_id=>pm1.team_id, :project_media_id=>pm1.id, :has_custom_id=>true}, :url=>Bot::Alegre.media_file_url(pm1)}).to_return(body: response.to_json)
assert_equal Bot::Alegre.safe_get_sync(pm1), {}
end

test "should run delete request" do
pm1 = create_project_media team: @team, media: create_uploaded_audio
response = {"requested"=>
Expand Down

0 comments on commit 53b82d7

Please sign in to comment.