Skip to content

Instantly share code, notes, and snippets.

@bingxie bingxie/unicorn.rb
Last active Aug 29, 2015

Embed
What would you like to do?
Unicorn的配置
# unicorn通过master进程管理worker进程,这里初始化工作进程的数量,默认为3个
# 举例: AWS EC2 m3.2xlarge 设置30个worker
worker_processes Integer(ENV["UNICORN_CONCURRENCY"] || 3)
preload_app true
# 工作进程的响应超时时间(秒)
timeout 120
listen "/tmp/sockets/unicorn.sock"
pid "/tmp/pids/unicorn.pid"
if ENV['RAILS_ENV'] == 'development' # 开发环境用于测试和验证一些配置
# 如果是开发环境,unicorn启动后可以通过8888端口访问
listen 888
working_directory "#{ENV['APP_PATH']}" # 指定working_directory,使得unicorn能够探查到应用文件的更新
else
# 产品和测试环境使用不同的部署路径
working_directory "#{ENV['APP_PATH']}/current"
# 指定unicorn的日志路径
stderr_path "/var/log/app-name/app-name-web-1.log"
stdout_path "/var/log/app-name/app-name-web-1.log"
end
# 如果你的部署路径是symlink的,确保Unicorn使用正确的gem版本
before_exec do |server|
ENV['BUNDLE_GEMFILE'] = "#{ENV['APP_PATH']}/current/Gemfile"
end
before_fork do |server, worker|
defined?(ActiveRecord::Base) and
ActiveRecord::Base.connection.disconnect!
# Graceful restart unicorn
old_pid = "#{server.config[:pid]}.oldbin"
if File.exists?(old_pid) && old_pid != server.pid
# 如果新的master进程已经产生,旧的进程还存在就杀死旧的进程
begin
#sig = (worker.nr + 1) >= server.worker_processes ? :QUIT : :TTOU
Process.kill("QUIT", File.read(old_pid).to_i)
rescue Errno::ENOENT, Errno::ESRCH
end
end
sleep 1
end
after_fork do |server, worker|
require 'analytics_ruby'
defined?(ActiveRecord::Base) and
ActiveRecord::Base.establish_connection
ActiveRecord::Base.verify_active_connections!
# 在应用启动后初始化Segment.IO analytics
prepare_analytics!
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.