Skip to content

Commit

Permalink
Add read only mode for maintenance
Browse files Browse the repository at this point in the history
  • Loading branch information
northeastprince committed Feb 14, 2025
1 parent 7dc1005 commit 700236c
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 0 deletions.
1 change: 1 addition & 0 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ class ApplicationController < ActionController::Base
include SetCurrentRequestDetails
include Authenticate
include StreamFlashes
include ReadOnlyMode
end
5 changes: 5 additions & 0 deletions app/controllers/concerns/api/errors.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ module Api::Errors
# Handle any non-custom API errors. These require a custom handler to add
# more context such as the `title` and `detail`.
rescue_from VersionCake::UnsupportedVersionError, with: :unsupported_version
rescue_from ReadOnlyModeError, with: :read_only_mode
rescue_from ActiveRecord::RecordNotFound, with: :not_found
rescue_from ActiveRecord::RecordInvalid, with: :invalid_record
rescue_from ActionController::ParameterMissing, with: :bad_request
Expand All @@ -28,6 +29,10 @@ def unsupported_version(error)
api_error ::Api::InvalidApiVersionError.new(detail:, backtrace: error.backtrace)
end

def read_only_mode
api_error ::Api::ReadOnlyModeError.new
end

def not_found(error)
model_name = error.try(:model) || "object"
id = error.try(:id) || params[:id]
Expand Down
10 changes: 10 additions & 0 deletions app/controllers/concerns/read_only_mode.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module ReadOnlyMode
extend ActiveSupport::Concern

included do
rescue_from ReadOnlyModeError do
stream_flash_notice \
"The app is in read only mode for maintenance. Try again in a few minutes."
end
end
end
11 changes: 11 additions & 0 deletions app/errors/api/read_only_mode_error.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class Api::ReadOnlyModeError < Api::Error
def initialize(
title: "Read Only Mode",
detail: "The app is in read only mode for maintenance. Try again in a few minutes.",
status: :service_unavailable,
type: :not_found_error,
backtrace: nil
)
super
end
end
2 changes: 2 additions & 0 deletions app/errors/read_only_mode_error.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
class ReadOnlyModeError < StandardError
end
19 changes: 19 additions & 0 deletions config/initializers/read_only_mode.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
module ReadOnly
def readonly?
if ENV["READ_ONLY_MODE"]
true
else
super
end
end

def _raise_readonly_record_error
if ENV["READ_ONLY_MODE"]
raise ReadOnlyModeError
else
super
end
end
end

ActiveRecord::Base.prepend ReadOnly
22 changes: 22 additions & 0 deletions test/system/read_only_mode_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
require "application_system_test_case"

class ReadOnlyModeTest < ApplicationSystemTestCase
setup do
ENV["READ_ONLY_MODE"] = "true"
end

teardown do
ENV.delete "READ_ONLY_MODE"
end

test "signing in" do
visit sign_in_path
assert_no_text(/read only/i)

fill_in :email_address, with: users(:matt).email_address
click_on "Send Sign In Link"

assert_text(/read only/i)
assert_not User::Authentication.exists? user: users(:matt)
end
end

0 comments on commit 700236c

Please sign in to comment.