Created
February 22, 2017 10:51
-
-
Save Electron-libre/d803621e33d4937eeaca1e04a68e0c8d to your computer and use it in GitHub Desktop.
Puma with auto worker count
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pidfile ENV.fetch('PUMA_PID_PATH','/tmp/pid') | |
# Should not be a solution | |
worker_timeout Integer(ENV.fetch('PUMA_WORKER_TIMEOUT', 60)) | |
# Default worker ( OS processes ) is set to 4 ( most machines has 4 cores ) | |
# But adjust depending on context ( how much CPU are given to my container ? ) | |
current_cpu_count = begin | |
Integer(`nproc`) - 1 | |
rescue | |
4 | |
end | |
workers Integer(ENV.fetch('PUMA_WORKER_COUNT', current_cpu_count)) | |
# I doubt the application is thread safe. | |
# If it is thread safe then you can increase this number. | |
threads_count = Integer(ENV.fetch('PUMA_MAX_THREADS', 1)) | |
threads threads_count, threads_count | |
preload_app! | |
rackup DefaultRackup | |
port ENV.fetch('PUMA_PORT', 3000) | |
environment ENV.fetch('RAILS_ENV', 'development') | |
on_worker_boot do | |
# after Rails 4.1 just whole block replace by ActiveRecord::Base.establish_connection | |
# and set pool in database.yml | |
ActiveSupport.on_load(:active_record) do | |
config = ActiveRecord::Base.configurations[Rails.env] || | |
Rails.application.config.database_configuration[Rails.env] | |
config['pool'] = threads_count | |
ActiveRecord::Base.establish_connection(config) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment