Rails + unicorn Configuration

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.