Skip to content

Instantly share code, notes, and snippets.

@ryansch
Created May 27, 2014 17:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ryansch/d551525afcf0cc26b463 to your computer and use it in GitHub Desktop.
Save ryansch/d551525afcf0cc26b463 to your computer and use it in GitHub Desktop.
Opsworks unicorn configuration
worker_processes <%= node[:unicorn][:worker_processes] %>
user "<%= @deploy[:user]%>"
working_directory "<%= @deploy[:deploy_to]%>/current"
listen "<%= @deploy[:deploy_to]%>/shared/sockets/unicorn.sock", :backlog => <%= node[:unicorn][:backlog] %>, :tcp_nodelay => <%= node[:unicorn][:tcp_nodelay] %>, :tcp_nopush => <%= node[:unicorn][:tcp_nopush] %>, :tries => <%= node[:unicorn][:tries] %>, :delay => <%= node[:unicorn][:delay] %>, :accept_filter => <%= node[:unicorn][:accept_filter].inspect %>
timeout <%= node[:unicorn][:timeout] %>
pid "<%= @deploy[:deploy_to]%>/shared/pids/unicorn.pid"
stderr_path "<%= @deploy[:deploy_to]%>/shared/log/unicorn.stderr.log"
stdout_path "<%= @deploy[:deploy_to]%>/shared/log/unicorn.stdout.log"
preload_app <%= node[:unicorn][:preload_app] %>
GC.copy_on_write_friendly = true if GC.respond_to?(:copy_on_write_friendly=)
# ensure Unicorn doesn't use a stale Gemfile when restarting
# more info: https://willj.net/2011/08/02/fixing-the-gemfile-not-found-bundlergemfilenotfound-error/
before_exec do |server|
# If we're loading from .env files, clear out the env vars so we pick up changes.
if defined?(Dotenv)
rails_env = ENV['RAILS_ENV'] || ENV['RACK_ENV']
[".env.#{rails_env}", '.env'].each do |file|
next unless File.exists?(file)
env = Dotenv::Environment.new(file)
env.each { |var, value| ENV[var] = nil }
end
end
ENV['BUNDLE_GEMFILE'] = "<%= @deploy[:deploy_to]%>/current/Gemfile"
end
before_fork do |server, worker|
# the following is highly recomended for Rails + "preload_app true"
# as there's no need for the master process to hold a connection
if defined?(ActiveRecord::Base)
ActiveRecord::Base.connection.disconnect!
end
<% @deploy[:unicorn][:before_fork_additions].each do |line| -%>
<%= line %>
<% end -%>
# The following is only recommended for memory/DB-constrained
# installations. It is not needed if your system can house
# twice as many worker_processes as you have configured.
#
# # This allows a new master process to incrementally
# # phase out the old master process with SIGTTOU to avoid a
# # thundering herd (especially in the "preload_app false" case)
# # when doing a transparent upgrade. The last worker spawned
# # will then kill off the old master process with a SIGQUIT.
old_pid = "#{server.config[:pid]}.oldbin"
if old_pid != server.pid
begin
sig = (worker.nr + 1) >= server.worker_processes ? :QUIT : :TTOU
Process.kill(sig, File.read(old_pid).to_i)
rescue Errno::ENOENT, Errno::ESRCH
end
end
#
# Throttle the master from forking too quickly by sleeping. Due
# to the implementation of standard Unix signal handlers, this
# helps (but does not completely) prevent identical, repeated signals
# from being lost when the receiving process is busy.
sleep 1
end
after_fork do |server, worker|
# per-process listener ports for debugging/admin/migrations
# addr = "127.0.0.1:#{9293 + worker.nr}"
# server.listen(addr, :tries => -1, :delay => 5, :tcp_nopush => true)
# the following is *required* for Rails + "preload_app true",
if defined?(ActiveRecord::Base)
ActiveRecord::Base.establish_connection
end
# if preload_app is true, then you may also want to check and
# restart any other shared sockets/descriptors such as Memcached,
# and Redis. TokyoCabinet file handles are safe to reuse
# between any number of forked children (assuming your kernel
# correctly implements pread()/pwrite() system calls)
<% @deploy[:unicorn][:after_fork_additions].each do |line| -%>
<%= line %>
<% end -%>
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment