Class: Async::Service::Controller

Inherits:
Container::Controller
  • Object
show all
Defined in:
lib/async/service/controller.rb

Overview

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.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(services, **options) ⇒ Controller

Initialize a new controller with services.



36
37
38
39
40
# File 'lib/async/service/controller.rb', line 36

def initialize(services, **options)
	super(**options)
	
	@services = services
end

Instance Attribute Details

#servicesObject (readonly)

All the services associated with this controller.



62
63
64
# File 'lib/async/service/controller.rb', line 62

def services
  @services
end

Class Method Details

.for(*services, **options) ⇒ Object

Create a controller for the given services.



29
30
31
# File 'lib/async/service/controller.rb', line 29

def self.for(*services, **options)
	self.new(services, **options)
end

.run(configuration, **options) ⇒ Object

Run a configuration of services.



21
22
23
# File 'lib/async/service/controller.rb', line 21

def self.run(configuration, **options)
	configuration.make_controller(**options).run
end

Instance Method Details

#make_policyObject

Create a policy for managing child lifecycle events.



66
67
68
# File 'lib/async/service/controller.rb', line 66

def make_policy
	Policy::DEFAULT
end

#setup(container) ⇒ Object

Setup all services into the given container.



84
85
86
87
88
89
90
91
92
# File 'lib/async/service/controller.rb', line 84

def setup(container)
	super
	
	@services.each do |service|
		service.setup(container)
	end
	
	return container
end

#startObject

Start all named services.



71
72
73
74
75
76
77
78
79
# File 'lib/async/service/controller.rb', line 71

def start
	@services.each do |service|
		service.start
	end
	
	super
	
	self.warmup
end

#stop(graceful = @graceful_stop) ⇒ Object

Stop all named services.



95
96
97
98
99
100
101
102
103
104
105
# File 'lib/async/service/controller.rb', line 95

def stop(graceful = @graceful_stop)
	@services.each do |service|
		begin
			service.stop
		rescue => error
			Console.error(self, error)
		end
	end
	
	super
end

#warmupObject

Warm up the Ruby process by preloading gems, running GC, and compacting memory. This reduces startup latency and improves copy-on-write efficiency.



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/async/service/controller.rb', line 44

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