Skip to content

Commit

Permalink
Support using puma if configured via USE_PUMA=true (multithreading)
Browse files Browse the repository at this point in the history
  • Loading branch information
machisuji committed Jul 11, 2019
1 parent 2fc4416 commit 60c29cc
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 15 deletions.
3 changes: 2 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,8 @@ gem 'sprockets', '~> 3.7.0'
# also, better than thin since we can control worker concurrency.
gem 'unicorn'

gem 'puma', '~> 4.0.0' # used for development and optionally for production

gem 'nokogiri', '~> 1.10.3'

gem 'carrierwave', '~> 1.3.1'
Expand Down Expand Up @@ -256,7 +258,6 @@ group :development do
end

group :development, :test do
gem 'puma', '~> 3.12.0'
gem 'thin', '~> 1.7.2'

# Tracing and profiling gems
Expand Down
5 changes: 3 additions & 2 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -649,7 +649,8 @@ GEM
binding_of_caller (>= 0.7)
pry (>= 0.9.11)
public_suffix (3.0.3)
puma (3.12.0)
puma (4.0.0)
nio4r (~> 2.0)
rabl (0.14.0)
activesupport (>= 2.3.14)
rack (2.0.6)
Expand Down Expand Up @@ -997,7 +998,7 @@ DEPENDENCIES
pry-rails (~> 0.3.6)
pry-rescue (~> 1.5.0)
pry-stack_explorer (~> 0.4.9.2)
puma (~> 3.12.0)
puma (~> 4.0.0)
rabl (~> 0.14.0)
rack-attack (~> 5.4.2)
rack-mini-profiler
Expand Down
2 changes: 1 addition & 1 deletion Procfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
web: bundle exec unicorn --config-file config/unicorn.rb --host ${HOST:="127.0.0.1"} --port ${PORT:="8080"} --env ${RAILS_ENV:="development"}
web: ./packaging/scripts/web
worker: bundle exec rake jobs:work
backup: ./packaging/scripts/backup
check: ./packaging/scripts/check
10 changes: 10 additions & 0 deletions config/environments/production.rb
Original file line number Diff line number Diff line change
Expand Up @@ -127,4 +127,14 @@


config.active_record.dump_schema_after_migration = false

if OpenProject::Configuration.enable_internal_assets_server?
config.public_file_server.enabled = true
config.public_file_server.headers = {
'Access-Control-Allow-Origin' => '*',
'Access-Control-Allow-Methods' => 'GET, OPTIONS, HEAD',
'Cache-Control' => 'public, s-maxage=31536000, max-age=15552000',
'Expires' => "#{1.year.from_now.to_formatted_s(:rfc822)}"
}
end
end
7 changes: 4 additions & 3 deletions config/puma.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
# Any libraries that use thread pools should be configured to match
# the maximum value specified for Puma.
#
threads_count = ENV.fetch("RAILS_MAX_THREADS") { 16 }
threads 0, threads_count
threads_min_count = ENV.fetch("RAILS_MIN_THREADS") { 4 }
threads_max_count = ENV.fetch("RAILS_MAX_THREADS") { 16 }
threads threads_min_count, threads_max_count

# Specifies the `port` that Puma will listen on to receive requests; default is 3000.
#
Expand All @@ -30,4 +31,4 @@
preload_app! if ENV["RAILS_ENV"] == 'production'

# Allow puma to be restarted by `rails restart` command.
plugin :tmp_restart
plugin :tmp_restart unless ENV["RAILS_ENV"] == 'production'
29 changes: 22 additions & 7 deletions docker/web
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,30 @@ MIN_INSTANCES="${PASSENGER_MIN_INSTANCES:=1}"
MAX_INSTANCES="${PASSENGER_MAX_INSTANCES:=3}"
SPAWN_METHOD="${PASSENGER_SPAWN_METHOD:=smart}"

USE_PUMA="${USE_PUMA:="false"}"

if [ "$RAILS_ENV" = "production" ] && [ "$USE_PUMA" = "true" ]; then
# @TODO Add apache config to serve the assets. Until then we have Puma serve
# them which is slower. This is ok if an asset host is used (SaaS).
# But for self-hosted installations we may want to fix that.
# Passenger does include a config to have nginx serve the assets.
export OPENPROJECT_ENABLE__INTERNAL__ASSETS__SERVER=true
fi

if [ "$MIGRATE" = "true" ]; then
echo "Migrating database..."
bundle exec rake db:migrate
fi

exec bundle exec passenger start \
-p $PORT \
-a "${BIND}" \
--min-instances "$MIN_INSTANCES" \
--max-pool-size "$MAX_INSTANCES" \
--spawn-method "$SPAWN_METHOD" \
--max-preloader-idle-time 0
if [ "$USE_PUMA" ]; then
# see `config/puma.rb` for configuration
bundle exec rails server puma -b $BIND -p $PORT
else
exec bundle exec passenger start \
-p $PORT \
-a "${BIND}" \
--min-instances "$MIN_INSTANCES" \
--max-pool-size "$MAX_INSTANCES" \
--spawn-method "$SPAWN_METHOD" \
--max-preloader-idle-time 0
fi
4 changes: 3 additions & 1 deletion lib/open_project/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ module Configuration
'rails_relative_url_root' => '',
'rails_force_ssl' => false,
'rails_asset_host' => nil,
# Enable internal asset server
'enable_internal_assets_server' => false,

# Additional / overridden help links
'force_help_link' => nil,
Expand Down Expand Up @@ -143,7 +145,7 @@ module Configuration

# Check for missing migrations in internal errors
'migration_check_on_exceptions' => true,

# Show pending migrations as warning bar
'show_pending_migrations_warning' => true,

Expand Down
21 changes: 21 additions & 0 deletions packaging/scripts/web
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#!/bin/bash -e

HOST="${HOST:=127.0.0.1}"
PORT="${PORT:=8080}"
RAILS_ENV="${RAILS_ENV:="development"}"

USE_PUMA="${USE_PUMA:="false"}"

if [ "$RAILS_ENV" = "production" ] && [ "$USE_PUMA" = "true" ]; then
# @TODO Add apache config to serve the assets. Until then we have Puma serve
# them which is slower. This is ok if an asset host is used (SaaS).
# But for self-hosted installations we may want to fix that.
# Passenger does include a config to have nginx serve the assets.
export OPENPROJECT_ENABLE__INTERNAL__ASSETS__SERVER=true
fi

if [ "$USE_PUMA" = "true" ]; then
bundle exec rails server puma -b $HOST -p $PORT
else
bundle exec unicorn --config-file config/unicorn.rb --host $HOST --port $PORT --env $RAILS_ENV
fi

0 comments on commit 60c29cc

Please sign in to comment.