Skip to content

Commit

Permalink
Cache the mediaflux good status for 5 minutes on the health.json page (
Browse files Browse the repository at this point in the history
  • Loading branch information
carolyncole authored Dec 18, 2024
1 parent 39305c1 commit b50125e
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 9 deletions.
18 changes: 9 additions & 9 deletions app/models/mediaflux_status.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ def check!
# Notice that we check Mediaflux status using our TigerData account
# (rather than the "logged in" user since there is not always a logged
# in user for the health check)
domain = Rails.configuration.mediaflux["api_domain"]
user = Rails.configuration.mediaflux["api_user"]
password = Rails.configuration.mediaflux["api_password"]
logon_request = Mediaflux::LogonRequest.new(domain:, user:, password:)
session_token = logon_request.session_token
if logon_request.error?
raise logon_request.response_error[:message]
else
Mediaflux::LogoutRequest.new(session_token:)
Rails.cache.fetch("mediaflux_health_session", expires_in: 5.minutes) do
logon_request = Mediaflux::LogonRequest.new
session_token = logon_request.session_token
if logon_request.error?
raise logon_request.response_error[:message]
else
Mediaflux::LogoutRequest.new(session_token:)
end
session_token
end
end
end
18 changes: 18 additions & 0 deletions spec/models/mediaflux_status_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,23 @@
require "rails_helper"

RSpec.describe MediafluxStatus, connect_to_mediaflux: true, type: :model do
let(:memory_store) { ActiveSupport::Cache.lookup_store(:memory_store) }

before do
allow(Mediaflux::LogonRequest).to receive(:new).and_call_original
allow(Rails).to receive(:cache).and_return(memory_store)
Rails.cache.clear
end

context "when Mediaflux is up and running" do
it "reports status OK" do
status = described_class.new
expect { status.check! }.not_to raise_error

# Does not make a second call immediately
expect { status.check! }.not_to raise_error

expect(Mediaflux::LogonRequest).to have_received(:new).once
end
end

Expand All @@ -20,6 +33,11 @@
it "reports that the status is not OK" do
status = described_class.new
expect { status.check! }.to raise_error(StandardError)

# Does make a second call immediately on error
expect { status.check! }.to raise_error(StandardError)

expect(Mediaflux::LogonRequest).to have_received(:new).twice
end
end
end

0 comments on commit b50125e

Please sign in to comment.