Module: Unicorn

Included in:
Configurator
Defined in:
lib/unicorn.rb,
lib/unicorn/preread_input.rb

Overview

Unicorn exposes very little of an user-visible API and most of its internals are subject to change. Unicorn is designed to host Rack applications, so applications should be written against the Rack SPEC and not Unicorn internals.

Defined Under Namespace

Modules: OobGC, Util Classes: ClientShutdown, Configurator, HttpServer, PrereadInput, StreamInput, TeeInput, Worker

Class Method Summary collapse

Class Method Details

.builder(ru, op) ⇒ Object

This returns a lambda to pass in as the app, this does not “build” the app (which we defer based on the outcome of “preload_app” in the Unicorn config). The returned lambda will be called when it is time to build the app.



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
# File 'lib/unicorn.rb', line 34

def self.builder(ru, op)
  # allow Configurator to parse cli switches embedded in the ru file
  op = Unicorn::Configurator::RACKUP.merge!(:file => ru, :optparse => op)

  # always called after config file parsing, may be called after forking
  lambda do ||
    inner_app = case ru
    when /\.ru$/
      raw = File.read(ru)
      raw.sub!(/^__END__\n.*/, '')
      eval("Rack::Builder.new {(\n#{raw}\n)}.to_app", TOPLEVEL_BINDING, ru)
    else
      require ru
      Object.const_get(File.basename(ru, '.rb').capitalize)
    end

    pp({ :inner_app => inner_app }) if $DEBUG

    # return value, matches rackup defaults based on env
    # Unicorn does not support persistent connections, but Rainbows!
    # and Zbatery both do.  Users accustomed to the Rack::Server default
    # middlewares will need ContentLength/Chunked middlewares.
    case ENV["RACK_ENV"]
    when "development"
      Rack::Builder.new do
        use Rack::ContentLength
        use Rack::Chunked
        use Rack::CommonLogger, $stderr
        use Rack::ShowExceptions
        use Rack::Lint
        run inner_app
      end.to_app
    when "deployment"
      Rack::Builder.new do
        use Rack::ContentLength
        use Rack::Chunked
        use Rack::CommonLogger, $stderr
        run inner_app
      end.to_app
    else
      inner_app
    end
  end
end

.listener_namesObject

returns an array of strings representing TCP listen socket addresses and Unix domain socket paths. This is useful for use with Raindrops::Middleware under Linux: raindrops.bogomips.org/



82
83
84
85
86
# File 'lib/unicorn.rb', line 82

def self.listener_names
  Unicorn::HttpServer::LISTENERS.map do |io|
    Unicorn::SocketHelper.sock_name(io)
  end + Unicorn::HttpServer::NEW_LISTENERS
end

.log_error(logger, prefix, exc) ⇒ Object



88
89
90
91
92
93
# File 'lib/unicorn.rb', line 88

def self.log_error(logger, prefix, exc)
  message = exc.message
  message = message.dump if /[[:cntrl:]]/ =~ message
  logger.error "#{prefix}: #{message} (#{exc.class})"
  exc.backtrace.each { |line| logger.error(line) }
end