|
4 | 4 | # Set unicorn options |
5 | 5 | worker_processes 22 |
6 | 6 |
|
7 | | -preload_app true |
8 | 7 | 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 |
10 | 10 |
|
11 | 11 | # Spawn unicorn master worker for user apps (group: apps) |
12 | 12 | user 'albert', 'albert' |
|
24 | 24 | # Set master PID location |
25 | 25 | pid "#{app_path}/tmp/pids/unicorn.pid" |
26 | 26 |
|
| 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 | + |
27 | 41 | 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! |
37 | 45 |
|
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 |
40 | 52 | 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 |
41 | 77 | end |
42 | 78 |
|
43 | 79 | after_fork do |server, worker| |
|
0 commit comments