Skip to content

Instantly share code, notes, and snippets.

@hendra
Last active August 5, 2018 19:21
Show Gist options
  • Select an option

  • Save hendra/bb1fcd5b4d3c7b44cccfe7351e7bd5be to your computer and use it in GitHub Desktop.

Select an option

Save hendra/bb1fcd5b4d3c7b44cccfe7351e7bd5be to your computer and use it in GitHub Desktop.
Puma Config
#!/usr/bin/env puma
require 'puma_worker_killer'
require 'concurrent'
# Change to match your CPU core count
workers Concurrent.processor_count || 1
# Min and Max threads per worker
min_thread = ENV['RAILS_MIN_THREADS'] || 0
max_thread = ENV['RAILS_MAX_THREADS'] || 16
threads min_thread, max_thread
app_dir = File.expand_path('../../', __FILE__)
shared_dir = "#{app_dir}/shared"
tag 'Puma HTTP Server'
# Default to production
rails_env = ENV['RAILS_ENV'] || "production"
environment rails_env
# Set up socket location
bind "unix://#{shared_dir}/tmp/sockets/puma.sock"
# Logging
stdout_redirect "#{shared_dir}/log/puma.stdout.log", "#{shared_dir}/log/puma.stderr.log", true
# Set master PID and state locations
pidfile "#{shared_dir}/pids/puma.pid"
activate_control_app "unix://#{shared_dir}/tmp/sockets/puma_control.sock"
state_path "/#{shared_dir}/tmp/sockets/puma.state"
worker_timeout 30
worker_boot_timeout 30
prune_bundler true
# If you are preloading your application and using Active Record, it's
# recommended that you close any connections to the database before workers
# are forked to prevent connection leakage.
#
before_fork do
ActiveRecord::Base.connection_pool.disconnect! if defined?(ActiveRecord)
end
# The code in the `on_worker_boot` will be called if you are using
# clustered mode by specifying a number of `workers`. After each worker
# process is booted, this block will be run. If you are using the `preload_app!`
# option, you will want to use this block to reconnect to any threads
# or connections that may have been created at application boot, as Ruby
# cannot share connections between processes.
#
on_worker_boot do
ActiveRecord::Base.establish_connection if defined?(ActiveRecord)
end
on_worker_boot do |server, worker|
PumaWorkerKiller.config do |config|
config.ram = 4096 # mb
config.frequency = 20 # seconds
config.percent_usage = 0.95
config.reaper_status_logs = true
config.pre_term = -> (worker) { puts "Worker #{worker.inspect} being killed at #{Time.now}" }
end
PumaWorkerKiller.start
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment