Move status into the settings object

Only the very early boot code needs to look at the value
from the environment directly.
This commit is contained in:
Tom Hughes 2019-03-16 16:29:12 +00:00
parent 6600221fe3
commit 141df02e67
14 changed files with 49 additions and 55 deletions

View file

@ -13,24 +13,7 @@ if File.exist?(File.expand_path("application.yml", __dir__))
end
# rubocop:enable Rails/Output, Rails/Exit
# Set the STATUS constant from the environment, if it matches a recognized value
ALLOWED_STATUS = [
:online, # online and operating normally
:api_readonly, # site online but API in read-only mode
:api_offline, # site online but API offline
:database_readonly, # database and site in read-only mode
:database_offline, # database offline with site in emergency mode
:gpx_offline # gpx storage offline
].freeze
status = if ENV["STATUS"] && ALLOWED_STATUS.include?(ENV["STATUS"].to_sym)
ENV["STATUS"].to_sym
else
:online
end
Object.const_set("STATUS", status)
if STATUS == :database_offline
if ENV["OPENSTREETMAP_STATUS"] == "database_offline"
require "active_model/railtie"
require "active_job/railtie"
require "active_storage/engine"
@ -63,15 +46,15 @@ module OpenStreetMap
# This defaults to true from rails 5.0 but our code doesn't comply
# with it at all so we turn it off
config.active_record.belongs_to_required_by_default = false unless STATUS == :database_offline
config.active_record.belongs_to_required_by_default = false unless Settings.status == "database_offline"
# Use SQL instead of Active Record's schema dumper when creating the database.
# This is necessary if your schema can't be completely dumped by the schema dumper,
# like if you have constraints or database-specific column types
config.active_record.schema_format = :sql unless STATUS == :database_offline
config.active_record.schema_format = :sql unless Settings.status == "database_offline"
# Don't eager load models when the database is offline
config.paths["app/models"].skip_eager_load! if STATUS == :database_offline
config.paths["app/models"].skip_eager_load! if Settings.status == "database_offline"
# Use memcached for caching if required
config.cache_store = :mem_cache_store, Settings.memcache_servers, { :namespace => "rails:cache" } if Settings.key?(:memcache_servers)

View file

@ -39,10 +39,10 @@ Rails.application.configure do
config.active_support.deprecation = :log
# Raise an error on page load if there are pending migrations.
config.active_record.migration_error = :page_load unless STATUS == :database_offline
config.active_record.migration_error = :page_load unless Settings.status == "database_offline"
# Highlight code that triggered database queries in logs.
config.active_record.verbose_query_logs = true unless STATUS == :database_offline
config.active_record.verbose_query_logs = true unless Settings.status == "database_offline"
# Debug mode disables concatenation and preprocessing of assets.
# This option may cause significant delays in view rendering with a large

View file

@ -95,7 +95,7 @@ Rails.application.configure do
end
# Do not dump schema after migrations.
config.active_record.dump_schema_after_migration = false unless STATUS == :database_offline
config.active_record.dump_schema_after_migration = false unless Settings.status == "database_offline"
# Enable autoloading of dependencies.
config.enable_dependency_loading = true

View file

@ -1,3 +1,13 @@
# Allowed status values
ALLOWED_STATUS ||= [
"online", # online and operating normally
"api_readonly", # site online but API in read-only mode
"api_offline", # site online but API offline
"database_readonly", # database and site in read-only mode
"database_offline", # database offline with site in emergency mode
"gpx_offline" # gpx storage offline
].freeze
Config.setup do |config|
# Name of the constant exposing loaded settings
config.const_name = "Settings"
@ -17,23 +27,23 @@ Config.setup do |config|
# Load environment variables from the `ENV` object and override any settings defined in files.
#
# config.use_env = false
config.use_env = true
# Define ENV variable prefix deciding which variables to load into config.
#
# config.env_prefix = 'Settings'
config.env_prefix = "OPENSTREETMAP"
# What string to use as level separator for settings loaded from ENV variables. Default value of '.' works well
# with Heroku, but you might want to change it for example for '__' to easy override settings from command line, where
# using dots in variable names might not be allowed (eg. Bash).
#
# config.env_separator = '.'
config.env_separator = "_"
# Ability to process variables names:
# * nil - no change
# * :downcase - convert to lower case
#
# config.env_converter = :downcase
config.env_converter = :downcase
# Parse numeric values as integers instead of strings.
#
@ -49,5 +59,6 @@ Config.setup do |config|
required(:max_number_of_way_nodes).filled(:int?)
required(:api_timeout).filled(:int?)
required(:imagery_blacklist).maybe(:array?)
required(:status).filled(:str?, :included_in? => ALLOWED_STATUS)
end
end

View file

@ -16,13 +16,13 @@ email_return_path: "openstreetmap@example.com"
# API version
api_version: "0.6"
# Application status - possible values are:
# :online - online and operating normally
# :api_readonly - site online but API in read-only mode
# :api_offline - site online but API offline
# :database_readonly - database and site in read-only mode
# :database_offline - database offline with site in emergency mode
# :gpx_offline - gpx storage offline
status: :online
# online - online and operating normally
# api_readonly - site online but API in read-only mode
# api_offline - site online but API offline
# database_readonly - database and site in read-only mode
# database_offline - database offline with site in emergency mode
# gpx_offline - gpx storage offline
status: "online"
# The maximum area you're allowed to request, in square degrees
max_request_area: 0.25
# Number of GPS trace/trackpoints returned per-page