Skip to content

Commit

Permalink
Fix Puma config, initialization
Browse files Browse the repository at this point in the history
  • Loading branch information
Dantemss committed Dec 19, 2024
1 parent e4c008c commit 463a94d
Show file tree
Hide file tree
Showing 8 changed files with 124 additions and 128 deletions.
4 changes: 3 additions & 1 deletion config/database.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
default: &default
adapter: postgresql
host: <%= ENV['OXE_DB_HOST'] || 'localhost' %>
port: <%= ENV['OXE_DB_PORT'] || 5432 %>
database: <%= ENV['OXE_DB'] || 'ox_exercises_dev' %>
username: <%= ENV['OXE_DB_USER'] || 'ox_exercises' %>
password: <%= ENV['OXE_DB_PASS'] || 'ox_exercises' %>
pool: <%= ENV.fetch('RAILS_MAX_THREADS', 5).to_i %>
Expand All @@ -10,6 +10,7 @@ development:
<<: *default
host: <%= ENV['OXE_DEV_DB_HOST'] || 'localhost' %>
database: <%= ENV['OXE_DEV_DB'] || 'ox_exercises_dev' %>
<% if RUBY_PLATFORM =~ /darwin/ %>gssencmode: disable<% end %> # Prevent OS X OBJC crash

# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
Expand All @@ -19,6 +20,7 @@ test:
host: <%= ENV['OXE_TEST_DB_HOST'] || 'localhost' %>
database: '<%= ENV['OXE_TEST_DB'] || 'ox_exercises_test' %><%= \
"_#{ENV['TEST_ENV_NUMBER']}" if !ENV['TEST_ENV_NUMBER'].blank? %>'
<% if RUBY_PLATFORM =~ /darwin/ %>gssencmode: disable<% end %> # Prevent OS X OBJC crash
reaping_frequency: 0 # 0 = disabled - incompatible with our DatabaseCleaner config

production:
Expand Down
30 changes: 16 additions & 14 deletions config/initializers/access_policies.rb
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
require_relative 'doorkeeper'

# User
OSU::AccessPolicy.register(User, UserAccessPolicy)
OSU::AccessPolicy.register(Delegation, DelegationAccessPolicy)
Rails.application.config.after_initialize do
# User
OSU::AccessPolicy.register(User, UserAccessPolicy)
OSU::AccessPolicy.register(Delegation, DelegationAccessPolicy)

# API
OSU::AccessPolicy.register(Exercise, ExerciseAccessPolicy)
OSU::AccessPolicy.register(VocabTerm, VocabTermAccessPolicy)
OSU::AccessPolicy.register(Attachment, AttachmentAccessPolicy)
OSU::AccessPolicy.register(List, ListAccessPolicy)
OSU::AccessPolicy.register(ListPublicationGroup, ListPublicationGroupAccessPolicy)
OSU::AccessPolicy.register(Publication, PublicationAccessPolicy)
OSU::AccessPolicy.register(CommunitySolution, CommunitySolutionAccessPolicy)
OSU::AccessPolicy.register(OpenStax::Content::Book, BookAccessPolicy)
# API
OSU::AccessPolicy.register(Exercise, ExerciseAccessPolicy)
OSU::AccessPolicy.register(VocabTerm, VocabTermAccessPolicy)
OSU::AccessPolicy.register(Attachment, AttachmentAccessPolicy)
OSU::AccessPolicy.register(List, ListAccessPolicy)
OSU::AccessPolicy.register(ListPublicationGroup, ListPublicationGroupAccessPolicy)
OSU::AccessPolicy.register(Publication, PublicationAccessPolicy)
OSU::AccessPolicy.register(CommunitySolution, CommunitySolutionAccessPolicy)
OSU::AccessPolicy.register(OpenStax::Content::Book, BookAccessPolicy)

# Doorkeeper
OSU::AccessPolicy.register(Doorkeeper::Application, Doorkeeper::ApplicationAccessPolicy)
# Doorkeeper
OSU::AccessPolicy.register(Doorkeeper::Application, Doorkeeper::ApplicationAccessPolicy)
end
26 changes: 14 additions & 12 deletions config/initializers/controllers.rb
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
ActionController::Base.class_exec do
protect_from_forgery
ActiveSupport.on_load(:action_controller_base) do
ActionController::Base.class_exec do
protect_from_forgery

layout "layouts/application_body_only"
layout "layouts/application_body_only"

helper ApplicationHelper
helper OpenStax::Utilities::OsuHelper
helper ApplicationHelper
helper OpenStax::Utilities::OsuHelper

helper_method :current_account
helper_method :current_account

protected
protected

