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
#1786)

* CV2-4233 add retry looper for alegre requests that intermittently fail

* CV2-4233 fix fixture

* update test with right params

* change blah to proper type

* update function based on it being a hash that's actually being passed around

* refactors from PR

* add stub

* fix typo
  • Loading branch information
DGaffney committed Jan 31, 2024
1 parent 049b53e commit 353e0f7
Show file tree
Hide file tree
Showing 4 changed files with 23 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, params={})
response = get_sync(project_media, field, params)
retries = 0
while (response.nil? || response["result"].nil?) && retries < 3
response = get_sync(project_media, field, params)
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
8 changes: 8 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,14 @@ 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
WebMock.stub_request(:post, "#{CheckConfig.get('alegre_host')}/similarity/sync/audio").to_return(body: '{}')
expected = {}
actual = Bot::Alegre.safe_get_sync(pm1, "audio", {})
assert_equal expected, actual
end

test "should run delete request" do
pm1 = create_project_media team: @team, media: create_uploaded_audio
response = {"requested"=>
Expand Down
2 changes: 2 additions & 0 deletions test/models/fact_check_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,11 @@ def setup
end

test "should create fact check without optional fields" do
Bot::Alegre.stubs(:send_field_to_similarity_index).returns({"success": true})
assert_difference 'FactCheck.count' do
create_fact_check url: nil, title: random_string, summary: nil
end
Bot::Alegre.unstub(:send_field_to_similarity_index)
end

test "should not create fact check without user" do
Expand Down
2 changes: 2 additions & 0 deletions test/models/project_media_2_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ def setup
Sidekiq::Testing.inline! do
pm = create_project_media quote: 'Title 0'
assert_equal 'Title 0', pm.title
Bot::Alegre.stubs(:send_field_to_similarity_index).returns({"success": true})
cd = create_claim_description project_media: pm, description: 'Title 1'
assert_queries 0, '=' do
assert_equal 'Title 1', pm.title
Expand All @@ -86,6 +87,7 @@ def setup
assert_queries(0, '>') do
assert_equal 'Title 1', pm.reload.title(true)
end
Bot::Alegre.unstub(:send_field_to_similarity_index)
end
end

Expand Down

0 comments on commit 353e0f7

Please sign in to comment.