container

package
v1.1.7 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 12, 2023 License: MIT Imports: 16 Imported by: 6

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Container

type Container struct {
	// contains filtered or unexported fields
}

Inversion of control (IoC) container that creates components and manages their lifecycle.

The container is driven by configuration, that usually stored in JSON or YAML file. The configuration contains a list of components identified by type or locator, followed by component configuration.

On container start it performs the following actions:

Creates components using their types or calls registered factories to create components using their locators Configures components that implement IConfigurable interface and passes them their configuration parameters Sets references to components that implement IReferenceable interface and passes them references of all components in the container Opens components that implement IOpenable interface On container stop actions are performed in reversed order:

Closes components that implement ICloseable interface Unsets references in components that implement IUnreferenceable interface Destroys components in the container. The component configuration can be parameterized by dynamic values. That allows specialized containers to inject parameters from command line or from environment variables.

The container automatically creates a ContextInfo component that carries detail information about the container and makes it available for other components.

see IConfigurable (in the PipServices "Commons" package)

see IReferenceable (in the PipServices "Commons" package)

see IOpenable (in the PipServices "Commons" package)

Configuration parameters name: the context (container or process) name description: human-readable description of the context properties: entire section of additional descriptive properties

  • ...

Example

======= config.yml ========
- descriptor: mygroup:mycomponent1:default:default:1.0
  param1: 123
  param2: ABC

- type: mycomponent2,mypackage
  param1: 321
  param2: XYZ
============================

container := NewEmptyContainer()
container.AddFactory(newMyComponentFactory())

parameters := NewConfigParamsFromValue(process.env)
container.ReadConfigFromFile("123", "./config/config.yml", parameters)

err := container.Open("123")
ftm.Println("Container is opened")
...
err = container.Close("123")
fmt.Println("Container is closed")

func InheritContainer

func InheritContainer(name string, description string,
	referenceable crefer.IReferenceable) *Container

Creates a new instance of the container inherit from reference. Parameters:

  • name string a container name (accessible via ContextInfo)
  • description string a container description (accessible via ContextInfo)
  • referenceable crefer.IReferenceable
  • referenceble object for inherit

Returns *Container

func NewContainer

func NewContainer(name string, description string) *Container

Creates a new instance of the container. Parameters:

  • name string a container name (accessible via ContextInfo)
  • description string a container description (accessible via ContextInfo)

Returns *Container

func NewEmptyContainer

func NewEmptyContainer() *Container

Creates a new empty instance of the container. Returns *Container

func (*Container) AddFactory

func (c *Container) AddFactory(factory cbuild.IFactory)

Adds a factory to the container. The factory is used to create components added to the container by their locators (descriptors). Parameters:

  • factory IFactory a component factory to be added.

func (*Container) Close

func (c *Container) Close(correlationId string) error

Closes component and frees used resources. Parameters:

  • correlationId string transaction id to trace execution through call chain.

Returns error

func (*Container) Configure

func (c *Container) Configure(conf *cconfig.ConfigParams)

Configures component by passing configuration parameters. Parameters:

  • config *cconfig.ConfigParams configuration parameters to be set.

func (*Container) Info

func (c *Container) Info() *info.ContextInfo

func (*Container) IsOpen

func (c *Container) IsOpen() bool

Checks if the component is opened. Returns bool true if the component has been opened and false otherwise.

func (*Container) Logger

func (c *Container) Logger() log.ILogger

func (*Container) Open

func (c *Container) Open(correlationId string) (err error)

Opens the component. Parameters:

  • correlationId string transaction id to trace execution through call chain.

Returns error

func (*Container) ReadConfigFromFile

func (c *Container) ReadConfigFromFile(correlationId string,
	path string, parameters *cconfig.ConfigParams) error

Reads container configuration from JSON or YAML file and parameterizes it with given values. Parameters:

  • correlationId string transaction id to trace execution through call chain.
  • path string a path to configuration file
  • parameters *cconfig.ConfigParams

values to parameters the configuration or null to skip parameterization.

func (*Container) SetLogger

func (c *Container) SetLogger(logger log.ILogger)

type ProcessContainer

type ProcessContainer struct {
	Container
	// contains filtered or unexported fields
}

Inversion of control (IoC) container that runs as a system process. It processes command line arguments and handles unhandled exceptions and Ctrl-C signal to gracefully shutdown the container.

Command line arguments

--config / -c path to JSON or YAML file with container configuration (default: "./config/config.yml")
--param / --params / -p value(s) to parameterize the container configuration
--help / -h prints the container usage help

see Container

Example

container = NewEmptyProcessContainer();
container.Container.AddFactory(NewMyComponentFactory());

container.Run(process.args);

func InheritProcessContainer

func InheritProcessContainer(name string, description string,
	referenceable crefer.IReferenceable) *ProcessContainer

Creates a new instance of the container inherit from reference. Parameters:

  • name string a container name (accessible via ContextInfo)
  • description string a container description (accessible via ContextInfo)
  • referenceable crefer.IReferenceable
  • referenceble object for inherit

Returns *Container

func NewEmptyProcessContainer

func NewEmptyProcessContainer() *ProcessContainer

Creates a new empty instance of the container. Returns ProcessContainer

func NewProcessContainer

func NewProcessContainer(name string, description string) *ProcessContainer

Creates a new instance of the container. Parameters:

  • name string a container name (accessible via ContextInfo)
  • description string a container description (accessible via ContextInfo)

Returns ProcessContainer

func (*ProcessContainer) Run

func (c *ProcessContainer) Run(args []string)

Runs the container by instantiating and running components inside the container. It reads the container configuration, creates, configures, references and opens components. On process exit it closes, unreferences and destroys components to gracefully shutdown. Parameters:

  • args []string command line arguments

func (*ProcessContainer) SetConfigPath

func (c *ProcessContainer) SetConfigPath(configPath string)

Set path for configuration file

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL