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.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(services, **options) ⇒ Controller

Initialize a new controller with services.



55
56
57
58
59
# File 'lib/async/service/controller.rb', line 55

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

Class Method Details

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

Create a controller for the given services.



48
49
50
# File 'lib/async/service/controller.rb', line 48

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

.run(configuration, **options) ⇒ Object

Run a configuration of services.



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

def self.run(configuration, **options)
	controller = Async::Service::Controller.new(configuration.services.to_a, **options)
	
	self.warmup
	
	controller.run
end

.warmupObject

Warm up the Ruby process by preloading gems and running GC.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/async/service/controller.rb', line 17

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

Instance Method Details

#servicesObject



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

def services
  @services
end

#setup(container) ⇒ Object

Setup all services into the given container.



77
78
79
80
81
82
83
84
85
# File 'lib/async/service/controller.rb', line 77

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

#startObject

Start all named services.



66
67
68
69
70
71
72
# File 'lib/async/service/controller.rb', line 66

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

#stop(graceful = true) ⇒ Object

Stop all named services.



88
89
90
91
92
93
94
95
96
97
98
# File 'lib/async/service/controller.rb', line 88

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