def current_administrator
Administrator.where(:account_id => current_account.id).first
end
def current_administrator
Administrator.where(:account_id => current_account.id).first
end

def authenticate_administrator!
current_administrator || raise(SecurityTransgression)
def authenticate_administrator!
current_administrator || raise(SecurityTransgression)
end
end
end
28 changes: 15 additions & 13 deletions config/initializers/doorkeeper_models.rb
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
require_relative 'doorkeeper'

Doorkeeper::Application.class_exec do
belongs_to :owner, polymorphic: true
Rails.application.config.to_prepare do
Doorkeeper::Application.class_exec do
belongs_to :owner, polymorphic: true

def is_human?
false
end
def is_human?
false
end

def is_application?
true
end
def is_application?
true
end

def is_anonymous?
false
end
def is_anonymous?
false
end

def is_administrator?
false
def is_administrator?
false
end
end
end
69 changes: 35 additions & 34 deletions config/initializers/models.rb
Original file line number Diff line number Diff line change
@@ -1,51 +1,52 @@
ActiveRecord::Base.class_exec do
ActiveSupport.on_load(:active_record) do
ActiveRecord::Base.class_exec do
def association_attributes(associations = [],
slice: nil,
except: nil,
exclude_foreign_keys: false,
transform_arrays_into_sets: false)
attrs = attributes
attrs = attrs.slice(*slice) unless slice.blank?
attrs = attrs.except(*except) unless except.blank?

def association_attributes(associations = [],
slice: nil,
except: nil,
exclude_foreign_keys: false,
transform_arrays_into_sets: false)
attrs = attributes
attrs = attrs.slice(*slice) unless slice.blank?
attrs = attrs.except(*except) unless except.blank?

if exclude_foreign_keys
foreign_keys = self.class.reflect_on_all_associations(:belongs_to).map do |ar|
ar.foreign_key
if exclude_foreign_keys
foreign_keys = self.class.reflect_on_all_associations(:belongs_to).map do |ar|
ar.foreign_key
end
attrs = attrs.except(*foreign_keys)
end
attrs = attrs.except(*foreign_keys)
end

[associations].flatten.compact.each_with_object(attrs) do |association, hash|
association = [association].flatten.compact unless association.respond_to?(:each)
[associations].flatten.compact.each_with_object(attrs) do |association, hash|
association = [association].flatten.compact unless association.respond_to?(:each)

association.each do |name, subtree|
objects = send(name)
next if objects.nil?

association.each do |name, subtree|
objects = send(name)
next if objects.nil?
if objects.respond_to?(:collect)
hash[name.to_s] = objects.collect do |object|
object.association_attributes(
subtree,
slice: slice,
except: except,
exclude_foreign_keys: exclude_foreign_keys,
transform_arrays_into_sets: transform_arrays_into_sets
)
end

