Skip to content

Commit 19d64fa

Browse files
samuel-williams-shopifyioquatix
authored andcommitted
Adopt Async::Service::ManagedService.
1 parent 1ce9c45 commit 19d64fa

File tree

3 files changed

+34
-74
lines changed

3 files changed

+34
-74
lines changed

falcon.gemspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ Gem::Specification.new do |spec|
3131
spec.add_dependency "async-container-supervisor", "~> 0.6"
3232
spec.add_dependency "async-http", "~> 0.75"
3333
spec.add_dependency "async-http-cache", "~> 0.4"
34-
spec.add_dependency "async-service", "~> 0.10"
34+
spec.add_dependency "async-service", "~> 0.16"
3535
spec.add_dependency "bundler"
3636
spec.add_dependency "localhost", "~> 1.1"
3737
spec.add_dependency "openssl", "~> 3.0"

lib/falcon/environment/server.rb

Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
# Released under the MIT License.
44
# Copyright, 2020-2025, by Samuel Williams.
55

6-
require "async/service/generic"
6+
require "async/service/managed_environment"
77
require "async/http/endpoint"
88

99
require_relative "../service/server"
@@ -13,6 +13,8 @@ module Falcon
1313
module Environment
1414
# Provides an environment for hosting a web application that uses a Falcon server.
1515
module Server
16+
include Async::Service::ManagedEnvironment
17+
1618
# The service class to use for the proxy.
1719
# @returns [Class]
1820
def service_class
@@ -25,21 +27,6 @@ def authority
2527
self.name
2628
end
2729

28-
# Number of instances to start. By default (when nil), uses `Etc.nprocessors`.
29-
# @returns [Integer | nil]
30-
def count
31-
nil
32-
end
33-
34-
# Options to use when creating the container.
35-
def container_options
36-
{
37-
restart: true,
38-
count: self.count,
39-
health_check_timeout: 30,
40-
}.compact
41-
end
42-
4330
# The host that this server will receive connections for.
4431
def url
4532
"http://[::]:9292"
@@ -80,13 +67,6 @@ def client_endpoint
8067
::Async::HTTP::Endpoint.parse(url)
8168
end
8269

83-
# Any scripts to preload before starting the server.
84-
#
85-
# @returns [Array(String)] The list of scripts to preload.
86-
def preload
87-
[]
88-
end
89-
9070
# Make a server instance using the given endpoint. The endpoint may be a bound endpoint, so we take care to specify the protocol and scheme as per the original endpoint.
9171
#
9272
# @parameter endpoint [IO::Endpoint] The endpoint to bind to.

lib/falcon/service/server.rb

Lines changed: 30 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -4,36 +4,21 @@
44
# Copyright, 2019-2025, by Samuel Williams.
55
# Copyright, 2020, by Daniel Evans.
66

7-
require "async/service/generic"
7+
require "async/service/managed_service"
88
require "async/container/supervisor/supervised"
99
require "async/http/endpoint"
1010

1111
require_relative "../server"
1212

1313
module Falcon
1414
module Service
15-
class Server < Async::Service::Generic
15+
class Server < Async::Service::ManagedService
1616
def initialize(...)
1717
super
1818

1919
@bound_endpoint = nil
2020
end
2121

22-
# Preload any resources specified by the environment.
23-
def preload!
24-
root = @evaluator.root
25-
26-
if scripts = @evaluator.preload
27-
scripts = Array(scripts)
28-
29-
scripts.each do |path|
30-
Console.logger.info(self) {"Preloading #{path}..."}
31-
full_path = File.expand_path(path, root)
32-
load(full_path)
33-
end
34-
end
35-
end
36-
3722
# Prepare the bound endpoint for the server.
3823
def start
3924
@endpoint = @evaluator.endpoint
@@ -42,47 +27,42 @@ def start
4227
@bound_endpoint = @endpoint.bound
4328
end
4429

45-
preload!
46-
4730
Console.logger.info(self) {"Starting #{self.name} on #{@endpoint}"}
4831

4932
super
5033
end
5134

52-
# Setup the container with the application instance.
53-
# @parameter container [Async::Container::Generic]
54-
def setup(container)
55-
container_options = @evaluator.container_options
56-
health_check_timeout = container_options[:health_check_timeout]
35+
# Run the service logic.
36+
#
37+
# @parameter instance [Object] The container instance.
38+
# @parameter evaluator [Environment::Evaluator] The environment evaluator.
39+
# @returns [Falcon::Server] The server instance.
40+
def run(instance, evaluator)
41+
if evaluator.key?(:make_supervised_worker)
42+
Console.warn(self, "Async::Container::Supervisor is replaced by Async::Services::Supervisor, please update your service definition.")
43+
44+
evaluator.make_supervised_worker(instance).run
45+
end
46+
47+
server = evaluator.make_server(@bound_endpoint)
5748

58-
container.run(name: self.name, **container_options) do |instance|
59-
evaluator = @environment.evaluator
49+
Async do |task|
50+
server.run
6051

61-
Async do |task|
62-
if @environment.implements?(Async::Container::Supervisor::Supervised)
63-
evaluator.make_supervised_worker(instance).run
64-
end
65-
66-
server = evaluator.make_server(@bound_endpoint)
67-
68-
server.run
69-
70-
instance.ready!
71-
72-
if health_check_timeout
73-
Async(transient: true) do
74-
while true
75-
# We only update this if the health check is enabled. Maybe we should always update it?
76-
instance.name = "#{self.name} (#{server.statistics_string} L=#{Fiber.scheduler.load.round(3)})"
77-
sleep(health_check_timeout / 2)
78-
instance.ready!
79-
end
80-
end
81-
end
82-
83-
task.children.each(&:wait)
84-
end
52+
task.children.each(&:wait)
8553
end
54+
55+
server
56+
end
57+
58+
# Format the process title with server statistics.
59+
#
60+
# @parameter evaluator [Environment::Evaluator] The environment evaluator.
61+
# @parameter server [Falcon::Server] The server instance.
62+
# @returns [String] The formatted process title.
63+
private def format_title(evaluator, server)
64+
load = Fiber.scheduler.load.round(3)
65+
"#{evaluator.name} (#{server.statistics_string} L=#{load})"
8666
end
8767

8868
# Close the bound endpoint.

0 commit comments

Comments
 (0)