1 # -*- encoding: binary -*-
6 # current version of Zbatery
11 # runs the Zbatery HttpServer with +app+ and +options+ and does
12 # not return until the server has exited.
13 def run(app, options = {})
14 HttpServer.new(app, options).start.join
18 Rainbows::Const::RACK_DEFAULTS["SERVER_SOFTWARE"] = "Zbatery #{VERSION}"
20 # true if our Ruby implementation supports unlinked files
22 tmp = Unicorn::Util.tmpio
30 # we don't actually fork workers, but allow using the
31 # {before,after}_fork hooks found in Unicorn/Rainbows!
33 FORK_HOOK = lambda { |_,_| }
35 class HttpServer < Rainbows::HttpServer
37 # this class is only used to avoid breaking Unicorn user switching
42 # only used if no concurrency model is specified
43 def worker_loop(worker)
44 init_worker_process(worker)
46 ret = IO.select(LISTENERS, nil, nil, nil) and
47 ret.first.each do |sock|
49 process_client(sock.accept_nonblock)
50 rescue Errno::EAGAIN, Errno::ECONNABORTED
54 rescue Errno::EBADF, TypeError
57 Rainbows::Error.listen_loop(e)
62 def maintain_worker_count; end
63 def init_self_pipe!; end
65 # can't just do a graceful exit if reopening logs fails, so we just
68 logger.info "reopening logs"
69 Unicorn::Util.reopen_logs
70 logger.info "done reopening logs"
72 logger.error "failed reopening logs #{e.message}"
75 def trap_deferred(sig)
81 trap(:INT) { stop(false) } # Mongrel trapped INT for Win32...
83 # try these anyways regardless of platform...
84 trap(:TERM) { stop(false) }
86 trap(:USR1) { reopen_logs }
87 trap(:USR2) { reexec }
89 # no other way to reliably switch concurrency models...
90 trap(:HUP) { reexec; stop }
92 # technically feasible in some cases, just not sanely supportable:
93 %w(TTIN TTOU WINCH).each do |sig|
94 trap(sig) { logger.info "SIG#{sig} is not handled by Zbatery" }
96 rescue => e # hopefully ignores errors on Win32...
97 logger.error "failed to setup signal handler: #{e.message}"
101 ready_pipe.syswrite($$.to_s)
102 ready_pipe.close rescue nil
103 self.ready_pipe = nil
106 worker = Worker.new(0, DeadIO.new)
107 before_fork.call(self, worker)
108 worker_loop(worker) # runs forever
111 def stop(graceful = true)
113 exit!(0) unless graceful
119 logger.warn "calling before_fork without forking"
126 logger.warn "calling after_fork without having forked"
133 # override stuff we don't need or can't use portably
137 # master == worker in our case
138 def init_worker_process(worker)
139 after_fork.call(self, worker)
140 worker.user(*user) if user.kind_of?(Array) && ! worker.switched
141 build_app! unless preload_app
143 # avoid spurious wakeups and blocking-accept() with 1.8 green threads
144 if RUBY_VERSION.to_f < 1.9
145 require "io/nonblock"
146 HttpServer::LISTENERS.each { |l| l.nonblock = true }
149 logger.info "Zbatery #@use worker_connections=#@worker_connections"
153 # we can't/don't need to do the fchmod heartbeat Unicorn/Rainbows! does
162 DEFAULTS[:before_fork] = DEFAULTS[:after_fork] = Zbatery::FORK_HOOK
165 unless Zbatery::UnlinkedIO
169 # Tempfiles should get automatically unlinked by GC
171 fp = Tempfile.new("zbatery")