if objects.respond_to?(:collect)
hash[name.to_s] = objects.collect do |object|
object.association_attributes(
hash[name.to_s] = Set.new(hash[name.to_s]) if transform_arrays_into_sets
else
hash[name.to_s] = objects.association_attributes(
subtree,
slice: slice,
except: except,
exclude_foreign_keys: exclude_foreign_keys,
transform_arrays_into_sets: transform_arrays_into_sets
)
end

hash[name.to_s] = Set.new(hash[name.to_s]) if transform_arrays_into_sets
else
hash[name.to_s] = objects.association_attributes(
subtree,
slice: slice,
except: except,
exclude_foreign_keys: exclude_foreign_keys,
transform_arrays_into_sets: transform_arrays_into_sets
)
end
end
end
end

end
end
22 changes: 14 additions & 8 deletions config/initializers/sentry.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
Raven.configure do |config|
sentry_secrets = Rails.application.secrets.sentry
Sentry.init do |config|
secrets = Rails.application.secrets

config.dsn = sentry_secrets[:dsn]
config.current_environment = Rails.application.secrets.environment_name
config.dsn = secrets.sentry[:dsn]
config.environment = secrets.environment_name
config.release = secrets.release_version

config.breadcrumbs_logger = %i[sentry_logger active_support_logger http_logger]

# Send POST data and cookies to Sentry
config.processors -= [ Raven::Processor::Cookies, Raven::Processor::PostData ]
config.release = sentry_secrets[:release]
config.send_default_pii = true

# Don't send transaction data to Sentry
config.traces_sample_rate = 0.0

# Don't log "Sentry is ready" message
config.silence_ready = true
# Reduce the amount of logging from Sentry
config.logger = Sentry::Logger.new(STDOUT)
config.logger.level = Logger::ERROR
end if Rails.env.production?
62 changes: 27 additions & 35 deletions config/puma.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,35 +9,12 @@

NUM_WORKERS = ENV.fetch('WEB_CONCURRENCY') { Etc.nprocessors }.to_i

worker_timeout ENV.fetch('WORKER_TIMEOUT', 60).to_i

stdout_redirect(
ENV.fetch('STDOUT_LOGFILE', "#{APP_DIR}/log/puma.stdout.log"),
ENV.fetch('STDERR_LOGFILE', "#{APP_DIR}/log/puma.stderr.log"),
true
) if ActiveModel::Type::Boolean.new.cast(ENV.fetch('REDIRECT_STDOUT', false))

before_fork do
require 'puma_worker_killer'

PumaWorkerKiller.config do |config|
# Restart workers when they start consuming more than 1G each
config.ram = ENV.fetch('MAX_MEMORY') do
ENV.fetch('MAX_WORKER_MEMORY', 512).to_i * NUM_WORKERS
end.to_i

config.frequency = 10

config.percent_usage = 0.75

config.rolling_restart_frequency = false

config.reaper_status_logs = false
end

PumaWorkerKiller.start
end

# https://github.com/rails/rails/blob/master/railties/lib/rails/generators/rails/app/templates/config/puma.rb.tt

# Puma can serve each request in a thread from an internal thread pool.
Expand All @@ -46,31 +23,28 @@
# the maximum value specified for Puma. Default is set to 5 threads for minimum
# and maximum; this matches the default thread size of Active Record.
#
max_threads_count = ENV.fetch("RAILS_MAX_THREADS") { 5 }
min_threads_count = ENV.fetch("RAILS_MIN_THREADS") { max_threads_count }
max_threads_count = ENV.fetch('RAILS_MAX_THREADS') { 5 }
min_threads_count = ENV.fetch('RAILS_MIN_THREADS') { max_threads_count }
threads min_threads_count, max_threads_count

# Specifies the `worker_timeout` threshold that Puma will use to wait before
# terminating a worker in development environments.
#
worker_timeout 3600 if ENV.fetch("RAILS_ENV", "development") == "development"
worker_timeout ENV.fetch('WORKER_TIMEOUT', 60).to_i

if ENV['SOCKET']
# Specifies the `socket` to which Puma will bind to receive requests.
#
bind ENV['SOCKET']
else
# Specifies the `port` that Puma will listen on to receive requests; default is 3000.
#
port ENV.fetch("PORT") { 3000 }
port ENV.fetch('PORT') { 3000 }
end

# Specifies the `environment` that Puma will run in.
#
environment ENV.fetch("RAILS_ENV") { "development" }
environment ENV.fetch('RAILS_ENV') { 'development' }

# Specifies the `pidfile` that Puma will use.
pidfile ENV.fetch("PIDFILE") { "tmp/pids/server.pid" }
pidfile ENV.fetch('PIDFILE') { 'tmp/pids/server.pid' }

# Specifies the number of `workers` to boot in clustered mode.
# Workers are forked web server processes. If using threads and workers together
Expand All @@ -87,12 +61,30 @@
#
preload_app! if ActiveModel::Type::Boolean.new.cast(ENV.fetch('PRELOAD_APP', false))

before_fork do
require 'puma_worker_killer'

PumaWorkerKiller.config do |config|
# Restart workers when they start consuming too much of the RAM
config.ram = ENV.fetch('MAX_MEMORY') do
ENV.fetch('MAX_WORKER_MEMORY', 1024).to_i * NUM_WORKERS
end.to_i

config.frequency = 10

config.percent_usage = 0.75

config.rolling_restart_frequency = false

config.reaper_status_logs = false
end

PumaWorkerKiller.start
end

# Allow puma to be restarted by `rails restart` command.
plugin :tmp_restart

# Call GC.start and GC.compact before forking to try to reduce worker memory usage
nakayoshi_fork

# Adds a small delay before accepting requests if the worker (process) has threads already
# processing other requests, to try to get idle workers to accept the request
wait_for_less_busy_worker ENV.fetch('WAIT_FOR_LESS_BUSY_WORKERS', '0.005').to_f
11 changes: 0 additions & 11 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@
match :search, action: :index, via: [:get, :post], on: :collection

publishable
# Not in V1
#has_logic
end

resources :vocab_terms do
Expand All @@ -33,15 +31,6 @@
publishable
end

# Not in V1
#resources :logics, only: [] do
# post 'seeds', on: :member
#end

#resources :lists do
# publishable
#end

resources :users, only: [:index]

resource :user, only: [:show, :update, :destroy]
Expand Down

0 comments on commit 463a94d

Please sign in to comment.