Skip to content

Commit

Permalink
Add Puma to dependencies
Browse files Browse the repository at this point in the history
This commit adds Puma to govuk_app_template's dependencies, along with a config file defining sensible defaults (and trying to keep inline with Unicorn to a large degree). Puma will be used as the default webserver when apps are replatformed to Kubernetes; this commit enables that work to progress using a single source of truth for configuration and following the same practice as we currently use for managing Unicorn.
  • Loading branch information
Karl Baker committed Oct 20, 2021
1 parent 2d6415f commit baaff17
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# Unreleased

- Add Puma to dependencies ([#214](https://github.com/alphagov/govuk_app_config/pull/214)).

# 4.0.1

- Update Content Security Policy with new klick2contact.com subdomain ([#213](https://github.com/alphagov/govuk_app_config/pull/213)).
Expand Down
1 change: 1 addition & 0 deletions govuk_app_config.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ Gem::Specification.new do |spec|
spec.require_paths = %w[lib]

spec.add_dependency "logstasher", ">= 1.2.2", "< 2.2.0"
spec.add_dependency "puma", ">= 5.3.2", "< 6.0.0"
spec.add_dependency "sentry-rails", "~> 4.5.0"
spec.add_dependency "sentry-ruby", "~> 4.5.0"
spec.add_dependency "statsd-ruby", "~> 1.5.0"
Expand Down
55 changes: 55 additions & 0 deletions lib/govuk_app_config/govuk_puma.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
module GovukPuma
def self.configure(config)
# Specifies the `port` that Puma will listen on to receive requests; default is 3000.
#
config.port ENV.fetch("PORT", 3000)

# Specifies the `environment` that Puma will run in.
#
config.environment ENV.fetch("RAILS_ENV", "development")

if ENV["GOVUK_APP_LOGROOT"]
config.stdout_redirect "#{ENV['GOVUK_APP_LOGROOT']}/app.out.log" "#{ENV['GOVUK_APP_LOGROOT']}/app.err.log"
end

# Specifies the `worker_timeout` threshold that Puma will use to wait before
# terminating a worker in development environments.
#
timeout = ENV.fetch("RAILS_ENV", "development") == "development" ? 3600 : 15
config.worker_timeout timeout

# Puma can serve each request in a thread from an internal thread pool.
# The `threads` method setting takes two numbers: a minimum and maximum.
# Any libraries that use thread pools should be configured to match
# 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)
config.threads min_threads_count, max_threads_count

# Specifies the number of `workers` to boot in clustered mode.
# Workers are forked web server processes. If using threads and workers together
# the concurrency of the application would be max `threads` * `workers`.
# Workers do not work on JRuby or Windows (both of which do not support
# processes).
#
config.workers ENV.fetch("WEB_CONCURRENCY", 2)

# Use the `preload_app!` method when specifying a `workers` number.
# This directive tells Puma to first boot the application and load code
# before forking the application. This takes advantage of Copy On Write
# process behavior so workers use less memory.
#
config.preload_app!

config.before_fork do |_server|
next unless ENV["GOVUK_APP_ROOT"]

ENV["BUNDLE_GEMFILE"] = "#{ENV['GOVUK_APP_ROOT']}/Gemfile"
end

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

0 comments on commit baaff17

Please sign in to comment.