run

package
Version: v1.1.6 Latest Latest
Warning

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

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

Documentation

Overview

Contains design patterns for the standard lifecycle of objects (opened, closed, openable, closable, runnable). Helper classes for lifecycle provisioning.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type FixedRateTimer

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

Timer that is triggered in equal time intervals.

It has summetric cross-language implementation and is often used by Pip.Services toolkit to perform periodic processing and cleanup in microservices.

see INotifiable

Example:

type MyComponent {
	timer FixedRateTimer
}
    ...
    func (mc* MyComponent) open(correlationId string) {
		...
		mc.timer = NewFixedRateTimerFromCallback(() => { this.cleanup }, 60000, 0);
        mc.timer.start();
        ...
    }

    func (mc* MyComponent) open(correlationId: string){
        ...
        mc.timer.stop();
        ...
    }

func NewFixedRateTimer

func NewFixedRateTimer() *FixedRateTimer

Creates new instance of the timer and sets its values. Returns *FixedRateTimer

func NewFixedRateTimerFromCallback

func NewFixedRateTimerFromCallback(callback func(), interval int, delay int) *FixedRateTimer

Creates new instance of the timer and sets its values. Parameters:

  • callback func() callback function to call when timer is triggered.
  • interval int an interval to trigger timer in milliseconds.
  • delay int a delay before the first triggering in milliseconds.

Returns *FixedRateTimer

func NewFixedRateTimerFromTask

func NewFixedRateTimerFromTask(task INotifiable, interval int, delay int) *FixedRateTimer

Creates new instance of the timer and sets its values. Parameters:

  • callback INotifiable Notifiable object to call when timer is triggered.
  • interval int an interval to trigger timer in milliseconds.
  • delay int a delay before the first triggering in milliseconds.

Returns *FixedRateTimer

func (*FixedRateTimer) Callback

func (c *FixedRateTimer) Callback() func()

Gets the callback function that is called when this timer is triggered. Returns function the callback function or null if it is not set. Returns func()

func (*FixedRateTimer) Close

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

Closes the timer. This is required by ICloseable interface, but besides that it is identical to stop(). Parameters:

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

Returns error

func (*FixedRateTimer) Delay

func (c *FixedRateTimer) Delay() int

Gets initial delay before the timer is triggered for the first time. Returns number the delay in milliseconds.

func (*FixedRateTimer) Interval

func (c *FixedRateTimer) Interval() int

Gets periodic timer triggering interval. Returns number the interval in milliseconds

func (*FixedRateTimer) IsStarted

func (c *FixedRateTimer) IsStarted() bool

Checks if the timer is started. Returns bool true if the timer is started and false if it is stopped.

func (*FixedRateTimer) SetCallback

func (c *FixedRateTimer) SetCallback(value func())

Sets the callback function that is called when this timer is triggered. Parameters:

  • value func() the callback function to be called.

func (*FixedRateTimer) SetDelay

func (c *FixedRateTimer) SetDelay(value int)

Sets initial delay before the timer is triggered for the first time. Parameters:

  • value int a delay in milliseconds.

func (*FixedRateTimer) SetInterval

func (c *FixedRateTimer) SetInterval(value int)

Sets periodic timer triggering interval. Parameters:

  • value int an interval in milliseconds.

func (*FixedRateTimer) SetTask

func (c *FixedRateTimer) SetTask(value INotifiable)

Sets a new INotifiable object to receive notifications from this timer. Parameters:

  • value INotifiable a INotifiable object to be triggered.

func (*FixedRateTimer) Start

func (c *FixedRateTimer) Start()

Starts the timer. Initially the timer is triggered after delay. After that it is triggered after interval until it is stopped.

func (*FixedRateTimer) Stop

func (c *FixedRateTimer) Stop()

Stops the timer.

func (*FixedRateTimer) Task

func (c *FixedRateTimer) Task() INotifiable

Gets the INotifiable object that receives notifications from this timer. Returns INotifiable the INotifiable object or null if it is not set.

type ICleanable

type ICleanable interface {
	// Clears component state.
	// Parameters:
	//  - correlationId string
	//  transaction id to trace execution through call chain.
	Clear(correlationId string) error
}

Interface for components that should clean their state.

Cleaning state most often is used during testing. But there may be situations when it can be done in production.

see Cleaner

Example:

type MyObjectWithState {
	_state interface{}
}
    ...
    func (mo * MyObjectWithState ) clear(correlationId string) {
        mo._state = interface{}
    }

type IClosable

type IClosable interface {
	Close(correlationId string) error
}

Interface for components that require explicit closure.

