1. Basic Configuration
When unicorn is configured, there are two cases:
preload_app truepreload_app false
If preload_app is false, the unicorn master does not pre-load the entire rails app environment, and every worker executes the entire rails app.
When preload_app is true, the master of Unicorn loads the rails app environment in advance. In the case of copy_on_write_friendly, each worker replicates data from the master when executing the write operation (Resource Saving ).
If prelad_app is true, you need to disable the database connection in before_fork, because the master does not need to maintain this connection.
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 defined?(ActiveRecord::Base) and ActiveRecord::Base.connection.disconnect!end
However, after after_fork, You need to reconnect to the database connection for each worker;
after_fork do |server, worker| # the following is *required* for Rails + "preload_app true", defined?(ActiveRecord::Base) and ActiveRecord::Base.establish_connectionend
When there are other socket connections, you also need to reconnect in after_fork, or close in before_fork first, and then reconnect in after_fork.
2. Unicorn Signal
kill -s HUP pid
When preload_app is false, the configuration and rails app will be re-loaded, and all modified code will be re-loaded, because every worker will re-Execute
When preload_app is true, the configuration will be re-loaded, but the rails app will not be re-loaded. Because every worker is copied from the master, the modified Code will not be re-loaded.
The following command is required:
kill -s USR2 pid
In addition, the old unicorn process is automatically disabled in before_fork.
Before_fork do | Server, worker | # It will be executed when kill-s usr2 'oldpid 'is executed, because unicorn adds the original PID file name. oldbin suffix 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 puts "Send 'quit' signal to Unicorn error! "End end
The result is unicorn again, a new process is generated, and the old process is disabled.