Skip to content

Commit 4f26d97

Browse files
author
Albert Yi
committed
tweak unicorn config
1 parent c075b01 commit 4f26d97

File tree

1 file changed

+49
-13
lines changed

1 file changed

+49
-13
lines changed

config/unicorn/production.rb

Lines changed: 49 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
# Set unicorn options
55
worker_processes 22
66

7-
preload_app true
87
timeout 180
9-
listen "127.0.0.1:9000"
8+
listen "127.0.0.1:9000", :tcp_nopush => true
9+
listen "/tmp/.unicorn.sock", :backlog => 64
1010

1111
# Spawn unicorn master worker for user apps (group: apps)
1212
user 'albert', 'albert'
@@ -24,20 +24,56 @@
2424
# Set master PID location
2525
pid "#{app_path}/tmp/pids/unicorn.pid"
2626

27+
# combine Ruby 2.0.0+ with "preload_app true" for memory savings
28+
preload_app true
29+
30+
# Enable this flag to have unicorn test client connections by writing the
31+
# beginning of the HTTP headers before calling the application. This
32+
# prevents calling the application for connections that have disconnected
33+
# while queued. This is only guaranteed to detect clients on the same
34+
# host unicorn runs on, and unlikely to detect disconnects even on a
35+
# fast LAN.
36+
check_client_connection false
37+
38+
# local variable to guard against running a hook multiple times
39+
run_once = true
40+
2741
before_fork do |server, worker|
28-
old_pid = "#{server.config[:pid]}.oldbin"
29-
30-
if File.exists?(old_pid) && server.pid != old_pid
31-
begin
32-
Process.kill("QUIT", File.read(old_pid).to_i)
33-
rescue Errno::ENOENT, Errno::ESRCH
34-
# someone else did our job for us
35-
end
36-
end
42+
# the following is highly recomended for Rails + "preload_app true"
43+
# as there's no need for the master process to hold a connection
44+
defined?(ActiveRecord::Base) and ActiveRecord::Base.connection.disconnect!
3745

38-
if defined?(ActiveRecord::Base)
39-
ActiveRecord::Base.connection.disconnect!
46+
# Occasionally, it may be necessary to run non-idempotent code in the
47+
# master before forking. Keep in mind the above disconnect! example
48+
# is idempotent and does not need a guard.
49+
if run_once
50+
# do_something_once_here ...
51+
run_once = false # prevent from firing again
4052
end
53+
54+
# The following is only recommended for memory/DB-constrained
55+
# installations. It is not needed if your system can house
56+
# twice as many worker_processes as you have configured.
57+
#
58+
# # This allows a new master process to incrementally
59+
# # phase out the old master process with SIGTTOU to avoid a
60+
# # thundering herd (especially in the "preload_app false" case)
61+
# # when doing a transparent upgrade. The last worker spawned
62+
# # will then kill off the old master process with a SIGQUIT.
63+
# old_pid = "#{server.config[:pid]}.oldbin"
64+
# if old_pid != server.pid
65+
# begin
66+
# sig = (worker.nr + 1) >= server.worker_processes ? :QUIT : :TTOU
67+
# Process.kill(sig, File.read(old_pid).to_i)
68+
# rescue Errno::ENOENT, Errno::ESRCH
69+
# end
70+
# end
71+
#
72+
# Throttle the master from forking too quickly by sleeping. Due
73+
# to the implementation of standard Unix signal handlers, this
74+
# helps (but does not completely) prevent identical, repeated signals
75+
# from being lost when the receiving process is busy.
76+
sleep 1
4177
end
4278

4379
after_fork do |server, worker|

0 commit comments

Comments
 (0)