discourse-ai/spec/requests/topic_spec.rb
Rafael dos Santos Silva cfc6e388df
FIX: Ensure embeddings database outages are handled gracefully (#80)
The rails_failover middleware will intercept all `PG::ConnectionBad` errors and put the cluster into readonly mode. It does not have any handling for multiple databases. Therefore, an issue with the embeddings database was taking the whole cluster into readonly.

This commit fixes the issue by rescuing `PG::Error` from all AI database accesses, and re-raises errors with a different class. It also adds a spec to ensure that an embeddings database outage does not affect the functionality of the topics/show route.

Co-authored-by: David Taylor <david@taylorhq.com>
2023-05-23 22:57:52 +01:00

55 lines
1.5 KiB
Ruby

# frozen_string_literal: true
require "rails_helper"
describe ::TopicsController do
fab!(:topic) { Fabricate(:topic) }
fab!(:topic1) { Fabricate(:topic) }
fab!(:topic2) { Fabricate(:topic) }
fab!(:topic3) { Fabricate(:topic) }
fab!(:user) { Fabricate(:admin) }
before do
Discourse.cache.clear
SiteSetting.ai_embeddings_semantic_related_topics_enabled = true
SiteSetting.ai_embeddings_semantic_related_topics = 2
end
after { Discourse.cache.clear }
context "when a user is logged on" do
it "includes related topics in payload when configured" do
DiscourseAi::Embeddings::Topic
.any_instance
.expects(:symmetric_semantic_search)
.returns([topic1.id, topic2.id, topic3.id])
get("#{topic.relative_url}.json")
expect(response.status).to eq(200)
json = response.parsed_body
expect(json["suggested_topics"].length).to eq(0)
expect(json["related_topics"].length).to eq(2)
sign_in(user)
get("#{topic.relative_url}.json")
json = response.parsed_body
expect(json["suggested_topics"].length).to eq(0)
expect(json["related_topics"].length).to eq(2)
end
it "excludes embeddings when the database is offline" do
DiscourseAi::Database::Connection.stubs(:db).raises(PG::ConnectionBad)
get "#{topic.relative_url}.json"
expect(response.status).to eq(200)
json = response.parsed_body
expect(json["suggested_topics"].length).not_to eq(0)
expect(json["related_topics"].length).to eq(0)
end
end
end