Skip to content

Commit 25eadab

Browse files
committed
Extend quota limit to AnswerTopicsJob
We're treating topic tagging as a metric. This means we need to extend the quota to include AnswerTopicsJob so that both jobs implement quota adherence. I've added a shared example to tests related to the quota.
1 parent e7958e3 commit 25eadab

4 files changed

Lines changed: 35 additions & 45 deletions

File tree

app/jobs/answer_analysis/tag_topics_job.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
module AnswerAnalysis
2-
class TagTopicsJob < ApplicationJob
3-
MAX_RETRIES = 5
2+
class TagTopicsJob < BaseJob
43
retry_on Anthropic::Errors::APIError, wait: 1.minute, attempts: MAX_RETRIES
54

65
def perform(answer_id)
@@ -11,6 +10,7 @@ def perform(answer_id)
1110
unless answer.eligible_for_topic_analysis?
1211
return logger.info("Answer #{answer_id} is not eligible for topic analysis")
1312
end
13+
return if quota_limit_reached?
1414

1515
result = AutoEvaluation::TopicTagger.call(answer.rephrased_question || answer.question.message)
1616

spec/jobs/answer_analysis/answer_relevancy_job_spec.rb

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@
1515
allow(AutoEvaluation::AnswerRelevancy)
1616
.to receive(:call).and_return(*results)
1717
stub_const("AnswerAnalysis::BaseMetricJob::NUMBER_OF_RUNS", 3)
18-
allow(Rails.configuration).to receive(:max_auto_evaluation_metrics_per_hour).and_return(2)
1918
end
2019

2120
it_behaves_like "a job in queue", "default"
21+
it_behaves_like "a job that adheres to the metric quota", AutoEvaluation::AnswerRelevancy
2222

2323
describe "#perform" do
2424
it "calls AutoEvaluation::AnswerRelevancy the configured number of times with the correct arguments" do
@@ -56,31 +56,6 @@
5656
end
5757
end
5858

59-
it "writes the auto_evaluation_metrics_run_count cache key on the first metric run" do
60-
expect(Rails.cache).to receive(:write)
61-
.with("auto_evaluation_metrics_run_count", 1, expires_in: 1.hour)
62-
63-
described_class.new.perform(answer.id)
64-
end
65-
66-
it "increments the auto_evaluation_metrics_run_count cache key in subsequent runs" do
67-
allow(Rails.cache).to receive(:read).with("auto_evaluation_metrics_run_count").and_return(1)
68-
expect(Rails.cache).to receive(:increment)
69-
.with("auto_evaluation_metrics_run_count")
70-
71-
described_class.new.perform(answer.id)
72-
end
73-
74-
it "logs info and does not perform evaluation when quota limit is reached" do
75-
allow(Rails.cache).to receive(:read).with("auto_evaluation_metrics_run_count").and_return(2)
76-
expect(described_class.logger)
77-
.to receive(:warn)
78-
.with("Auto-evaluation quota limit of 2 metrics per hour reached")
79-
expect(AutoEvaluation::AnswerRelevancy).not_to receive(:call)
80-
81-
described_class.new.perform(answer.id)
82-
end
83-
8459
context "when the answer has a rephrased_question" do
8560
let(:rephrased_question) { "This is a rephrased_question" }
8661

spec/jobs/answer_analysis/tag_topics_job_spec.rb

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
before { allow(AutoEvaluation::TopicTagger).to receive(:call).and_return(topic_tagger_result) }
2020

2121
it_behaves_like "a job in queue", "default"
22+
it_behaves_like "a job that adheres to the metric quota", AutoEvaluation::TopicTagger
2223

2324
describe "#perform" do
2425
it "calls the AutoEvaluation::TopicTagger with the answer message" do
@@ -78,23 +79,6 @@
7879
end
7980
end
8081

81-
context "when AutoEvaluation::TopicTagger raises an Anthropic::Errors::APIError" do
82-
it "retries the job the max number of times" do
83-
allow(AutoEvaluation::TopicTagger).to receive(:call)
84-
.and_raise(Anthropic::Errors::APIError.new(
85-
url: "url",
86-
))
87-
88-
(described_class::MAX_RETRIES - 1).times do
89-
described_class.perform_later(answer.id)
90-
expect { perform_enqueued_jobs }.not_to raise_error
91-
end
92-
93-
described_class.perform_later(answer.id)
94-
expect { perform_enqueued_jobs }.to raise_error(Anthropic::Errors::APIError)
95-
end
96-
end
97-
9882
context "when the answer is not eligible for topic analysis" do
9983
let(:answer) { create(:answer, status: Answer::STATUSES_EXCLUDED_FROM_TOPIC_ANALYSIS.sample) }
10084

spec/support/job_examples.rb

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,35 @@ module JobExamples
44
expect(described_class.queue_name).to eq(expected_queue)
55
end
66
end
7+
8+
shared_examples "a job that adheres to the metric quota" do |metric|
9+
let(:answer) { create(:answer) }
10+
11+
before { allow(Rails.configuration).to receive(:max_auto_evaluation_metrics_per_hour).and_return(2) }
12+
13+
it "writes the auto_evaluation_metrics_run_count cache key on the first metric run" do
14+
expect(Rails.cache).to receive(:write)
15+
.with("auto_evaluation_metrics_run_count", 1, expires_in: 1.hour)
16+
17+
described_class.new.perform(answer.id)
18+
end
19+
20+
it "increments the auto_evaluation_metrics_run_count cache key in subsequent runs" do
21+
allow(Rails.cache).to receive(:read).with("auto_evaluation_metrics_run_count").and_return(1)
22+
expect(Rails.cache).to receive(:increment)
23+
.with("auto_evaluation_metrics_run_count")
24+
25+
described_class.new.perform(answer.id)
26+
end
27+
28+
it "logs info and does not perform evaluation when quota limit is reached" do
29+
allow(Rails.cache).to receive(:read).with("auto_evaluation_metrics_run_count").and_return(2)
30+
expect(described_class.logger)
31+
.to receive(:warn)
32+
.with("Auto-evaluation quota limit of 2 metrics per hour reached")
33+
expect(metric).not_to receive(:call)
34+
35+
described_class.new.perform(answer.id)
36+
end
37+
end
738
end

0 commit comments

Comments
 (0)