Class: Falcon::Controller::Serve
- Inherits:
-
Async::Container::Controller
- Object
- Async::Container::Controller
- Falcon::Controller::Serve
- Defined in:
- lib/falcon/controller/serve.rb
Overview
A generic controller for serving an application. Uses Server for handling incoming requests.
Instance Method Summary collapse
-
#create_container ⇒ Object
Create the controller as specified by the command.
-
#endpoint ⇒ Object
The endpoint the server will bind to.
-
#initialize(command, **options) ⇒ Serve
constructor
Initialize the server controller.
- #load_app ⇒ Object
-
#name ⇒ Object
The name of the controller which is used for the process title.
-
#setup(container) ⇒ Object
Setup the container with the application instance.
-
#start ⇒ Object
Prepare the bound endpoint for the server.
-
#stop ⇒ Object
Close the bound endpoint.
Constructor Details
#initialize(command, **options) ⇒ Serve
Initialize the server controller.
37 38 39 40 41 42 43 44 45 |
# File 'lib/falcon/controller/serve.rb', line 37 def initialize(command, **) @command = command @endpoint = nil @bound_endpoint = nil @debug_trap = Async::IO::Trap.new(:USR1) super(**) end |
Instance Method Details
#create_container ⇒ Object
Create the controller as specified by the command. e.g. `Async::Container::Forked`.
49 50 51 |
# File 'lib/falcon/controller/serve.rb', line 49 def create_container @command.container_class.new end |
#endpoint ⇒ Object
The endpoint the server will bind to.
54 55 56 |
# File 'lib/falcon/controller/serve.rb', line 54 def endpoint @command.endpoint end |
#load_app ⇒ Object
59 60 61 |
# File 'lib/falcon/controller/serve.rb', line 59 def load_app @command.load_app end |
#name ⇒ Object
The name of the controller which is used for the process title.
79 80 81 |
# File 'lib/falcon/controller/serve.rb', line 79 def name "Falcon Server" end |
#setup(container) ⇒ Object
Setup the container with the application instance.
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/falcon/controller/serve.rb', line 85 def setup(container) container.run(name: self.name, restart: true, **@command.) do |instance| Async do |task| # Load one app instance per container: app = self.load_app task.async do if @debug_trap.install! Console.logger.info(instance) do "- Per-process status: kill -USR1 #{Process.pid}" end end @debug_trap.trap do Console.logger.info(self) do |buffer| task.reactor.print_hierarchy(buffer) end end end server = Falcon::Server.new(app, @bound_endpoint, protocol: @endpoint.protocol, scheme: @endpoint.scheme) server.run instance.ready! task.children.each(&:wait) end end end |
#start ⇒ Object
Prepare the bound endpoint for the server.
64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/falcon/controller/serve.rb', line 64 def start @endpoint ||= self.endpoint @bound_endpoint = Async do Async::IO::SharedEndpoint.bound(@endpoint) end.wait Console.logger.info(self) { "Starting #{name} on #{@endpoint.to_url}" } @debug_trap.ignore! super end |
#stop ⇒ Object
Close the bound endpoint.
117 118 119 120 121 122 123 |
# File 'lib/falcon/controller/serve.rb', line 117 def stop(*) @bound_endpoint&.close @debug_trap.default! super end |