Class: Falcon::Service::Server

Inherits:
Async::Service::Managed::Service
  • Object
show all
Defined in:
lib/falcon/service/server.rb

Overview

A managed service for running Falcon servers.

Direct Known Subclasses

Cluster

Instance Method Summary collapse

Constructor Details

#initializeServer

Initialize the server service.



19
20
21
22
23
# File 'lib/falcon/service/server.rb', line 19

def initialize(...)
	super
	
	@listener = nil
end

Instance Method Details

#bind_endpointObject

Bind the endpoint used by each server worker.



40
41
42
43
44
45
46
47
48
49
# File 'lib/falcon/service/server.rb', line 40

def bind_endpoint
	@endpoint = @evaluator.endpoint
	
	Sync do
		bound_endpoint = @endpoint.bound
		@listener = make_listener(@evaluator, @endpoint, bound_endpoint)
	end
	
	Console.info(self){"Starting #{self.name} on #{@endpoint}"}
end

#make_listener(evaluator, endpoint, bound_endpoint) ⇒ Object

Build a listener from a configured and bound endpoint.



30
31
32
33
34
35
36
37
# File 'lib/falcon/service/server.rb', line 30

def make_listener(evaluator, endpoint, bound_endpoint)
	Listener.new(
		name: evaluator.name,
		scheme: endpoint.scheme,
		protocols: endpoint.protocol.names,
		endpoint: bound_endpoint,
	)
end

#run(instance, evaluator, listener = @listener) ⇒ Object

Run the service logic.



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/falcon/service/server.rb', line 107

def run(instance, evaluator, listener = @listener)
	if evaluator.respond_to?(:make_supervised_worker)
		Console.warn(self, "Async::Container::Supervisor is replaced by Async::Service::Supervisor, please update your service definition.")
		
		evaluator.make_supervised_worker(instance).run
	end
	
	server = evaluator.make_server(listener.endpoint)
	
	Async do |task|
		server.run
		
		task.children&.each(&:wait)
	end
	
	server
end

#setup(container) ⇒ Object

Setup the service into the specified container.



68
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
# File 'lib/falcon/service/server.rb', line 68

def setup(container)
	container_options = @evaluator.container_options
	health_check_timeout = container_options[:health_check_timeout]
	
	container.run(**container_options) do |instance|
		clock = Async::Clock.start
		evaluator = self.environment.evaluator
		
		with_listener(evaluator) do |listener|
			Async do
				server = nil
				
				health_checker(instance, health_check_timeout) do
					if server
						instance.name = format_title(evaluator, server)
					end
				end
				
				instance.status!("Preparing...")
				evaluator.prepare_worker!(instance, listener)
				emit_prepared(instance, clock)
				
				instance.status!("Running...")
				server = run(instance, evaluator, listener)
				instance.name = format_title(evaluator, server)
				emit_running(instance, clock)
				
				instance.ready!
			end
		end
	end
end

#startObject

Prepare the bound endpoint for the server.



52
53
54
55
56
# File 'lib/falcon/service/server.rb', line 52

def start
	bind_endpoint
	
	super
end

#stopObject

Close the bound endpoint.



136
137
138
139
140
141
142
143
144
145
# File 'lib/falcon/service/server.rb', line 136

def stop(...)
	if @listener
		@listener.close
		@listener = nil
	end
	
	@endpoint = nil
	
	super
end

#with_listener(evaluator) {|@listener| ... } ⇒ Object

Yield the listener used by a server worker.

Yields:

  • (@listener)


62
63
64
# File 'lib/falcon/service/server.rb', line 62

def with_listener(evaluator)
	yield @listener
end