#!/home/ew/bin/ruby require 'unicorn/launcher' require 'optparse' require 'fileutils' rails_pid = File.join(Unicorn::HttpServer::DEFAULT_START_CTX[:cwd], "/tmp/pids/unicorn.pid") cmd = File.basename($0) daemonize = false listeners = [] options = { :listeners => listeners } host, port = Unicorn::Const::DEFAULT_HOST, 3000 ENV['RAILS_ENV'] ||= "development" map_path = ENV['RAILS_RELATIVE_URL_ROOT'] opts = OptionParser.new("", 24, ' ') do |opts| 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 end opts.on("-p", "--port PORT", "use PORT (default: #{port})") do |p| port = p.to_i end opts.on("-E", "--env ENVIRONMENT", "use ENVIRONMENT 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] = File.expand_path(f) end opts.on("-P", "--path PATH", "Runs Rails app mounted at a specific path.", "(default: /") do |v| map_path = 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 exit end opts.on_tail("-v", "--version", "Show version") do puts " v#{Unicorn::Const::UNICORN_VERSION}" exit end opts.parse! ARGV end require 'pp' if $DEBUG # Loads Rails and the private version of Rack it bundles. Returns a # lambda of arity==0 that will return *another* lambda of arity==1 # suitable for using inside Rack::Builder.new block. rails_loader = lambda do || begin require 'config/boot' defined?(::RAILS_ROOT) or abort "RAILS_ROOT not defined by config/boot" defined?(::RAILS_ENV) or abort "RAILS_ENV not defined by config/boot" defined?(::Rails::VERSION::STRING) or abort "Rails::VERSION::STRING not defined by config/boot" rescue LoadError abort "#$0 must be run inside RAILS_ROOT (#{::RAILS_ROOT})" end # return the lambda config = ::ARGV[0] || (File.exist?('config.ru') ? 'config.ru' : nil) case config when nil lambda do || require 'config/environment' ActionController::Dispatcher.new end when /\.ru$/ raw = File.open(config, "rb") { |fp| fp.sysread(fp.stat.size) } # parse embedded command-line options in config.ru comments if raw[/^#\\(.*)/] opts.parse! $1.split(/\s+/) require 'pp' if $DEBUG end lambda { || eval("Rack::Builder.new {(#{raw}\n)}.to_app", nil, config) } else lambda do || require config Object.const_get(File.basename(config, '.rb').capitalize) end end end # this won't run until after forking if preload_app is false app = lambda do || inner_app = rails_loader.call require 'active_support' require 'action_controller' ActionController::Base.relative_url_root = map_path if map_path Rack::Builder.new do use Rails::Rack::LogTailer unless daemonize use Rails::Rack::Debugger if $DEBUG map(map_path || '/') do use Rails::Rack::Static run inner_app.call end end.to_app end if listeners.empty? listener = "#{host}:#{port}" listeners << listener end if $DEBUG pp({ :unicorn_options => options, :app => app, :daemonize => daemonize, }) end # ensure Rails standard tmp paths exist %w(cache pids sessions sockets).each do |dir| FileUtils.mkdir_p("tmp/#{dir}") end if daemonize options[:pid] = rails_pid Unicorn::Launcher.daemonize! end Unicorn.run(app, options)