Class: Falcon::Controller::Serve

Inherits:
Async::Container::Controller
  • Object
show all
Defined in:
lib/falcon/controller/serve.rb

Overview

A generic controller for serving an application. Uses Server for handling incoming requests.

Direct Known Subclasses

Proxy, Redirect

Instance Method Summary collapse

Constructor Details

#initialize(command, **options) ⇒ Serve

Initialize the server controller.



21
22
23
24
25
26
27
28
29
# File 'lib/falcon/controller/serve.rb', line 21

def initialize(command, **options)
	@command = command
	
	@endpoint = nil
	@bound_endpoint = nil
	@debug_trap = Async::IO::Trap.new(:USR1)
	
	super(**options)
end

Instance Method Details

#create_containerObject

Create the controller as specified by the command. e.g. ‘Async::Container::Forked`.



33
34
35
# File 'lib/falcon/controller/serve.rb', line 33

def create_container
	@command.container_class.new
end

#endpointObject

The endpoint the server will bind to.



38
39
40
# File 'lib/falcon/controller/serve.rb', line 38

def endpoint
	@command.endpoint
end

#load_appObject



43
44
45
# File 'lib/falcon/controller/serve.rb', line 43

def load_app
	@command.load_app
end

#nameObject

The name of the controller which is used for the process title.



63
64
65
# File 'lib/falcon/controller/serve.rb', line 63

def name
	"Falcon Server"
end

#setup(container) ⇒ Object

Setup the container with the application instance.



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/falcon/controller/serve.rb', line 69

def setup(container)
	container.run(name: self.name, restart: true, **@command.container_options) 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

#startObject

Prepare the bound endpoint for the server.



48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/falcon/controller/serve.rb', line 48

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

#stopObject

Close the bound endpoint.



101
102
103
104
105
106
107
# File 'lib/falcon/controller/serve.rb', line 101

def stop(*)
	@bound_endpoint&.close
	
	@debug_trap.default!
	
	super
end