Skip to content

Instantly share code, notes, and snippets.

@danman01
Last active December 20, 2015 17:09
Show Gist options
  • Save danman01/6166343 to your computer and use it in GitHub Desktop.
Save danman01/6166343 to your computer and use it in GitHub Desktop.
unicorn.rb for a ec2 micro instance
worker_processes 2
working_directory "/var/www/apps/myapp/current"
preload_app true
if GC.respond_to?(:copy_on_write_friendly=)
GC.copy_on_write_friendly = true
end
timeout 70
# This is where we specify the socket.
# We will point the upstream Nginx module to this socket later on
listen "/tmp/myapp.socket", :backlog => 512
pid "/var/www/apps/myapp/current/tmp/pids/unicorn.myapp.pid"
# Set the path of the log files inside the log folder of the testapp
stderr_path "/var/www/apps/myapp/shared/log/unicorn.stderr.log"
stdout_path "/var/www/apps/myapp/shared/log/unicorn.stdout.log"
before_fork do |server, worker|
# This option works in together with preload_app true setting
# What is does is prevent the master process from holding
# the database connection
defined?(ActiveRecord::Base) and
ActiveRecord::Base.connection.disconnect!
old_pid = "#{server.config[: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|
# 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",
defined?(ActiveRecord::Base) and
ActiveRecord::Base.establish_connection
# 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)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment