Zbatery 0.1.1
[zbatery.git] / lib / zbatery.rb
blob6f2a47432fbc755b9697b231648d5397fc17626c
1 # -*- encoding: binary -*-
2 require 'rainbows'
4 module Zbatery
6   # current version of Zbatery
7   VERSION = "0.1.1"
9   class << self
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
15     end
16   end
18   Rainbows::Const::RACK_DEFAULTS["SERVER_SOFTWARE"] = "Zbatery #{VERSION}"
20   # true if our Ruby implementation supports unlinked files
21   UnlinkedIO = begin
22     tmp = Unicorn::Util.tmpio
23     tmp.chmod(0)
24     tmp.close
25     true
26   rescue
27     false
28   end
30   # we don't actually fork workers, but allow using the
31   # {before,after}_fork hooks found in Unicorn/Rainbows!
32   # config files...
33   FORK_HOOK = lambda { |_,_| }
35   class HttpServer < Rainbows::HttpServer
37     # this class is only used to avoid breaking Unicorn user switching
38     class DeadIO
39       def chown(*args); end
40     end
42     # only used if no concurrency model is specified
43     def worker_loop(worker)
44       init_worker_process(worker)
45       begin
46         ret = IO.select(LISTENERS, nil, nil, nil) and
47         ret.first.each do |sock|
48           begin
49             process_client(sock.accept_nonblock)
50           rescue Errno::EAGAIN, Errno::ECONNABORTED
51           end
52         end
53       rescue Errno::EINTR
54       rescue Errno::EBADF, TypeError
55         break
56       rescue => e
57         Rainbows::Error.listen_loop(e)
58       end while G.alive
59     end
61     # no-op
62     def maintain_worker_count; end
64     # can't just do a graceful exit if reopening logs fails, so we just
65     # continue on...
66     def reopen_logs
67       logger.info "reopening logs"
68       Unicorn::Util.reopen_logs
69       logger.info "done reopening logs"
70       rescue => e
71         logger.error "failed reopening logs #{e.message}"
72     end
74     def join
75       begin
76         trap(:INT) { stop(false) } # Mongrel trapped INT for Win32...
78         # try these anyways regardless of platform...
79         trap(:TERM) { stop(false) }
80         trap(:QUIT) { stop }
81         trap(:USR1) { reopen_logs }
82         trap(:USR2) { reexec }
84         # no other way to reliably switch concurrency models...
85         trap(:HUP) { reexec; stop }
87         # technically feasible in some cases, just not sanely supportable:
88         %w(TTIN TTOU WINCH).each do |sig|
89           trap(sig) { logger.info "SIG#{sig} is not handled by Zbatery" }
90         end
91       rescue => e # hopefully ignores errors on Win32...
92         logger.error "failed to setup signal handler: #{e.message}"
93       end
94       worker = Worker.new(0, DeadIO.new)
95       before_fork.call(self, worker)
96       worker_loop(worker) # runs forever
97     end
99     def stop(graceful = true)
100       Rainbows::G.quit!
101       exit!(0) unless graceful
102     end
104     def before_fork
105       hook = super
106       hook == FORK_HOOK or
107         logger.warn "calling before_fork without forking"
108       hook
109     end
111     def after_fork
112       hook = super
113       hook == FORK_HOOK or
114         logger.warn "calling after_fork without having forked"
115       hook
116     end
117   end
120 # :stopdoc:
121 # override stuff we don't need or can't use portably
122 module Rainbows
124   module Base
125     # master == worker in our case
126     def init_worker_process(worker)
127       after_fork.call(self, worker)
128       build_app! unless preload_app
129       logger.info "Zbatery #@use worker_connections=#@worker_connections"
130     end
131   end
133   # we can't/don't need to do the fchmod heartbeat Unicorn/Rainbows! does
134   def G.tick
135     alive
136   end
139 module Unicorn
141   class Configurator
142     DEFAULTS[:before_fork] = DEFAULTS[:after_fork] = Zbatery::FORK_HOOK
143   end
145   unless Zbatery::UnlinkedIO
146     require 'tempfile'
147     class Util
149       # Tempfiles should get automatically unlinked by GC
150       def self.tmpio
151         fp = Tempfile.new("zbatery")
152         fp.binmode
153         fp.sync = true
154         fp
155       end
156     end
157   end