Method: Unicorn::HttpServer#initialize

Defined in:
lib/unicorn/http_server.rb

#initialize(app, options = {}) ⇒ HttpServer

Creates a working server on host:port (strange things happen if port isn’t a Number). Use HttpServer::run to start the server and HttpServer.run.join to join the thread that’s processing incoming requests on the socket.



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

def initialize(app, options = {})
  @app = app
  @reexec_pid = 0
  @default_middleware = true
  options = options.dup
  @ready_pipe = options.delete(:ready_pipe)
  @init_listeners = options[:listeners] ? options[:listeners].dup : []
  options[:use_defaults] = true
  self.config = Unicorn::Configurator.new(options)
  self.listener_opts = {}

  # We use @self_pipe differently in the master and worker processes:
  #
  # * The master process never closes or reinitializes this once
  # initialized.  Signal handlers in the master process will write to
  # it to wake up the master from IO.select in exactly the same manner
  # djb describes in https://cr.yp.to/docs/selfpipe.html
  #
  # * The workers immediately close the pipe they inherit.  See the
  # Unicorn::Worker class for the pipe workers use.
  @self_pipe = []
  @workers = {} # hash maps PIDs to Workers
  @sig_queue = [] # signal queue used for self-piping
  @pid = nil

  # we try inheriting listeners first, so we bind them later.
  # we don't write the pid file until we've bound listeners in case
  # unicorn was started twice by mistake.  Even though our #pid= method
  # checks for stale/existing pid files, race conditions are still
  # possible (and difficult/non-portable to avoid) and can be likely
  # to clobber the pid if the second start was in quick succession
  # after the first, so we rely on the listener binding to fail in
  # that case.  Some tests (in and outside of this source tree) and
  # monitoring tools may also rely on pid files existing before we
  # attempt to connect to the listener(s)
  config.commit!(self, :skip => [:listeners, :pid])
  @orig_app = app
  # list of signals we care about and trap in master.
  @queue_sigs = [
    :WINCH, :QUIT, :INT, :TERM, :USR1, :USR2, :HUP, :TTIN, :TTOU ]

  @worker_data = if worker_data = ENV['UNICORN_WORKER']
    worker_data = worker_data.split(',').map!(&:to_i)
    worker_data[1] = worker_data.slice!(1..2).map do |i|
      Kgio::Pipe.for_fd(i)
    end
    worker_data
  end
end