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 opts
.on("-s", "--server SERVER",
71 "this flag only exists for compatibility") do |s
|
72 warn
"-s/--server only exists for compatibility with rackup"
75 # Unicorn-specific stuff
76 opts
.on("-l", "--listen {HOST:PORT|PATH}",
77 "listen on HOST:PORT or PATH",
78 "this may be specified multiple times",
79 "(default: #{Unicorn::Const::DEFAULT_LISTEN})") do |address
|
83 opts
.on("-c", "--config-file FILE", "Unicorn-specific config file") do |f
|
84 options
[:config_file] = File
.expand_path(f
)
87 # I'm avoiding Unicorn-specific config options on the command-line.
88 # IMNSHO, config options on the command-line are redundant given
89 # config files and make things unnecessarily complicated with multiple
90 # places to look for a config option.
92 opts
.separator
"Common options:"
94 opts
.on_tail("-h", "--help", "Show this message") do
99 opts
.on_tail("-v", "--version", "Show version") do
100 puts
"unicorn v#{Unicorn::Const::UNICORN_VERSION}"
107 require 'pp' if $DEBUG
109 # require Rack as late as possible in case $LOAD_PATH is modified
110 # in config.ru or command-line
113 config
= ARGV[0] || "config.ru"
114 abort
"configuration file #{config} not found" unless File
.exist
?(config
)
116 inner_app
= case config
118 raw
= File
.open(config
, "rb") { |fp
| fp
.sysread(fp
.stat
.size
) }
119 # parse embedded command-line options in config.ru comments
121 opts
.parse
! $1.split(/\s+/)
123 lambda
{ || eval("Rack::Builder.new {(#{raw}\n)}.to_app", nil, config
) }
127 Object
.const_get(File
.basename(config
, '.rb').capitalize
)
135 use Rack
::CommonLogger, $stderr
136 use Rack
::ShowExceptions
144 use Rack
::CommonLogger, $stderr
153 listener
= "#{host}:#{port}"
154 listeners
<< listener
159 :unicorn_options => options
,
161 :inner_app => inner_app
,
162 :daemonize => daemonize
,
166 # only daemonize if we're not inheriting file descriptors from our parent
169 $stdin.reopen("/dev/null")
170 unless ENV['UNICORN_FD']
176 # We don't do a lot of standard daemonization stuff:
177 # * $stderr/$stderr can/will be redirected separately
178 # * umask is whatever was set by the parent process at startup
179 # and can be set in config.ru and config_file, so making it
180 # 0000 and potentially exposing sensitive log data can be bad
182 # * Don't bother to chdir here since Unicorn is designed to
183 # run inside APP_ROOT. Unicorn will also re-chdir() to
184 # the directory it was started in when being re-executed
185 # to pickup code changes if the original deployment directory
186 # is a symlink or otherwise got replaced.
189 Unicorn
.run(app
, options
)