File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 11module AnswerAnalysis
22 class AnswerTopicsJob < BaseMetricJob
3- MAX_RETRIES = 5
43 retry_on Anthropic ::Errors ::APIError , wait : 1 . minute , attempts : MAX_RETRIES
54
65 def perform ( answer_id )
Original file line number Diff line number Diff line change 5757
5858 it_behaves_like "a job in queue" , "default"
5959 it_behaves_like "a job that adheres to the metric quota" , AutoEvaluation ::AnswerRelevancy
60+ it_behaves_like "a job that retries on service errors" , Aws ::Errors ::ServiceError do
61+ before do
62+ allow ( AutoEvaluation ::AnswerRelevancy ) . to receive ( :call )
63+ . and_raise (
64+ Aws ::Errors ::ServiceError . new ( nil , "error" ) ,
65+ )
66+ end
67+ end
6068
6169 describe "#perform" do
6270 it "calls AutoEvaluation::AnswerRelevancy the configured number of times with the correct arguments" do
169177 end
170178 end
171179
172- context "when the AnswerRelevancy metric raises an Aws::Errors::ServiceError" do
173- it "retries the job the max number of times" do
174- allow ( AutoEvaluation ::AnswerRelevancy ) . to receive ( :call )
175- . and_raise ( Aws ::Errors ::ServiceError . new ( nil , "error" ) )
176-
177- ( described_class ::MAX_RETRIES - 1 ) . times do
178- described_class . perform_later ( answer . id )
179- expect { perform_enqueued_jobs } . not_to raise_error
180- end
181-
182- described_class . perform_later ( answer . id )
183- expect { perform_enqueued_jobs } . to raise_error ( Aws ::Errors ::ServiceError )
184- end
185- end
186-
187180 context "when the answer is not eligible for auto-evaluation" do
188181 let ( :answer ) { create ( :answer , status : Answer . statuses . except ( :answered ) . keys . sample ) }
189182
Original file line number Diff line number Diff line change 2020
2121 it_behaves_like "a job in queue" , "default"
2222 it_behaves_like "a job that adheres to the metric quota" , TopicTagger
23+ it_behaves_like "a job that retries on service errors" , Anthropic ::Errors ::APIError do
24+ before do
25+ allow ( TopicTagger ) . to receive ( :call )
26+ . and_raise ( Anthropic ::Errors ::APIError . new ( url : "url" ) )
27+ end
28+ end
2329
2430 describe "#perform" do
2531 it "calls the TopicTagger with the answer message" do
7985 end
8086 end
8187
82- context "when TopicTagger raises an Anthropic::Errors::APIError" do
83- it "retries the job the max number of times" do
84- allow ( TopicTagger ) . to receive ( :call )
85- . and_raise ( Anthropic ::Errors ::APIError . new (
86- url : "url" ,
87- ) )
88-
89- ( described_class ::MAX_RETRIES - 1 ) . times do
90- described_class . perform_later ( answer . id )
91- expect { perform_enqueued_jobs } . not_to raise_error
92- end
93-
94- described_class . perform_later ( answer . id )
95- expect { perform_enqueued_jobs } . to raise_error ( Anthropic ::Errors ::APIError )
96- end
97- end
98-
9988 context "when the answer is not eligible for topic analysis" do
10089 let ( :answer ) { create ( :answer , status : Answer ::STATUSES_EXCLUDED_FROM_TOPIC_ANALYSIS . sample ) }
10190
Original file line number Diff line number Diff line change @@ -35,4 +35,17 @@ module JobExamples
3535 described_class . new . perform ( answer . id )
3636 end
3737 end
38+
39+ shared_examples "a job that retries on service errors" do |error_class |
40+ let ( :answer ) { create ( :answer ) }
41+ it "retries the job the max number of times on #{ error_class } " do
42+ ( described_class ::MAX_RETRIES - 1 ) . times do
43+ described_class . perform_later ( answer . id )
44+ expect { perform_enqueued_jobs } . not_to raise_error
45+ end
46+
47+ described_class . perform_later ( answer . id )
48+ expect { perform_enqueued_jobs } . to raise_error ( error_class )
49+ end
50+ end
3851end
You can’t perform that action at this time.
0 commit comments