class Controller
Controls multiple services and their lifecycle.
The controller manages starting, stopping, and monitoring multiple services within containers. It extends Async::Container::Controller to provide service-specific functionality.
Definitions
def self.run(configuration, **options)
Run a configuration of services.
Signature
-
parameter
configurationConfiguration The service configuration to run.
-
parameter
optionsHash Additional options for the controller.
Implementation
def self.run(configuration, **options)
configuration.make_controller(**options).run
end
def self.for(*services, **options)
Create a controller for the given services.
Signature
-
parameter
servicesArray(Generic) The services to control.
-
parameter
optionsHash Additional options for the controller.
-
returns
Controller A new controller instance.
Implementation
def self.for(*services, **options)
self.new(services, **options)
end
def initialize(services, **options)
Initialize a new controller with services.
Signature
-
parameter
servicesArray(Generic) The services to manage.
-
parameter
optionsHash Options passed to the parent controller.
Implementation
def initialize(services, **options)
super(**options)
@services = services
end
def warmup
Warm up the Ruby process by preloading gems, running GC, and compacting memory. This reduces startup latency and improves copy-on-write efficiency.
Implementation
def warmup
begin
require "bundler"
Bundler.require(:preload)
rescue Bundler::GemfileNotFound, LoadError
# Ignore.
end
if ::Process.respond_to?(:warmup)
::Process.warmup
elsif ::GC.respond_to?(:compact)
3.times{::GC.start}
::GC.compact
end
end
attr :services
All the services associated with this controller.
Signature
-
attribute
Array(Async::Service::Generic)
def make_policy
Create a policy for managing child lifecycle events.
Signature
-
returns
Policy The service-level policy with failure rate monitoring.
Implementation
def make_policy
Policy::DEFAULT
end
def start
Start all named services.
Implementation
def start
@services.each do |service|
service.start
end
super
self.warmup
end
def setup(container)
Setup all services into the given container.
Signature
-
parameter
containerAsync::Container::Generic
Implementation
def setup(container)
super
@services.each do |service|
service.setup(container)
end
return container
end
def stop(graceful = true)
Stop all named services.
Implementation
def stop(graceful = true)
@services.each do |service|
begin
service.stop
rescue => error
Console.error(self, error)
end
end
super
end