Class: Falcon::Controller::Serve

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

Direct Known Subclasses

Proxy, Redirect

Instance Method Summary collapse

Constructor Details

#initialize(command, **options) ⇒ Serve

Returns a new instance of Serve.



31
32
33
34
35
36
37
38
39
# File 'lib/falcon/controller/serve.rb', line 31

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



41
42
43
# File 'lib/falcon/controller/serve.rb', line 41

def create_container
	@command.container_class.new
end

#endpointObject



45
46
47
# File 'lib/falcon/controller/serve.rb', line 45

def endpoint
	@command.endpoint
end

#load_appObject



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

def load_app
	@command.load_app
end

#nameObject



65
66
67
# File 'lib/falcon/controller/serve.rb', line 65

def name
	"Falcon Server"
end

#setup(container) ⇒ Object



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
99
100
101
# File 'lib/falcon/controller/serve.rb', line 69

def setup(container)
	app, _ = self.load_app
	
	if GC.respond_to?(:compact)
		GC.compact
	end
	
	container.run(name: self.name, restart: true, **@command.container_options) do |instance|
		Async do |task|
			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, @endpoint.protocol, @endpoint.scheme)
			
			server.run
			
			instance.ready!
			
			task.children.each(&:wait)
		end
	end
end

#startObject



53
54
55
56
57
58
59
60
61
62
63
# File 'lib/falcon/controller/serve.rb', line 53

def start
	@endpoint ||= self.endpoint
	
	@bound_endpoint = Async::Reactor.run do
		Async::IO::SharedEndpoint.bound(@endpoint)
	end.wait
	
	@debug_trap.ignore!
	
	super
end

#stopObject



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

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