unicorn.git  about / heads / tags
Rack HTTP server for Unix and fast clients
blob 37ee0279a9eec6d79071c40fd7a2a8e89a1081c1 5918 bytes (raw)
$ git show v0.97.1:bin/unicorn_rails	# shows this blob on the CLI

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
 
#!/this/will/be/overwritten/or/wrapped/anyways/do/not/worry/ruby
# -*- encoding: binary -*-
require 'unicorn/launcher'
require 'optparse'
require 'fileutils'

daemonize = false
listeners = []
options = { :listeners => listeners }
host, port = Unicorn::Const::DEFAULT_HOST, Unicorn::Const::DEFAULT_PORT
set_listener = false
ENV['RAILS_ENV'] ||= "development"

opts = OptionParser.new("", 24, '  ') do |opts|
  cmd = File.basename($0)
  opts.banner = "Usage: #{cmd} " \
                "[ruby options] [#{cmd} options] [rackup config file]"
  opts.separator "Ruby options:"

  lineno = 1
  opts.on("-e", "--eval LINE", "evaluate a LINE of code") do |line|
    eval line, TOPLEVEL_BINDING, "-e", lineno
    lineno += 1
  end

  opts.on("-d", "--debug", "set debugging flags (set $DEBUG to true)") do
    $DEBUG = true
  end

  opts.on("-w", "--warn", "turn warnings on for your script") do
    $-w = true
  end

  opts.on("-I", "--include PATH",
          "specify $LOAD_PATH (may be used more than once)") do |path|
    $LOAD_PATH.unshift(*path.split(/:/))
  end

  opts.on("-r", "--require LIBRARY",
          "require the library, before executing your script") do |library|
    require library
  end

  opts.separator "#{cmd} options:"

  # some of these switches exist for rackup command-line compatibility,

  opts.on("-o", "--host HOST",
          "listen on HOST (default: #{Unicorn::Const::DEFAULT_HOST})") do |h|
    host = h
    set_listener = true
  end

  opts.on("-p", "--port PORT", "use PORT (default: #{port})") do |p|
    port = p.to_i
    set_listener = true
  end

  opts.on("-E", "--env RAILS_ENV",
          "use RAILS_ENV for defaults (default: development)") do |e|
    ENV['RAILS_ENV'] = e
  end

  opts.on("-D", "--daemonize", "run daemonized in the background") do |d|
    daemonize = d ? true : false
  end

  # Unicorn-specific stuff
  opts.on("-l", "--listen {HOST:PORT|PATH}",
          "listen on HOST:PORT or PATH",
          "this may be specified multiple times",
          "(default: #{Unicorn::Const::DEFAULT_LISTEN})") do |address|
    listeners << address
  end

  opts.on("-c", "--config-file FILE", "Unicorn-specific config file") do |f|
    options[:config_file] = f
  end

  opts.on("-P PATH", "DEPRECATED") do |v|
    warn %q{Use of -P is ambiguous and discouraged}
    warn %q{Use --path or RAILS_RELATIVE_URL_ROOT instead}
    ENV['RAILS_RELATIVE_URL_ROOT'] = v
  end

  opts.on("--path PATH", "Runs Rails app mounted at a specific path.",
          "(default: /)") do |v|
    ENV['RAILS_RELATIVE_URL_ROOT'] = v
  end

  # I'm avoiding Unicorn-specific config options on the command-line.
  # IMNSHO, config options on the command-line are redundant given
  # config files and make things unnecessarily complicated with multiple
  # places to look for a config option.

  opts.separator "Common options:"

  opts.on_tail("-h", "--help", "Show this message") do
    puts opts.to_s.gsub(/^.*DEPRECATED.*$/s, '')
    exit
  end

  opts.on_tail("-v", "--version", "Show version") do
    puts " v#{Unicorn::Const::UNICORN_VERSION}"
    exit
  end

  opts.parse! ARGV
end

config = ARGV[0] || (File.exist?('config.ru') ? 'config.ru' : nil)

if config && config =~ /\.ru\z/
  # parse embedded command-line options in config.ru comments
  /^#\\(.*)/ =~ File.read(config) and opts.parse!($1.split(/\s+/))
end

def rails_builder(config, daemonize)
  # this lambda won't run until after forking if preload_app is false
  lambda do ||
    # Load Rails and (possibly) the private version of Rack it bundles.
    begin
      require 'config/boot'
    rescue LoadError => err
      abort "#$0 must be run inside RAILS_ROOT: #{err.inspect}"
    end

    inner_app = case config
    when nil
      require 'config/environment'

      defined?(::Rails::VERSION::STRING) or
        abort "Rails::VERSION::STRING not defined by config/{boot,environment}"
      # it seems Rails >=2.2 support Rack, but only >=2.3 requires it
      old_rails = case ::Rails::VERSION::MAJOR
      when 0, 1 then true
      when 2 then Rails::VERSION::MINOR < 3 ? true : false
      else
        false
      end

      if old_rails
        require 'unicorn/app/old_rails'
        Unicorn::App::OldRails.new
      else
        ActionController::Dispatcher.new
      end
    when /\.ru$/
      raw = File.open(config, "rb") { |fp| fp.sysread(fp.stat.size) }
      raw.sub!(/^__END__\n.*/, '')
      eval("Rack::Builder.new {(#{raw}\n)}.to_app", nil, config)
    else
      require config
      Object.const_get(File.basename(config, '.rb').capitalize)
    end

    Rack::Builder.new do
      map_path = ENV['RAILS_RELATIVE_URL_ROOT'] || '/'
      if inner_app.class.to_s == "Unicorn::App::OldRails"
        if map_path != '/'
          # patches + tests welcome, but I really cbf to deal with this
          # since all apps I've ever dealt with just use "/" ...
          $stderr.puts "relative URL roots may not work for older Rails"
        end
        $stderr.puts "LogTailer not available for Rails < 2.3" unless daemonize
        $stderr.puts "Debugger not available" if $DEBUG
        map(map_path) do
          use Unicorn::App::OldRails::Static
          run inner_app
        end
      else
        use Rails::Rack::LogTailer unless daemonize
        use Rails::Rack::Debugger if $DEBUG
        map(map_path) do
          use Rails::Rack::Static
          run inner_app
        end
      end
    end.to_app
  end
end

app = rails_builder(config, daemonize)
listeners << "#{host}:#{port}" if set_listener

if $DEBUG
  require 'pp'
  pp({
    :unicorn_options => options,
    :app => app,
    :daemonize => daemonize,
  })
end

# ensure Rails standard tmp paths exist
options[:after_reload] = lambda do
  FileUtils.mkdir_p(%w(cache pids sessions sockets).map! { |d| "tmp/#{d}" })
end

if daemonize
  options[:pid] = "tmp/pids/unicorn.pid"
  Unicorn::Launcher.daemonize!(options)
end
Unicorn.run(app, options)

git clone https://yhbt.net/unicorn.git