For components that require opening as well as closing use IOpenable interface instead.

see IOpenable

see Closer

Example:

type MyConnector {
    _client interface{}
}
    ... // The _client can be lazy created

    func (mc *MyConnector) Close(correlationId: string):error {
        if (mc._client != nil) {
            mc._client.Close()
			mc._client = nil
			return nil
		}
    }

type IExecutable

type IExecutable interface {
	// 	Executes component with arguments and receives execution result.
	// Parameters:
	//  - correlationId string
	//  transaction id to trace execution through call chain.
	//  - args *Parameters
	//  execution arguments.
	// Return interface{}, error
	// result or execution and error
	Execute(correlationId string, args *Parameters) (result interface{}, err error)
}

Interface for components that can be called to execute work.

Example:

type EchoComponent {}
    ...
    func  (ec* EchoComponent) Execute(correlationId: string, args: Parameters) (result interface{}, err error) {
        return nil, result = args.getAsObject("message")
    }

echo := EchoComponent{};
message = "Test";
res, err = echo.Execute("123", NewParametersFromTuples("message", message));
fmt.Println(res);

type INotifiable

type INotifiable interface {
	// Notifies the component about occured event.
	// Parameters:
	//  - correlationId string
	//  transaction id to trace execution through call chain.
	//  - args *Parameters
	//  notification arguments.
	Notify(correlationId string, args *Parameters)
}

Interface for components that can be asynchronously notified. The notification may include optional argument that describe the occured event.

see Notifier

see IExecutable

Example:

type MyComponent {}
    ...
    func (mc *MyComponent)Notify(correlationId: string, args: Parameters){
        fmt.Println("Occured event " + args.GetAsString("event"));
    }

myComponent := MyComponent{};

myComponent.Notify("123", NewParametersFromTuples("event", "Test Event"));

type IOpenable

type IOpenable interface {
	IClosable
	// Checks if the component is opened.
	// Returns bool
	// true if the component has been opened and false otherwise.
	IsOpen() bool
	// Opens the component.
	// Parameters:
	//  - correlationId: string
	//  transaction id to trace execution through call chain.
	// Return error
	Open(correlationId string) error
}

Interface for components that require explicit opening and closing.

For components that perform opening on demand consider using ICloseable interface instead.

see IOpenable

see Opener

Example:

type MyPersistence {
	_client interface{}
}

    func (mp* MyPersistence)IsOpen() bool {
        return mp._client != nil;
    }

    (mp* MyPersistence) Open(correlationId: string) error {
        if (mp.isOpen()) {
            return nil;
        }
    }

    (mp* MyPersistence) Close(correlationId: string) {
        if (mp._client != nil) {
            mp._client.close();
            mp._client = nil;
        }
    }

type IParameterized

type IParameterized interface {
	// Sets execution parameters.
	// Parameters:
	// parameters *Parameters
	// execution parameters.
	SetParameters(parameters *Parameters)
}

Interface for components that require execution parameters.

type Parameters

type Parameters struct {
	data.AnyValueMap
}

Contains map with execution parameters.

In general, this map may contain non-serializable values. And in contrast with other maps, its getters and setters support dot notation and able to access properties in the entire object graph.

This class is often use to pass execution and notification arguments, and parameterize classes before execution.

func NewEmptyParameters

func NewEmptyParameters() *Parameters

Creates a new instance of the map and assigns its value. Returns *Parameters

func NewParameters

func NewParameters(values map[string]interface{}) *Parameters

Creates a new instance of the map and assigns its value. Parameters:

  • values map[string]interface{}

Returns *Parameters

func NewParametersFromConfig

func NewParametersFromConfig(config *config.ConfigParams) *Parameters

Returns Parameters a new Parameters object.

func NewParametersFromMaps

func NewParametersFromMaps(maps ...map[string]interface{}) *Parameters

Creates a new Parameters by merging two or more maps. Maps defined later in the list override values from previously defined maps. Parameters:

  • maps ...map[string]interface{} an array of maps to be merged

Returns *Parameters a newly created Parameters.

func NewParametersFromTuples

func NewParametersFromTuples(tuples ...interface{}) *Parameters

Creates a new Parameters object filled with provided key-value pairs called tuples. Tuples parameters contain a sequence of key1, value1, key2, value2, ... pairs. see AnyValueMapFromTuplesArray Parameters:

  • tuples ...interface{} the tuples to fill a new Parameters object.

Returns *Parameters a new Parameters object.

func NewParametersFromTuplesArray

func NewParametersFromTuplesArray(tuples []interface{}) *Parameters

