Async::ServiceSourceAsyncServiceGeneric

class Generic

Captures the stateful behaviour of a specific service. Specifies the interfaces required by derived classes.

Designed to be invoked within an Async::Controller::Container.

Definitions

def self.wrap(environment)

Convert the given environment into a service if possible.

If the evaluator responds to make_service, it is called with the environment and its return value is used as the service. This allows environments to compose child environments and return a concrete service without a dedicated service class.

Otherwise, the evaluator's service_class is instantiated with the environment and evaluator as arguments.

Signature

parameter environment Environment

The environment to use to construct the service.

returns Generic | Nil

The constructed service if the environment specifies a service class or make_service.

Implementation

def self.wrap(environment)
	evaluator = environment.evaluator
	
	if evaluator.respond_to?(:make_service)
		return evaluator.make_service(environment)
	elsif evaluator.key?(:service_class)
		if service_class = evaluator.service_class
			return service_class.new(environment, evaluator)
		end
	end
end

def initialize(environment, evaluator = environment.evaluator)

Initialize the service from the given environment.

Signature

parameter environment Environment

Implementation

def initialize(environment, evaluator = environment.evaluator)
	@environment = environment
	@evaluator = evaluator
end

attr :environment

Signature

attribute Environment

The environment which is used to configure the service.

def to_h

Convert the service evaluator to a hash.

Signature

returns Hash

A hash representation of the evaluator.

Implementation

def to_h
	@evaluator.to_h
end

def name

The name of the service - used for informational purposes like logging. e.g. myapp.com.

Implementation

def name
	@evaluator.name
end

def start

Start the service. Called before the container setup.

Implementation

def start
	Console.debug(self){"Starting service #{self.name}..."}
end

def setup(container)

Setup the service into the specified container.

Signature

parameter container Async::Container::Generic

Implementation

def setup(container)
	Console.debug(self){"Setting up service #{self.name}..."}
end

def stop(graceful = true)

Stop the service. Called after the container is stopped.

Implementation

def stop(graceful = true)
	Console.debug(self){"Stopping service #{self.name}..."}
end