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.warmup
Warm up the Ruby process by preloading gems and running GC.
Implementation
def self.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
def self.run(configuration, **options)
Run a configuration of services.
Signature
-
parameter
configuration
Configuration
The service configuration to run.
-
parameter
options
Hash
Additional options for the controller.
Implementation
def self.run(configuration, **options)
controller = Async::Service::Controller.new(configuration.services.to_a, **options)
self.warmup
controller.run
end
def self.for(*services, **options)
Create a controller for the given services.
Signature
-
parameter
services
Array(Generic)
The services to control.
-
parameter
options
Hash
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
services
Array(Generic)
The services to manage.
-
parameter
options
Hash
Options passed to the parent controller.
Implementation
def initialize(services, **options)
super(**options)
@services = services
end
attr :services
All the services associated with this controller.
Signature
-
attribute
Array(Async::Service::Generic)
def start
Start all named services.
Implementation
def start
@services.each do |service|
service.start
end
super
end
def setup(container)
Setup all services into the given container.
Signature
-
parameter
container
Async::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