Creates a new AnyValueMap from a list of key-value pairs called tuples. The method is similar to fromTuples but tuples are passed as array instead of parameters. Parameters:

  • tuples []interface{} a list of values where odd elements are keys and the following even elements are values

Returns *Parameters a newly created Parameters.

func NewParametersFromValue

func NewParametersFromValue(value interface{}) *Parameters

Creates a new Parameters object filled with key-value pairs from specified object. Parameters:

  • value interface{} an object with key-value pairs used to initialize a new Parameters.

Returns *Parameters a new Parameters object.

func (*Parameters) AssignTo

func (c *Parameters) AssignTo(value interface{})

Assigns (copies over) properties from the specified value to this map. Parameters:

  • value interface{} value whose properties shall be copied over.

func (*Parameters) Clone

func (c *Parameters) Clone() interface{}

Creates a binary clone of this object. Returns interface{} a clone of this object.

func (*Parameters) Contains

func (c *Parameters) Contains(key string) bool

Checks if this map contains an element with specified key. The key can be defined using dot notation and allows to recursively access elements of elements. Parameters:

  • key string a key to be checked

Returns bool true if this map contains the key or false otherwise.

func (*Parameters) Get

func (c *Parameters) Get(key string) interface{}

Gets a map element specified by its key. The key can be defined using dot notation and allows to recursively access elements of elements. Parameters:

  • key string a key of the element to get.

Returns interface{} the value of the map element.

func (*Parameters) GetAsNullableParameters

func (c *Parameters) GetAsNullableParameters(key string) *Parameters

Converts map element into an Parameters or returns nil if conversion is not possible. Parameters:

  • key: string a key of element to get.

Returns *Parameters Parameters value of the element or nil if conversion is not supported.

func (*Parameters) GetAsParameters

func (c *Parameters) GetAsParameters(key string) *Parameters

Converts map element into an Parameters or returns empty Parameters if conversion is not possible. Parameters:

  • key string a key of element to get.

Returns *Parameters Parameters value of the element or empty Parameters if conversion is not supported.

func (*Parameters) GetAsParametersWithDefault

func (c *Parameters) GetAsParametersWithDefault(key string, defaultValue *Parameters) *Parameters

Converts map element into an Parameters or returns default value if conversion is not possible. Parameters:

-key string
a key of element to get.
- defaultValue *Parameters
the default value

Returns *Parameters Parameters value of the element or default value if conversion is not supported.

func (*Parameters) Omit

func (c *Parameters) Omit(paths ...string) *Parameters

Omits selected parameters from this Parameters and returns the rest as a new Parameters object. Parameters:

  • paths ...string keys to be omitted from copying over to new Parameters.

Returns *Parameters a new Parameters object.

func (*Parameters) Override

func (c *Parameters) Override(parameters *Parameters, recursive bool) *Parameters

Overrides parameters with new values from specified Parameters and returns a new Parameters object. see setDefaults Parameters:

  • parameters: Parameters Parameters with parameters to override the current values.
  • recursive bool true to perform deep copy, and false for shallow copy. Default: false

Returns *Parameters a new Parameters object.

func (*Parameters) Pick

func (c *Parameters) Pick(paths ...string) *Parameters

Picks select parameters from this Parameters and returns them as a new Parameters object. Parameters:

  • paths ...string keys to be picked and copied over to new Parameters.

Returns *Parameters a new Parameters object.

func (*Parameters) Put

func (c *Parameters) Put(key string, value interface{})

Puts a new value into map element specified by its key. The key can be defined using dot notation and allows to recursively access elements of elements. Parameters:

  • key string a key of the element to put.
  • value interface{} a new value for map element.

func (*Parameters) Remove

func (c *Parameters) Remove(key string)

Removes a map element specified by its key Parameters:

  • key string a key of the element to remove.

func (*Parameters) SetDefaults

func (c *Parameters) SetDefaults(defaultParameters *Parameters, recursive bool) *Parameters

Set default values from specified Parameters and returns a new Parameters object. see Override Parameters:

  • defaultParameters *Parameters Parameters with default parameter values.
  • recursive bool true to perform deep copy, and false for shallow copy. Default: false

Returns *Parameters a new Parameters object.

type TCleaner

type TCleaner struct{}

Helper class that cleans stored object state.

var Cleaner *TCleaner = &TCleaner{}

func (*TCleaner) Clear

func (c *TCleaner) Clear(correlationId string, components []interface{}) error

Clears state of multiple components. To be cleaned state components must implement ICleanable interface. If they don't the call to this method has no effect. Parameters:

  • correlationId string transaction id to trace execution through call chain.
  • components []interface{}

