Skip to content

Commit

Permalink
Use read only mode for database maintenance windows
Browse files Browse the repository at this point in the history
closes #2648
  • Loading branch information
hackartisan committed Aug 12, 2021
1 parent db13d87 commit 681cea7
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 24 deletions.
15 changes: 9 additions & 6 deletions app/controllers/account_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,12 @@
class AccountController < ApplicationController
include ApplicationHelper

before_action :read_only_redirect
before_action :require_user_authentication_provider
before_action :verify_user, except: [:borrow_direct_redirect]

def index
if Orangelight.read_only_mode
msg = "Account page is disabled. #{Orangelight.read_only_message}"
redirect_to root_path, flash: { notice: msg }
else
redirect_to digitization_requests_path
end
redirect_to digitization_requests_path
end

def digitization_requests
Expand Down Expand Up @@ -42,6 +38,13 @@ def cancel_ill_requests

protected

def read_only_redirect
if Orangelight.read_only_mode
flash[:notice] = 'Account login unavailable during maintenace.'
redirect_to(root_url) && return
end
end

def verify_user
unless current_user
flash[:error] = I18n.t('blacklight.saved_searches.need_login') &&
Expand Down
6 changes: 5 additions & 1 deletion app/views/account/_login_links.html.erb
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
<% if !current_user %>
<div class='card'>
<div class='card-body'>
To view previously saved <%= feature_page %>, please <%= link_to "log in", main_app.new_user_session_path(origin: request.fullpath) %> to your catalog account.
<% if Orangelight.read_only_mode %>
Previously saved <%= feature_page %> unavailable during maintenance.
<% else %>
To view previously saved <%= feature_page %>, please <%= link_to "log in", main_app.new_user_session_path(origin: request.fullpath) %> to your catalog account.
<% end %>
</div>
</div>
<% end %>
15 changes: 14 additions & 1 deletion config/initializers/read_only_mode.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,22 @@ def read_only_mode
end

def read_only_message
default_msg = "Catalog is in read-only mode."
default_msg = "The Library Catalog is undergoing maintenance. Bookmarks, digitization requests, and saved searches are currently unavailable."
@read_only_message ||= ENV.fetch("OL_READ_ONLY_MESSAGE", default_msg)
end

module_function :read_only_mode, :read_only_message
end

# Block database writes in read_only mode
if Orangelight.read_only_mode
class ActiveRecord::Base
before_save do
raise ActiveRecord::Rollback, "Read-only"
end

before_destroy do
raise ActiveRecord::Rollback, "Read-only"
end
end
end
9 changes: 3 additions & 6 deletions spec/controllers/account_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,16 @@
.to_return(status: 200, body: verify_user_response)
end

describe '#index' do
describe '#digitization_requests' do
context 'when Orangelight is in read only mode' do
let(:valid_user) { FactoryBot.create(:valid_princeton_patron) }

before do
sign_in(valid_user)
allow(Orangelight).to receive(:read_only_mode).and_return(true)
end

it 'redirects to root and flashes an explanatory message' do
get :index
get :digitization_requests
expect(response).to redirect_to(root_path)
expect(flash[:notice]).to include("Account page is disabled", "read-only")
expect(flash[:notice]).to include("Account login unavailable during maintenace.")
end
end
end
Expand Down
11 changes: 11 additions & 0 deletions spec/features/bookmarks_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,17 @@
end
end

context 'when orangelight is in readonly mode' do
it 'has a maintenance message' do
allow(Orangelight).to receive(:read_only_mode).and_return(true)
visit '/bookmarks'
within('#content') do
expect(page).not_to have_link("log in")
expect(page).to have_content("unavailable during maintenance")
end
end
end

context 'when logged in' do
let(:user) { FactoryBot.create(:user) }

Expand Down
10 changes: 0 additions & 10 deletions spec/features/request_options_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,6 @@
end
end

describe 'the request button when orangelight is in read-only mode', js: true do
it 'does display a request button' do
allow(Orangelight).to receive(:read_only_mode).and_return(true)
visit '/catalog/9618072'
using_wait_time 5 do
expect(page).to have_selector('td[data-requestable="true"]')
end
end
end

describe 'Available status non-requestable location', js: true do
before do
visit '/catalog/9222024'
Expand Down

0 comments on commit 681cea7

Please sign in to comment.