Skip to content

Commit 4e8c222

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 e32f332 commit 4e8c222

4 files changed

Lines changed: 35 additions & 27 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.question_used)
1616

spec/jobs/answer_analysis/answer_relevancy_job_spec.rb

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
end
1818

1919
it_behaves_like "a job in queue", "default"
20+
it_behaves_like "a job that adheres to the metric quota", AutoEvaluation::AnswerRelevancy
2021

2122
describe "#perform" do
2223
it "calls AutoEvaluation::AnswerRelevancy the configured number of times with the correct arguments" do
@@ -51,31 +52,6 @@
5152
end
5253
end
5354

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

spec/jobs/answer_analysis/tag_topics_job_spec.rb

Lines changed: 1 addition & 0 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

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)