the list of components that are to be cleaned. Returns error

func (*TCleaner) ClearOne

func (c *TCleaner) ClearOne(correlationId string, component interface{}) error

Clears state of specific component. To be cleaned state components must implement ICleanable interface. If they don't the call to this method has no effect. Parameters:

  • correlationId: string transaction id to trace execution through call chain.
  • component interface{} the component that is to be cleaned.

Returns error

type TCloser

type TCloser struct{}

Helper class that closes previously opened components.

var Closer *TCloser = &TCloser{}

func (*TCloser) Close

func (c *TCloser) Close(correlationId string, components []interface{}) error

Closes multiple components. To be closed components must implement ICloseable interface. If they don't the call to this method has no effect. Parameters:

  • correlationId string transaction id to trace execution through call chain.
  • components []interface{} the list of components that are to be closed.

Returns error

func (*TCloser) CloseOne

func (c *TCloser) CloseOne(correlationId string, component interface{}) error

To be closed components must implement ICloseable interface. If they don't the call to this method has no effect. Parameters:

  • correlationId string transaction id to trace execution through call chain.
  • component interface{} the component that is to be closed.

Returns error

type TExecutor

type TExecutor struct{}

Helper class that executes components.

var Executor *TExecutor = &TExecutor{}

func (*TExecutor) Execute

func (c *TExecutor) Execute(correlationId string, components []interface{}, args *Parameters) ([]interface{}, error)

To be executed components must implement IExecutable interface. If they don't the call to this method has no effect. Parameters:

  • correlationId string transaction id to trace execution through call chain.
  • components []interface{} a list of components that are to be executed.
  • args *Parameters execution arguments.

Returns []interface{}, error execution result or error

func (*TExecutor) ExecuteOne

func (c *TExecutor) ExecuteOne(correlationId string, component interface{}, args *Parameters) (interface{}, error)

Executes specific component. To be executed components must implement IExecutable interface. If they don't the call to this method has no effect. Parameters:

  • correlationId string transaction id to trace execution through call chain.
  • component interface{} the component that is to be executed.
  • args: *Parameters execution arguments.

Returns []interface{}, error execution result or error

type TNotifier

type TNotifier struct{}

Helper class that notifies components.

var Notifier *TNotifier = &TNotifier{}

func (*TNotifier) Notify

func (c *TNotifier) Notify(correlationId string, components []interface{}, args *Parameters)

Notifies multiple components. To be notified components must implement INotifiable interface. If they don't the call to this method has no effect. see NotifyOne see INotifiable Parameters:

  • correlationId string transaction id to trace execution through call chain.
  • components []interface{} a list of components that are to be notified.
  • args *Parameters notification arguments.

func (*TNotifier) NotifyOne

func (c *TNotifier) NotifyOne(correlationId string, component interface{}, args *Parameters)

Notifies specific component. To be notiied components must implement INotifiable interface. If they don't the call to this method has no effect. see INotifiable Parameters:

  • correlationId string transaction id to trace execution through call chain.
  • component interface{} the component that is to be notified.
  • args *Parameters notifiation arguments.

type TOpener

type TOpener struct{}

Helper class that opens components.

var Opener *TOpener = &TOpener{}

func (*TOpener) IsOpen

func (c *TOpener) IsOpen(components []interface{}) bool

Checks if all components are opened. To be checked components must implement IOpenable interface. If they don't the call to this method returns true. see isOpenOne see IOpenable Parameters:

  • components []interface{} a list of components that are to be checked.

Returns bool true if all components are opened and false if at least one component is closed.

func (*TOpener) IsOpenOne

func (c *TOpener) IsOpenOne(component interface{}) bool

Checks if specified component is opened. To be checked components must implement IOpenable interface. If they don't the call to this method returns true. see IOpenable

  • component interface{} the component that is to be checked.

Returns bool true if component is opened and false otherwise.

func (*TOpener) Open

func (c *TOpener) Open(correlationId string, components []interface{}) error

Opens multiple components. To be opened components must implement IOpenable interface. If they don't the call to this method has no effect. see OpenOne see IOpenable Parameters:

  • correlationId string transaction id to trace execution through call chain. components []interface{} the list of components that are to be closed.

Returns error

func (*TOpener) OpenOne

func (c *TOpener) OpenOne(correlationId string, component interface{}) error

Opens specific component. To be opened components must implement IOpenable interface. If they don't the call to this method has no effect. see IOpenable Parameters:

  • correlationId string (optional) transaction id to trace execution through call chain.
  • component interface{} the component that is to be opened.

Returns error

Jump to

Keyboard shortcuts

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