|
4 | 4 | require "async/container" |
5 | 5 | require "console" |
6 | 6 |
|
| 7 | +require "io/endpoint/host_endpoint" |
| 8 | +require "io/endpoint/bound_endpoint" |
| 9 | + |
7 | 10 | # Console.logger.debug! |
8 | 11 |
|
9 | 12 | class Application < Async::Container::Controller |
| 13 | + def endpoint |
| 14 | + IO::Endpoint.tcp("0.0.0.0", 9292) |
| 15 | + end |
| 16 | + |
| 17 | + def bound_socket |
| 18 | + bound = endpoint.bound |
| 19 | + |
| 20 | + bound.sockets.each do |socket| |
| 21 | + socket.listen(Socket::SOMAXCONN) |
| 22 | + end |
| 23 | + |
| 24 | + return bound |
| 25 | + end |
| 26 | + |
10 | 27 | def setup(container) |
| 28 | + @bound = bound_socket |
| 29 | + |
11 | 30 | container.spawn(name: "Web", restart: true) do |instance| |
12 | | - # Replace the current process with Puma: |
13 | | - # instance.exec("bundle", "exec", "puma", "-C", "puma.rb", ready: false) |
| 31 | + env = ENV.to_h |
14 | 32 |
|
15 | | - # Manage a child process of puma / puma workers: |
16 | | - pid = ::Process.spawn("puma", "-C", "puma.rb") |
17 | | - |
18 | | - instance.ready! |
19 | | - |
20 | | - begin |
21 | | - status = ::Process.wait2(pid) |
22 | | - rescue Async::Container::Hangup |
23 | | - Console.warn(self, "Restarting puma...") |
24 | | - ::Process.kill("USR1", pid) |
25 | | - retry |
26 | | - ensure |
27 | | - ::Process.kill("TERM", pid) |
| 33 | + @bound.sockets.each_with_index do |socket, index| |
| 34 | + env["PUMA_INHERIT_#{index}"] = "#{socket.fileno}:tcp://0.0.0.0:9292" |
28 | 35 | end |
| 36 | + |
| 37 | + instance.exec(env, "bundle", "exec", "puma", "-C", "puma.rb", ready: false) |
29 | 38 | end |
30 | 39 | end |
31 | 40 | end |
32 | 41 |
|
33 | | -Application.new.run |
| 42 | +application = Application.new |
| 43 | +application.run |
0 commit comments