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.
Important 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) ICloudFunctionService services to handle action requests.
- *:service:commandable-gcp-function:*:1.0: (optional) ICloudFunctionService services to handle action requests.
Constructors
Creates a new instance of this Google Function.
CloudFunction(name: str = None, description: str = None)
- name: str - (optional) container’s name (accessible via ContextInfo)
- description: str - (optional) container’s description (accessible via ContextInfo)
Fields
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
_execute(req: flask.Request): Any
- req: flask.Request - the request object (with function arguments)
- returns: Any - function result.
_get_command
Returns command from Google Function request. This method can be overloaded in child classes
_get_command(req: flask.Request): str
- req: flask.Request - Google Function request
- returns: str - Returns command from req
_get_correlation_id
Returns correlationId from Googel Function request. This method can be overloaded in child classes
getCorrelationId(req: flask.Request): str
- req: flask.Request - Google Function request
- returns: str - Returns correlationId from req
get_handler
Return plugin function
get_handler(): Callable[flask.Request, Any]
- returns: Callable[flask.Request, Any] - plugin function
- req: flask.Request - an incoming request object with invocation parameters.
- res: Any - 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.
_instrument(correlation_id: Optional[str], name: str): InstrumentTiming
- correlation_id: Optional[str] - (optional) transaction id used to trace execution through the call chain.
- name: str - method’s name.
- returns: InstrumentTiming - Timing object to end the time measurement.
open
Opens the component.
open(correlation_id: Optional[str])
- correlation_id: Optional[str] - (optional) transaction id used to trace execution through the call chain.
_register_action
Registers an action in this Google Function.
Note: This method has been deprecated. Use CloudFunctionService instead.
_register_action(cmd: str, schema: Schema, action: Callable[flask.Request, Any])
- cmd: str - a action/command name.
- schema: Schema - a validation schema to validate received parameters.
- action: Callable[flask.Request, 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()
_register_services
Registers all Google Function services in the container.
_register_services()
run
Runs this Google Function, loads container configuration, instantiate components and manage their lifecycle, makes this function ready to access action calls.
run()
set_references
Sets references to dependent components.
set_references(references: IReferences)
- references: IReferences - references to locate the component’s dependencies.
Examples
class MyCloudFunction(CloudFunction):
def __init__(self):
super().__init__("mygroup", "MyGroup Google Function")
cloud_function = MyCloudFunction()
cloud_function.run()
print("MyCloudFunction is started")