Last active
August 29, 2015 14:04
-
-
Save dorentus/3a86f818686fdd512868 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
begin | |
require 'unicorn' | |
rescue LoadError | |
abort "Required gem not found: unicorn" | |
end | |
if not File.exists? 'config.ru' | |
abort "Required file not found: ./config.ru" | |
end | |
%w(bin config).each do |path| | |
Dir.mkdir path unless Dir.exists? path | |
end | |
require 'open-uri' | |
open 'bin/server', 'wb' do |file| | |
file << open('https://gist.githubusercontent.com/dorentus/3a86f818686fdd512868/raw/server.rb').read | |
file.chmod 0744 | |
end | |
open 'config/unicorn.rb', 'wb' do |file| | |
file << open('https://gist.githubusercontent.com/dorentus/3a86f818686fdd512868/raw/unicorn.rb').read | |
end | |
puts %x[bin/server] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
Command = ARGV[0] | |
ARGV.shift | |
RootDir = File.expand_path "../", File.dirname(__FILE__) | |
UnicornConfigFile = "#{RootDir}/config/unicorn.rb" | |
class ServerCtl | |
class << self | |
def status | |
if pid.nil? | |
puts "Server is not running" | |
return 1 | |
else | |
puts "Server is running, pid = #{pid}" | |
return 0 | |
end | |
end | |
def start | |
if not pid.nil? | |
puts "Server is already running (pid = #{pid}), doing nothing" | |
return | |
end | |
if ARGV.count == 0 | |
args = "-E deployment -D" | |
else | |
args = ARGV.join " " | |
end | |
puts "Starting server with args: #{args}" | |
Dir.chdir RootDir do | |
system "bundle exec unicorn -c \"#{UnicornConfigFile}\" #{args}" | |
end | |
status | |
end | |
def stop | |
if pid.nil? | |
puts "Server is not running, doing nothing" | |
return | |
end | |
puts "Sending QUIT to process #{pid}..." | |
Process.kill "QUIT", pid | |
File.delete pid_file | |
status | |
return 0 | |
end | |
def restart | |
if pid.nil? | |
return start | |
end | |
puts "Sending USR2 to process #{pid}..." | |
Process.kill "USR2", pid | |
puts "Please wait..." | |
sleep 1 | |
old_pid = pid_from_file "#{RootDir}/tmp/pids/unicorn.pid.oldbin" | |
if not old_pid.nil? | |
puts "Sending QUIT to process #{old_pid}..." | |
Process.kill "QUIT", old_pid | |
end | |
status | |
end | |
private | |
def pid | |
pid_from_file pid_file | |
end | |
def pid_file | |
"#{RootDir}/tmp/pids/unicorn.pid" | |
end | |
def pid_from_file path | |
return nil unless File.exists? path | |
pid = File.read(path).chomp.to_i | |
begin | |
Process.getpgid pid | |
pid | |
rescue Errno::ESRCH | |
nil | |
end | |
end | |
end | |
end | |
%w(tmp/pids tmp/sockets tmp/log).each do |path| | |
full_path = "#{RootDir}/#{path}" | |
Dir.mkdir full_path unless Dir.exists? full_path | |
end | |
case Command | |
when "status" | |
exit ServerCtl.status.to_i | |
when "start" | |
exit ServerCtl.start.to_i | |
when "stop" | |
exit ServerCtl.stop.to_i | |
when "restart" | |
exit ServerCtl.restart.to_i | |
else | |
puts "Usage: #{$0} {start|stop|restart|status}" | |
exit 1 | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
root_dir = File.expand_path '../', File.dirname(__FILE__) | |
worker_processes Integer(ENV["WEB_CONCURRENCY"] || 2) | |
timeout 15 | |
preload_app true | |
GC.copy_on_write_friendly = true if GC.respond_to?(:copy_on_write_friendly=) | |
check_client_connection false | |
working_directory root_dir | |
listen "#{root_dir}/tmp/sockets/web.sock", :backlog => 64 | |
pid "#{root_dir}/tmp/pids/unicorn.pid" | |
if ENV['RACK_ENV'] != 'development' | |
stderr_path "#{root_dir}/log/unicorn.log" | |
stdout_path "#{root_dir}/log/unicorn.log" | |
end | |
before_fork do |server, worker| | |
Signal.trap 'TERM' do | |
puts 'Unicorn master intercepting TERM and sending myself QUIT instead' | |
Process.kill 'QUIT', Process.pid | |
end | |
defined?(ActiveRecordBase) and | |
ActiveRecordBase.connection.disconnect! | |
end | |
after_fork do |server, worker| | |
Signal.trap 'TERM' do | |
puts 'Unicorn worker intercepting TERM and doing nothing. Wait for master to send QUIT' | |
end | |
defined?(ActiveRecordBase) and | |
ActiveRecordBase.establish_connection | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment