2 $stdin.sync
= $stdout.sync
= $stderr.sync
= true
3 require 'unicorn' # require this first to populate Unicorn::DEFAULT_START_CTX
9 options
= { :listeners => listeners
}
10 host
= Unicorn
::Const::DEFAULT_HOST
11 port
= Unicorn
::Const::DEFAULT_PORT
13 opts
= OptionParser
.new("", 24, ' ') do |opts
|
14 opts
.banner
= "Usage: #{File.basename($0)} " \
15 "[ruby options] [unicorn options] [rackup config file]"
17 opts
.separator
"Ruby options:"
20 opts
.on("-e", "--eval LINE", "evaluate a LINE of code") do |line
|
21 eval line
, TOPLEVEL_BINDING
, "-e", lineno
25 opts
.on("-d", "--debug", "set debugging flags (set $DEBUG to true)") do
29 opts
.on("-w", "--warn", "turn warnings on for your script") do
33 opts
.on("-I", "--include PATH",
34 "specify $LOAD_PATH (may be used more than once)") do |path
|
35 $LOAD_PATH.unshift(*path
.split(/:/))
38 opts
.on("-r", "--require LIBRARY",
39 "require the library, before executing your script") do |library
|
43 opts
.separator
"Unicorn options:"
45 # some of these switches exist for rackup command-line compatibility,
47 opts
.on("-o", "--host HOST",
48 "listen on HOST (default: #{Unicorn::Const::DEFAULT_HOST})") do |h
|
52 opts
.on("-p", "--port PORT",
53 "use PORT (default: #{Unicorn::Const::DEFAULT_PORT})") do |p
|
57 opts
.on("-E", "--env ENVIRONMENT",
58 "use ENVIRONMENT for defaults (default: development)") do |e
|
62 opts
.on("-D", "--daemonize", "run daemonized in the background") do |d
|
63 daemonize
= d
? true : false
66 opts
.on("-P", "--pid FILE", "file to store PID (default: none)") do |f
|
67 options
[:pid] = File
.expand_path(f
)
70 # Unicorn-specific stuff
71 opts
.on("-l", "--listen {HOST:PORT|PATH}",
72 "listen on HOST:PORT or PATH",
73 "this may be specified multiple times",
74 "(default: #{Unicorn::Const::DEFAULT_LISTEN})") do |address
|
78 opts
.on("-c", "--config-file FILE", "Unicorn-specific config file") do |f
|
79 options
[:config_file] = File
.expand_path(f
)
82 # I'm avoiding Unicorn-specific config options on the command-line.
83 # IMNSHO, config options on the command-line are redundant given
84 # config files and make things unnecessarily complicated with multiple
85 # places to look for a config option.
87 opts
.separator
"Common options:"
89 opts
.on_tail("-h", "--help", "Show this message") do
94 opts
.on_tail("-v", "--version", "Show version") do
95 puts
"unicorn v#{Unicorn::Const::UNICORN_VERSION}"
102 require 'pp' if $DEBUG
104 # require Rack as late as possible in case $LOAD_PATH is modified
105 # in config.ru or command-line
108 config
= ARGV[0] || "config.ru"
109 abort
"configuration file #{config} not found" unless File
.exist
?(config
)
111 inner_app
= case config
113 raw
= File
.open(config
, "rb") { |fp
| fp
.sysread(fp
.stat
.size
) }
114 # parse embedded command-line options in config.ru comments
116 opts
.parse
! $1.split(/\s+/)
118 lambda
{ || eval("Rack::Builder.new {(#{raw}\n)}.to_app", nil, config
) }
122 Object
.const_get(File
.basename(config
, '.rb').capitalize
)
130 use Rack
::CommonLogger, $stderr
131 use Rack
::ShowExceptions
139 use Rack
::CommonLogger, $stderr
148 listener
= "#{host}:#{port}"
149 listeners
<< listener
154 :unicorn_options => options
,
156 :inner_app => inner_app
,
157 :daemonize => daemonize
,
161 # only daemonize if we're not inheriting file descriptors from our parent
164 $stdin.reopen("/dev/null")
165 unless ENV['UNICORN_FD']
171 # We don't do a lot of standard daemonization stuff:
172 # * $stderr/$stderr can/will be redirected separately
173 # * umask is whatever was set by the parent process at startup
174 # and can be set in config.ru and config_file, so making it
175 # 0000 and potentially exposing sensitive log data can be bad
177 # * Don't bother to chdir here since Unicorn is designed to
178 # run inside APP_ROOT. Unicorn will also re-chdir() to
179 # the directory it was started in when being re-executed
180 # to pickup code changes if the original deployment directory
181 # is a symlink or otherwise got replaced.
184 Unicorn
.run(app
, options
)