Class Port<PortClient, PortConfig>Abstract

This is the class that should be extended to implement a new specific Port.

This class implements some util logic to facilitate the creation of new Ports, for this reason is exposed as abstract class, instead of an interface. The basic operations that already implemented in the class are:

  • Health.Checks management: using the Port.addCheck method is possible to include new observed values that will be used in the observability layers.
  • Create a Port.uuid unique identifier for the port instance, this uuid is used in error traceability.
  • Establish the context for the logger to simplify the identification of the port in the logs, this is, it's not necessary to indicate the uuid and context in each logging function call.
  • Store the configuration PortConfig previously validated by the Manager.

What the user of this class should develop in the specific port:

  • The Port.start method, which is responsible initialize or stablish the connection to the resources.
  • The Port.stop method, which is responsible stop services or disconnect from the resources.
  • The Port.close method, which is responsible to destroy the services, resources or perform a simple disconnection.
  • The Port.state property, a boolean value that indicates if the port is connected healthy (true) or not (false).
  • The Port.client property, that return the PortClient instance that is used to interact with the resources.

class diagram

In the other hand, this class extends the EventEmitter class, so it's possible to emit events to notify the status of the port:

  • error: should be emitted to notify errors in the resource management or access, this will not change the provider state, but the error will be registered in the observability layers.
  • closed: should be emitted if the access to the resources is not longer possible. This event should not be emitted if Port.stop or Port.close methods are used.
  • unhealthy: should be emitted when the port has limited access to the resources.
  • healthy: should be emitted when the port has recovered the access to the resources.

class diagram

Check some examples of implementation in:

Type Parameters

  • PortClient

    Underlying client type, this is, the real client of the wrapped provider

  • PortConfig

    Port configuration object, could be an extended version of the client config

Hierarchy (View Summary)

Constructors

Properties

Accessors

Methods

Constructors

Properties

config: PortConfig

Port configuration options

name: string

Port name, to be used as identifier

uuid: string = ...

Port unique identifier for trace purposes

Accessors

  • get checks(): Record<string, Check<any>[]>
  • Return the actual checks

    Returns Record<string, Check<any>[]>

  • get state(): boolean
  • Return the port state as a boolean value, true if the port is available, false in otherwise

    Returns boolean

Methods

  • Update or add a check measure. This should be used to inform about the state of resources behind the Port, for example memory usage, CPU usage, etc.

    The new check will be taking into account in the overall health status. The new check will be included in the checks object with the key indicated in the param measure.* If this key already exists, the componentId of the check parameter will be checked, if there is a check with the same componentId in the array, the check will be updated, in other case the new check will be added to the existing array.

    The maximum number external checks is 100

    Parameters

    • measure: string

      measure identification

    • check: Check<any>

      check to be updated or included

    Returns boolean

    true, if the check has been updated

  • Close the port, making it no longer available

    Returns Promise<void>

  • Emit an error event, to notify errors in the resource management or access, this will change the provider state by the upper manager.

    Parameters

    • event: "error"

      error event

    • error: Multi | Crash

      Error to be notified to the upper manager

    Returns boolean

  • Emit a closed event, to notify that the access to the resources is not longer possible. This event should not be emitted if Port.stop or Port.close methods are used. This event will change the provider state by the upper manager.

    Parameters

    • event: "closed"

      closed event

    • Optionalerror: Multi | Crash

      Error to be notified to the upper manager, if any

    Returns boolean

  • Emit an unhealthy event, to notify that the port has limited access to the resources. This event will change the provider state by the upper manager.

    Parameters

    • event: "unhealthy"

      unhealthy event

    • error: Multi | Crash

      Error to be notified to the upper manager

    Returns boolean

  • Emit a healthy event, to notify that the port has recovered the access to the resources. This event will change the provider state by the upper manager.

    Parameters

    • event: "healthy"

      healthy event

    Returns boolean

  • Add a listener for the error event, emitted when the component detects an error.

    Parameters

    • event: "error"

      error event

    • listener: (error: Multi | Crash) => void

      Error event listener

    Returns this

  • Add a listener for the closed event, emitted when the port resources are no longer available

    Parameters

    • event: "closed"

      closed event

    • listener: (error?: Multi | Crash) => void

      Closed event listener

    Returns this

  • Add a listener for the unhealthy event, emitted when the port has limited access to the resources

    Parameters

    • event: "unhealthy"

      unhealthy event

    • listener: (error: Multi | Crash) => void

      Unhealthy event listener

    Returns this

  • Add a listener for the healthy event, emitted when the port has recovered the access to the resources

    Parameters

    • event: "healthy"

      healthy event

    • listener: () => void

      Healthy event listener

    Returns this

  • Add a listener for the error event, emitted when the component detects an error. This is a one-time event, the listener will be removed after the first emission.

    Parameters

    • event: "error"

      error event

    • listener: (error: Multi | Crash) => void

      Error event listener

    Returns this

  • Add a listener for the closed event, emitted when the port resources are no longer available. This is a one-time event, the listener will be removed after the first emission.

    Parameters

    • event: "closed"

      closed event

    • listener: (error?: Multi | Crash) => void

      Closed event listener

    Returns this

  • Add a listener for the unhealthy event, emitted when the port has limited access to the resources. This is a one-time event, the listener will be removed after the first emission.

    Parameters

    • event: "unhealthy"

      unhealthy event

    • listener: (error: Multi | Crash) => void

      Unhealthy event listener

    Returns this

  • Add a listener for the healthy event, emitted when the port has recovered the access to the resources. This is a one-time event, the listener will be removed after the first emission.

    Parameters

    • event: "healthy"

      healthy event

    • listener: () => void

      Healthy event listener

    Returns this

  • Start the port, making it available

    Returns Promise<void>

  • Stop the port, making it unavailable

    Returns Promise<void>