Unicorn 4.x resync for ticker
[rainbows.git] / lib / rainbows / base.rb
blobe614ccfbdec68556ea88123866ccc06357596cce
1 # -*- encoding: binary -*-
3 # base class for \Rainbows! concurrency models, this is currently used by
4 # ThreadSpawn and ThreadPool models.  Base is also its own
5 # (non-)concurrency model which is basically Unicorn-with-keepalive, and
6 # not intended for production use, as keepalive with a pure prefork
7 # concurrency model is extremely expensive.
8 module Rainbows::Base
9   # :stopdoc:
11   # this method is called by all current concurrency models
12   def init_worker_process(worker) # :nodoc:
13     super(worker)
14     Rainbows::Response.setup(self.class)
15     Rainbows::MaxBody.setup
16     Rainbows.worker = worker
18     # we're don't use the self-pipe mechanism in the Rainbows! worker
19     # since we don't defer reopening logs
20     Rainbows::HttpServer::SELF_PIPE.each { |x| x.close }.clear
21     trap(:USR1) { reopen_worker_logs(worker.nr) }
22     trap(:QUIT) { Rainbows.quit! }
23     [:TERM, :INT].each { |sig| trap(sig) { exit!(0) } } # instant shutdown
24     Rainbows::ProcessClient.const_set(:APP, Rainbows.server.app)
25     logger.info "Rainbows! #@use worker_connections=#@worker_connections"
26   end
28   def process_client(client)
29     client.process_loop
30   end
32   def self.included(klass) # :nodoc:
33     klass.const_set :LISTENERS, Rainbows::HttpServer::LISTENERS
34   end
36   def reopen_worker_logs(worker_nr)
37     logger.info "worker=#{worker_nr} reopening logs..."
38     Unicorn::Util.reopen_logs
39     logger.info "worker=#{worker_nr} done reopening logs"
40     rescue
41       Rainbows.quit! # let the master reopen and refork us
42   end
43   # :startdoc:
44 end