Skip to content

Instantly share code, notes, and snippets.

@fraank
Created April 22, 2015 08:22
Show Gist options
  • Save fraank/d01c59ff5e4aec6fcb50 to your computer and use it in GitHub Desktop.
Save fraank/d01c59ff5e4aec6fcb50 to your computer and use it in GitHub Desktop.
unicorn aws opsworks recipe with included EventMachine (used for async_publish with keen.io)
# adapted version of https://github.com/aws/opsworks-cookbooks/blob/release-chef-11.10/unicorn/templates/default/unicorn.conf.erb
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"
<% @environment.each do |key, value| %>
ENV['<%= key %>'] = "<%= value %>"
<% end %>
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|
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
# Before forking, kill the master process that belongs to the .oldbin PID.
# This enables 0 downtime deploys.
old_pid = "<%= @deploy[:deploy_to]%>/shared/pids/unicorn.pid.oldbin"
if File.exists?(old_pid) && server.pid != old_pid
begin
Process.kill("QUIT", File.read(old_pid).to_i)
rescue Errno::ENOENT, Errno::ESRCH
# someone else did our job for us
end
end
end
after_fork do |server, worker|
# 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)
if defined?(EventMachine)
unless EventMachine.reactor_running? && EventMachine.reactor_thread.alive?
if EventMachine.reactor_running?
EventMachine.stop_event_loop
EventMachine.release_machine
EventMachine.instance_variable_set("@reactor_running",false)
end
Thread.new { EventMachine.run }
end
end
Signal.trap("INT") { EventMachine.stop }
Signal.trap("TERM") { EventMachine.stop }
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment