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

feat: cache eresources config in Redis #711

Merged
merged 1 commit into from
Aug 30, 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
62 changes: 12 additions & 50 deletions app/models/eresources.rb
Original file line number Diff line number Diff line change
@@ -1,18 +1,13 @@
# frozen_string_literal: true

require "json"
require "down"
require "fileutils"
require "caching/eresources_cache"

class Eresources
include ActiveModel::Model

attr_accessor :entries

def initialize
@entries = Caching::EresourcesCache.instance.fetch(["eresources_config"], race_condition_ttl: 10.seconds, expires_in: 4.hours) do
fetch_latest_config
@entries = Rails.cache.fetch("eresources_config", expires_in: 4.hours) do
fetch_config
end
end

Expand All @@ -39,51 +34,18 @@ def known_url(url)

private

def fetch_latest_config
Rails.logger.info "Fetching latest eResources config"

config = nil
begin
tempfile = Down.download(ENV["ERESOURCES_CONFIG_URL"], headers: {"User-Agent" => "nla-blacklight/#{Rails.configuration.version}"})
if tempfile.present? && tempfile.status.include?("200") && valid_json?(tempfile)
same = if File.exist? current_config_path
FileUtils.compare_file(current_config_path, tempfile.path)
else
false
end

if same
Rails.logger.info "eResources config has not changed. Keeping current config."
elsif File.exist?(current_config_path) && ((File.size(current_config_path) - File.size(tempfile.path)).abs / File.size(current_config_path) > 0.5)
# compare the filesizes
Rails.logger.error "Suspicious difference in file size between latest and current config. Keeping current config."
else
FileUtils.mv(tempfile.path, current_config_path)
Rails.logger.info "eResources config updated"
end
else
Rails.logger.error "Failed to retrieve latest eResources config. Keeping current config."
end
rescue Down::ServerError
Rails.logger.error "Failed to retrieve latest eResources config. Keeping current config."
ensure
if File.exist? current_config_path
config = JSON.parse File.read(current_config_path)
def fetch_config
res = Faraday.get(ENV["ERESOURCES_CONFIG_URL"], nil, {content_type: "application/json", accept: "application/json"})
if res.status == 200
if res.body.present?
JSON.parse(res.body)
end
end

config
end

def valid_json?(tempfile)
!!begin
JSON.parse(File.read(tempfile))
rescue
else
Rails.logger.error "Failed to retrieve eResources config"
nil
end
end

def current_config_path
"#{ENV.fetch("BLACKLIGHT_TMP_PATH", "./tmp")}/cache/eresources.cfg"
rescue => e
Rails.logger.error "Failed to retrieve eResources config: #{e.message}"
nil
end
end
43 changes: 0 additions & 43 deletions spec/models/eresources_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,35 +28,6 @@
end
end

context "when the eResources manager returns a non-200 status" do
let(:current_config) { [{current_config: true}] }

it "keeps the current config" do
stub_const("ENV", ENV.to_hash.merge("ERESOURCES_CONFIG_URL" => "http://eresource-manager.example.com/service-fail"))

expect(cache.exist?("eresources_config")).to be(false)

# setup the current_config
File.write("#{ENV["BLACKLIGHT_TMP_PATH"]}/cache/eresources.cfg", current_config.to_json)

described_class.new
expect(cache.read("eresources_config")).to eq JSON.parse current_config.to_json
end

# rubocop:disable RSpec/NestedGroups
context "when there is no previous config" do
it "returns an empty array" do
stub_const("ENV", ENV.to_hash.merge("ERESOURCES_CONFIG_URL" => "http://eresource-manager.example.com/service-fail"))

expect(cache.exist?("eresources_config")).to be(false)

described_class.new
expect(cache.read("eresources_config")).to be_nil
end
end
# rubocop:enable RSpec/NestedGroups
end

context "when the latest config is the same as the current config" do
# setup the current_config
let(:current_config) { File.read("spec/files/eresources/config.txt") }
Expand All @@ -71,20 +42,6 @@
expect(cache.read("eresources_config")).to eq JSON.parse current_config
end
end

context "when file size difference is too great" do
let(:current_config) { [{current_config: true}] }

it "keeps the current config" do
expect(cache.exist?("eresources_config")).to be(false)

# setup the current_config
File.write("#{ENV["BLACKLIGHT_TMP_PATH"]}/cache/eresources.cfg", current_config.to_json)

described_class.new
expect(cache.read("eresources_config")).to eq JSON.parse current_config.to_json
end
end
end

describe "#url_append" do
Expand Down
4 changes: 2 additions & 2 deletions spec/support/webmock.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
WebMock.stub_request(:get, "http://eresource-manager.example.com/")
.with(
headers: {
"Accept" => "*/*",
"Accept" => "application/json",
"Accept-Encoding" => "gzip;q=1.0,deflate;q=0.6,identity;q=0.3"
}
)
Expand All @@ -21,7 +21,7 @@
WebMock.stub_request(:get, "http://eresource-manager.example.com/service-fail")
.with(
headers: {
"Accept" => "*/*",
"Accept" => "application/json",
"Accept-Encoding" => "gzip;q=1.0,deflate;q=0.6,identity;q=0.3"
}
)
Expand Down