Documentation
¶
Index ¶
- Constants
- type Container
- func (c *Container) AddFactory(factory cbuild.IFactory)
- func (c *Container) Close(ctx context.Context, correlationId string) error
- func (c *Container) Configure(ctx context.Context, conf *cconfig.ConfigParams)
- func (c *Container) Info() *info.ContextInfo
- func (c *Container) IsOpen() bool
- func (c *Container) Logger() log.ILogger
- func (c *Container) Open(ctx context.Context, correlationId string) error
- func (c *Container) ReadConfigFromFile(ctx context.Context, correlationId string, path string, ...) error
- func (c *Container) SetLogger(logger log.ILogger)
- type ProcessContainer
Constants ¶
const DefaultConfigFilePath = "./config/config.yml"
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Container ¶
type Container struct { References *refer.ContainerReferences // contains filtered or unexported fields }
Container 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(os.Environ()); container.ReadConfigFromFile(context.Background(), "123", "./config/config.yml", parameters); err := container.Open(context.Background(), "123") fmt.Println("Container is opened")) ... err = container.Close(context.Background(), "123") fmt.Println("Container is closed")
func InheritContainer ¶
func InheritContainer(name string, description string, referenceable crefer.IReferenceable) *Container
InheritContainer 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 ¶
NewContainer 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
NewEmptyContainer creates a new empty instance of the container.
Returns *Container
func (*Container) AddFactory ¶
AddFactory 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 ¶
Close component and frees used resources.
Parameters: - ctx context.Context - correlationId string transaction id to trace execution through call chain. Returns: error
func (*Container) Configure ¶
func (c *Container) Configure(ctx context.Context, conf *cconfig.ConfigParams)
Configure component by passing configuration parameters.
Parameters: - ctx context.Context - config *cconfig.ConfigParams configuration parameters to be set.
func (*Container) Info ¶
func (c *Container) Info() *info.ContextInfo
func (*Container) IsOpen ¶
IsOpen checks if the component is opened.
Returns bool true if the component has been opened and false otherwise.
func (*Container) Open ¶
Open the component.
Parameters: - ctx context.Context - correlationId string transaction id to trace execution through call chain. Returns: error
func (*Container) ReadConfigFromFile ¶
func (c *Container) ReadConfigFromFile(ctx context.Context, correlationId string, path string, parameters *cconfig.ConfigParams) error
ReadConfigFromFile container configuration from JSON or YAML file and parameterizes it with given values.
Parameters: - ctx context.Context - 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.
type ProcessContainer ¶
type ProcessContainer struct { *Container // contains filtered or unexported fields }
ProcessContainer 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(context.Background(), os.Environ())
func InheritProcessContainer ¶
func InheritProcessContainer(name string, description string, referenceable crefer.IReferenceable) *ProcessContainer
InheritProcessContainer 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
NewEmptyProcessContainer creates a new empty instance of the container.
Returns: ProcessContainer
func NewProcessContainer ¶
func NewProcessContainer(name string, description string) *ProcessContainer
NewProcessContainer 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(ctx context.Context, args []string)
Run 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: - ctx context.Context - args []string command line arguments
func (*ProcessContainer) SetConfigPath ¶
func (c *ProcessContainer) SetConfigPath(configPath string)
SetConfigPath set path for configuration file