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.



37
38
39
40
41
42
43
44
45
# File 'lib/falcon/controller/serve.rb', line 37

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`.



49
50
51
# File 'lib/falcon/controller/serve.rb', line 49

def create_container
	@command.container_class.new
end

#endpointObject

The endpoint the server will bind to.



54
55
56
# File 'lib/falcon/controller/serve.rb', line 54

def endpoint
	@command.endpoint
end

#load_appObject



59
60
61
# File 'lib/falcon/controller/serve.rb', line 59

def load_app
	@command.load_app
end

#nameObject

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.container_options) do |instance|
		Async do |task|
			# Load one app instance per container:
			app = self.load_app
			
			task.async do
				if @debug_trap.install!
					Async.logger.info(instance) do
						"- Per-process status: kill -USR1 #{Process.pid}"
					end
				end
				
				@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, 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.



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
	
	Async.logger.info(self) { "Starting #{name} on #{@endpoint.to_url}" }
	
	@debug_trap.ignore!
	
	super
end

#stopObject

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