Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve error tracking for missing service instance #533

Merged
merged 1 commit into from
Sep 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 29 additions & 5 deletions app/loaders/nitter_loader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,41 @@ class NitterLoader < BaseLoader

# :reek:TooManyStatements
def content
service_instance.update!(used_at: Time.current, usages_count: service_instance.usages_count.succ)
response = http.get(nitter_rss_url.to_s)
pick_service_instance
load_content
end

private

def pick_service_instance
service_instance.update!(
used_at: Time.current,
usages_count: service_instance.usages_count.succ
)
end

def load_content
raise unless response.status.success?
response.to_s
rescue StandardError
Honeybadger.context(nitter_loader: {response: response.as_json, service_instance: service_instance.as_json})
service_instance.register_error
register_error
raise
end

private
def register_error
Honeybadger.context(
nitter_loader: {
response: response.as_json,
service_instance: service_instance.as_json
}
)

service_instance.register_error
end

def response
@response ||= http.get(nitter_rss_url.to_s)
end

def nitter_rss_url
URI.parse(service_instance.url).merge("/#{twitter_user}/rss")
Expand Down
2 changes: 2 additions & 0 deletions app/models/service_instance.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ class ServiceInstance < ApplicationRecord
# timestamp. "Operational" means not disabled or suspended.
def self.pick!(required_type)
operational.where(service_type: required_type).first!
rescue ActiveRecord::RecordNotFound
raise "no available service instances of #{required_type} type"
end

def register_error
Expand Down
1 change: 1 addition & 0 deletions app/services/feeds_configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ def sync
log_info("updating feeds from configuration")
disable_missing_feeds
create_or_update_feeds
log_success("configuration update complete")
end

private
Expand Down
65 changes: 48 additions & 17 deletions spec/loaders/nitter_loader_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,36 +5,67 @@

let(:feed) { build(:feed, options: {"twitter_user" => "username"}) }
let(:body) { "CONTENT BODY" }
let(:service_instance) { create(:service_instance, service_type: "nitter", state: "enabled", url: "https://example.com", usages_count: 0) }

let(:service_instance) do
create(
:service_instance,
service_type: "nitter",
state: "enabled",
url: "https://example.com",
usages_count: 0
)
end

let(:disabled_service_instance) do
create(
:service_instance,
service_type: "nitter",
state: "disabled",
url: "https://example.com",
usages_count: 0
)
end

let(:nitter_url) { "https://example.com/username/rss" }

before do
freeze_time
ServiceInstance.delete_all
service_instance
end

it "fetches nitter url" do
stub_request(:get, nitter_url).to_return(body: body)
expect(body).to eq(load_content)
end
context "with existing service instance" do
before { service_instance }

it "require Twitter user option" do
feed.options = {}
expect { load_content }.to raise_error(KeyError)
end
it "fetches nitter url" do
stub_request(:get, nitter_url).to_return(body: body)
expect(body).to eq(load_content)
end

it "increment service instance usages counter" do
stub_request(:get, nitter_url).to_return(body: body)
expect { load_content }.to(change { service_instance.reload.usages_count }.from(0).to(1))
it "require Twitter user option" do
feed.options = {}
expect { load_content }.to raise_error(KeyError)
end

it "increment service instance usages counter" do
stub_request(:get, nitter_url).to_return(body: body)
expect { load_content }.to(change { service_instance.reload.usages_count }.from(0).to(1))
end

it "updates service instance last usage timestamp" do
stub_request(:get, nitter_url).to_return(body: body)
expect { load_content }.to(change { service_instance.reload.used_at }.from(nil).to(Time.current))
end
end

it "updates service instance last usage timestamp" do
stub_request(:get, nitter_url).to_return(body: body)
expect { load_content }.to(change { service_instance.reload.used_at }.from(nil).to(Time.current))
context "with no available service instances" do
before { disabled_service_instance }

it { expect { load_content }.to raise_error(StandardError) }
end

context "when error" do
context "when loading error" do
before { service_instance }

it "updates service instance state" do
expect_failed_loader_to change { service_instance.reload.state }.from("enabled").to("failed")
end
Expand Down
Loading