59 lines
2.1 KiB
Ruby
59 lines
2.1 KiB
Ruby
# unicorn_rails -c config/unicorn.rb -E development -D
|
|
# kill -s USR2 PID_OF_MASTER
|
|
rails_root = `pwd`.gsub("\n", "")
|
|
|
|
rails_env = ENV['RAILS_ENV'] || 'production'
|
|
|
|
cpu_cores = %x(cat /proc/cpuinfo | grep processor | wc -l).sub("\n",'').to_i * 3 / 4 rescue 2
|
|
default_cpu_cores = cpu_cores
|
|
if File.exist?("#{rails_root}/cpu_cores.txt")
|
|
cpu_cores = File.read("#{rails_root}/cpu_cores.txt").force_encoding('utf-8').strip.to_i rescue default_cpu_cores
|
|
else
|
|
begin
|
|
if (File.exist?("#{rails_root}/../cpu_cores.txt") rescue false)
|
|
cpu_cores = File.read("#{rails_root}/../cpu_cores.txt").force_encoding('utf-8').strip.to_i
|
|
elsif (File.exist?("#{ENV['HOME']}/cpu_cores.txt") rescue false)
|
|
cpu_cores = File.read("#{ENV['HOME']}/cpu_cores.txt").force_encoding('utf-8').strip.to_i
|
|
end
|
|
rescue => e
|
|
cpu_cores = default_cpu_cores
|
|
end
|
|
end
|
|
cpu_cores = 1 if (cpu_cores < 1)
|
|
worker_processes (rails_env == 'production' ? cpu_cores : 1)
|
|
|
|
# preload_app true
|
|
|
|
timeout 30
|
|
require File.expand_path("../set_global_variable",__FILE__)
|
|
site = Site.first
|
|
if (site.use_advanced_cache rescue false)
|
|
listen "#{rails_root}/tmp/unicorn_rails.sock", :backlog => 4000
|
|
pid "#{rails_root}/tmp/pids/unicorn_rails.pid"
|
|
|
|
roda_pid_file = "#{rails_root}/tmp/pids/unicorn.pid"
|
|
roda_pid = File.open(roda_pid_file, 'r').read().gsub("\n",'').to_i rescue nil
|
|
if !roda_pid || !system("kill -0 #{roda_pid}")
|
|
`cd #{rails_root}/config/roda && ulimit -n 100000 && bundle exec unicorn -c unicorn.rb -D -E production`
|
|
end
|
|
else
|
|
listen "#{rails_root}/tmp/unicorn.sock", :backlog => 4000
|
|
pid "#{rails_root}/tmp/pids/unicorn.pid"
|
|
end
|
|
if Unicorn::Configurator::RACKUP[:daemonized]
|
|
stderr_path "#{rails_root}/log/unicorn.log"
|
|
stdout_path "#{rails_root}/log/unicorn.log"
|
|
end
|
|
ENV['worker_num'] = '0'
|
|
before_fork do |server, worker|
|
|
ENV['worker_num'] = "#{worker.nr}"
|
|
old_pid = "#{server.config[:pid]}.oldbin"
|
|
if File.exist?(old_pid)
|
|
begin
|
|
sig = (worker.nr + 1) >= server.worker_processes ? :QUIT : :TTOU
|
|
Process.kill(sig, File.read(old_pid).to_i)
|
|
rescue Errno::ENOENT, Errno::ESRCH
|
|
end
|
|
end
|
|
end
|