tests: "wc -c" portability for *BSDs
[rainbows.git] / lib / rainbows / base.rb
blob54f1d2de65f8f86821e242c46dce47f81fcb39e7
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
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
22     # spawn Threads since Logger takes a mutex by default and
23     # we can't safely lock a mutex in a signal handler
24     trap(:USR1) { Thread.new { reopen_worker_logs(worker.nr) } }
25     trap(:QUIT) { Thread.new { Rainbows.quit! } }
26     [:TERM, :INT].each { |sig| trap(sig) { exit!(0) } } # instant shutdown
27     Rainbows::ProcessClient.const_set(:APP, Rainbows.server.app)
28     logger.info "Rainbows! #@use worker_connections=#@worker_connections"
29   end
31   def process_client(client)
32     client.process_loop
33   end
35   def self.included(klass) # :nodoc:
36     klass.const_set :LISTENERS, Rainbows::HttpServer::LISTENERS
37   end
39   def reopen_worker_logs(worker_nr)
40     logger.info "worker=#{worker_nr} reopening logs..."
41     Unicorn::Util.reopen_logs
42     logger.info "worker=#{worker_nr} done reopening logs"
43     rescue
44       Rainbows.quit! # let the master reopen and refork us
45   end
46   # :startdoc:
47 end