CloudFunction

Abstract Google Function that acts as a container to instantiate and run components and expose them via an external entry point.

Implements: Container

Description

The CloudFunction class allows you to create an Abstract Google Function that acts as a container to instantiate and run components and expose them via an external entry point.

Importan points

  • When handling calls, the “cmd” parameter determines the action that shall be called, while the other parameters are passed to the action itself.

  • The container configuration for this Google Function is stored in "./config/config.yml" file. But this path can be overriden by CONFIG_PATH environment variable.

References

  • *:logger:*:*:1.0: (optional) ILogger components to pass log messages
  • *:counters:*:*:1.0: (optional) ICounters components to pass collected measurements
  • *:service:gcp-function:*:1.0: (optional) ICloudFunctionController services to handle action requests.
  • *:service:commandable-gcp-function:*:1.0: (optional) ICloudFunctionController services to handle action requests.

Constructors

Creates a new instance of this Google Function.

public constructor(name?: string, description?: string)

  • name: string - (optional) container’s name (accessible via ContextInfo)
  • description: string - (optional) container’s description (accessible via ContextInfo)

Fields

_actions

The map of registered actions.

protected _actions: { [id: string]: any }

_config_path

The default path to config file.

protected _config_path: str = ‘./config/config.yml’

_counters

Performance counters.

protected _counters: CompositeCounters

_dependencyResolver

Dependencies resolver.

protected _dependencyResolver: DependencyResolver

_logger

Logger.

protected _logger: CompositeLogger

_tracer

The tracer.

protected _tracer: CompositeTracer

_schemas

The map of registred validation schemas.

protected _schemas: { [id: string]: Schema }

Instance methods

execute

Executes this Google Function and returns the result. This method can be overloaded in child classes if they need to change the default behavior

protected execute(req: Request, res: Response): Promise<any>

  • req: Request - the request object (with function arguments)
  • res: Response - the result of the function execution.
  • returns: Promise<any> - the promise.

getCommand

Returns command from Google Function request. This method can be overloaded in child classes

protected getCommand(req: Request): string

  • req: Request - Google Function request
  • returns: string - Returns command from req

getTraceID

Returns a trace id from a Google Function request. This method can be overloaded in child classes

protected getTraceID(req: Request): string

  • req: Request - Google Function request
  • returns: string - Returns a TraceId from req

getHandler

Return plugin function

public getHandler(): (req: Request, res: Response) => Promise<any>

  • returns: (req: Request, res: Response) => Promise<any> - plugin function
    • req: Request - an incoming request object with invocation parameters.
    • res: Response - an returnning response object with result parameters.

instrument

Adds instrumentation to log calls and measures call time. It returns a Timing object that is used to end the time measurement.

protected instrument(context: IContext, name: string): InstrumentTiming

  • context: IContext - (optional) a context to trace execution through a call chain.
  • name: string - method’s name.
  • returns: InstrumentTiming - Timing object to end the time measurement.

open

Opens the component.

public open(context: IContext) :Promise<void>

  • context: IContext - (optional) a context to trace execution through a call chain.

registerAction

Registers an action in this Google Function.

Note: This method has been deprecated. Use CloudFunctionController instead.

protected registerAction(cmd: string, schema: Schema, action: (req: Request, res: Response) => Promise): void

  • cmd: str - a action/command name.
  • schema: Schema - a validation schema to validate received parameters.
  • action: (req: Request, res: Response) => Promise<any> - an action function that is called when action is invoked.

register

Registers all actions in this Googel Function.

Note: Overloading of this method has been deprecated. Use GoogelFunctionService instead.

register()

registerServices

Registers all Google Function services in the container.

protected registerServices(): void

run

Runs this Google Function, loads container configuration, instantiate components and manage their lifecycle, makes this function ready to access action calls.

public run(): Promise<void>

setReferences

Sets references to dependent components.

public setReferences(references: IReferences)

  • references: IReferences - references to locate the component’s dependencies.

Examples

class MyCloudFunction extends CloudFunction {
    public constructor() {
        base("mygroup", "MyGroup Google Function");
    }
}

let googleFunction = new MyCloudFunction();
   
await service.run();
console.log("MyCloudFunction is started");