Skip to content

Commit

Permalink
Initial support for Async::Container::Controller.
Browse files Browse the repository at this point in the history
  • Loading branch information
ioquatix committed Nov 3, 2019
1 parent a803b4c commit 79cf746
Show file tree
Hide file tree
Showing 9 changed files with 54 additions and 32 deletions.
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ source 'https://rubygems.org'

gemspec

# gem "async-container", path: "../async-container"

group :development do
gem 'ruby-prof', platform: :mri
end
Expand Down
2 changes: 2 additions & 0 deletions examples/beer/falcon.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@
load :rack, :self_signed_tls, :supervisor

rack 'beer.localhost', :self_signed_tls

supervisor
4 changes: 2 additions & 2 deletions falcon.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ Gem::Specification.new do |spec|

spec.add_dependency "async", "~> 1.13"
spec.add_dependency "async-io", "~> 1.22"
spec.add_dependency "async-http", "~> 0.48.0"
spec.add_dependency "async-container", "~> 0.14.0"
spec.add_dependency "async-http", "~> 0.49.0"
spec.add_dependency "async-container", "~> 0.15.0"

spec.add_dependency "rack", ">= 1.0"

Expand Down
8 changes: 8 additions & 0 deletions lib/falcon/command/host.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,14 @@ def run(container, verbose = false)
def call(container = Async::Container.new)
container = run(container, parent&.verbose?)

Signal.trap(:USR2) do
replacement = run(container.class.new, parent&.verbose?)

container.stop

container = replacement
end

container.wait(true)
end
end
Expand Down
56 changes: 29 additions & 27 deletions lib/falcon/command/serve.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
require_relative '../endpoint'

require 'async/container'

require 'async/io/trap'
require 'async/io/host_endpoint'
require 'async/io/shared_endpoint'
Expand Down Expand Up @@ -117,48 +118,49 @@ def run(verbose = false)
Async.logger.info(endpoint) do |buffer|
buffer.puts "Falcon v#{VERSION} taking flight! Using #{container_class} #{container_options}"
buffer.puts "- To terminate: Ctrl-C or kill #{Process.pid}"
buffer.puts "- To reload configuration: kill -HUP #{Process.pid}"
end

debug_trap = Async::IO::Trap.new(:USR1)
debug_trap.ignore!

container = container_class.new

container.attach do
bound_endpoint.close
end

container.run(name: "Falcon Server", restart: true, **container_options) do |task, instance|
task.async do
if debug_trap.install!
Async.logger.info(instance) do
"- Per-process status: kill -USR1 #{Process.pid}"
controller = Async::Container::Controller.new(container_class: self.container_class) do |container|
app, _ = self.load_app(verbose)

container.run(name: "Falcon Server", restart: true, **self.container_options) do |task, instance|
task.async do
if debug_trap.install!
Async.logger.info(instance) do
"- Per-process status: kill -USR1 #{Process.pid}"
end
end
end

debug_trap.trap do
Async.logger.info(self) do |buffer|
task.reactor.print_hierarchy(buffer)
debug_trap.trap do
Async.logger.info(self) do |buffer|
task.reactor.print_hierarchy(buffer)
end
end
end

server = Falcon::Server.new(app, bound_endpoint, endpoint.protocol, endpoint.scheme)

server.run

task.children.each(&:wait)
end

app, _ = load_app(verbose)

server = Falcon::Server.new(app, bound_endpoint, endpoint.protocol, endpoint.scheme)

server.run

task.children.each(&:wait)
end

return container
# container.attach do
# bound_endpoint.close
# end

return controller
end

def call
container = run(parent.verbose?)
controller = run(parent.verbose?)

container.wait
controller.run
end
end
end
Expand Down
3 changes: 3 additions & 0 deletions lib/falcon/command/virtual.rb
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,9 @@ def run(verbose = false)
def call
container = run(parent.verbose?)

# If we are asked to restart a given container, spawn a new container to replace the old one.


container.wait
end

Expand Down
6 changes: 6 additions & 0 deletions lib/falcon/supervisor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,12 @@ def initialize(endpoint)
end

def restart(message)
# Tell the parent of this process group to spin up a new process group/container.
# Wait for that to start accepting new connections.
# Stop accepting connections.
# Wait for existing connnections to drain.
# Terminate this process group.

signal = message[:signal] || :INT

# Sepukku:
Expand Down
4 changes: 2 additions & 2 deletions spec/falcon/command/serve_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
end

it "can listen on specified port" do
container = command.run(true)
controller = command.run(true)

begin
Async do
Expand All @@ -42,7 +42,7 @@
client.close
end
ensure
container.stop(false)
controller.stop(false)
end
end
end
Expand Down
1 change: 0 additions & 1 deletion spec/falcon/command/top_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
expect(response).to be_success

response.finish

client.close
end

Expand Down

0 comments on commit 79cf746

Please sign in to comment.