quiet mismatched indentation warnings
[rainbows.git] / lib / rainbows / base.rb
blobd0e73421d563759251607dfdcd5cb0d8985ea5eb
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   def sig_receiver(worker)
12     begin
13       worker.to_io.kgio_wait_readable
14       worker.kgio_tryaccept # Unicorn::Worker#kgio_tryaccept
15     rescue => e
16       Rainbows.alive or return
17       Unicorn.log_error(Rainbows.server.logger, "signal receiver", e)
18     end while true
19   end
21   # this method is called by all current concurrency models
22   def init_worker_process(worker) # :nodoc:
23     readers = super(worker)
24     Rainbows::Response.setup
25     Rainbows::MaxBody.setup
26     Rainbows.worker = worker
28     # spawn Threads since Logger takes a mutex by default and
29     # we can't safely lock a mutex in a signal handler
30     trap(:USR1) { Thread.new { reopen_worker_logs(worker.nr) } }
31     trap(:QUIT) { Thread.new { Rainbows.quit! } }
32     [:TERM, :INT].each { |sig| trap(sig) { exit!(0) } } # instant shutdown
33     Rainbows::ProcessClient.const_set(:APP, Rainbows.server.app)
34     Thread.new { sig_receiver(worker) }
35     logger.info "Rainbows! #@use worker_connections=#@worker_connections"
36     Rainbows.readers = readers # for Rainbows.quit
37     readers # unicorn 4.8+ needs this
38   end
40   def process_client(client)
41     client.process_loop
42   end
44   def self.included(klass) # :nodoc:
45     klass.const_set :LISTENERS, Rainbows::HttpServer::LISTENERS
46   end
48   def reopen_worker_logs(worker_nr)
49     logger.info "worker=#{worker_nr} reopening logs..."
50     Unicorn::Util.reopen_logs
51     logger.info "worker=#{worker_nr} done reopening logs"
52   rescue
53     Rainbows.quit! # let the master reopen and refork us
54   end
55   # :startdoc:
56 end