Async::ServiceSourceAsyncServiceContainerService

class ContainerService

A service that runs in a container with built-in health checking and process title formatting.

This is the recommended base class for most services.

Definitions

def run(instance, evaluator)

Run the service logic.

Override this method to implement your service. Return an object that represents the running service (e.g., a server, task, or worker pool) for health checking.

Signature

parameter instance Object

The container instance.

parameter evaluator Environment::Evaluator

The environment evaluator.

returns Object

The service object (server, task, etc.)

Implementation

def run(instance, evaluator)
	Async do
		sleep
	end
end

def preload!

Preload any resources specified by the environment.

Implementation

def preload!
	if scripts = @evaluator.preload
		root = @evaluator.root
		scripts = Array(scripts)
		
		scripts.each do |path|
			Console.info(self) {"Preloading #{path}..."}
			full_path = File.expand_path(path, root)
			require(full_path)
		end
	end
rescue => error
	Console.warn(self, "Service preload failed!", error)
end

def start

Start the service, including preloading resources.

Implementation

def start
	preload!
	
	super
end

def setup(container)

Set up the container with health checking and process title formatting.

Signature

parameter container Async::Container

The container to configure.

Implementation

def setup(container)
	super
	
	container_options = @evaluator.container_options
	health_check_timeout = container_options[:health_check_timeout]
	
	container.run(**container_options) do |instance|
		evaluator = self.environment.evaluator
		
		server = run(instance, evaluator)
		
		health_checker(instance) do
			instance.name = format_title(evaluator, server)
		end
	end
end