services/

directory
v0.0.10-rc6 Latest Latest
Warning

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

Go to latest
Published: Mar 29, 2020 License: Apache-2.0

README

Service packages structure

All service cloud function packages share a consistent structure

Two functions and one type

Initialize function
  • Goal
    • Optimize cloud function performance by reducing the invocation latency
  • Implementation
    • Is executed once per cloud function instance as a cold start.
    • Cache objects expensive to create, like clients
    • Retreive settings once, like environment variables
    • Cached objects and reteived settings are exposed in one global variable named global
Global type
  • A struct to define a global variable carrying cached objects and retreived settings by Initialized function and used by EntryPoint function
EntryPoint function
  • Goal
    • Execute operations to be performed each time the cloud function is invoked
  • Implementation
    • Is executed on every event triggering the cloud function
    • Uses cached objects and retreived settings prepared by the Initialized function and carried by a global variable of type Global
    • Performs the task a given service is targetted to do that is described before the package key word

Implementation example

How to implement a RAM service package in a Google Cloud function?

go.mod

Replace <vX.Y.Z> in the go.mod file by the RAM version release to be used

module example.com/cloudfunction

go 1.11

require github.com/BrunoReboul/ram <vX.Y.Z>
function.go for a background function triggered by PubSub events

Replace <package_name> by the name of the service package to be used

// Package p contains a background cloud function
package p

import (
    "context"

    "github.com/BrunoReboul/ram/<package_name>"
    "github.com/BrunoReboul/ram/ram"
)

var global <package_name>.Global
var ctx = context.Background()

// EntryPoint is the function to be executed for each cloud function occurence
func EntryPoint(ctxEvent context.Context, PubSubMessage ram.PubSubMessage) error {
    return <package_name>.EntryPoint(ctxEvent, PubSubMessage, &global)
}

func init() {
    <package_name>.Initialize(ctx, &global)
}
function.go for a background function triggered by Google Cloud Storage events

Replace <package_name> by the name of the service package to be used

// Package p contains a background cloud function
package p

import (
    "context"

    "github.com/BrunoReboul/ram/<package_name>"
    "github.com/BrunoReboul/ram/ram"
)

var global <package_name>.Global
var ctx = context.Background()

// EntryPoint is the function to be executed for each cloud function occurence
func EntryPoint(ctxEvent context.Context, gcsEvent ram.GCSEvent) error {
    return <package_name>.EntryPoint(ctxEvent, PubSubMessage, &global)
}

func init() {
    <package_name>.Initialize(ctx, &global)
}

Automatic retrying

Automatic retrying is consistently implemented in RAM service packages as documented in Google Cloud Function best practice Retrying Background Functions

Impact on cloud functions Stackdriver logs:

  • Errors entry in log only appears for transient errors.
    • As en error is reported the function is retried.
  • Other errors and loged as information to avoid unwanted retries
    • To find errors in such cloud function logs you can use the following Stackdriver logging filter
resource.type="cloud_function"
textPayload:"error"

Directories

Path Synopsis
Package dumpinventory request Cloud Asset Inventory to perform an export - Triggered by: Cloud Scheduler Job, through PubSub messages - Instances: - one for all IAM bindings policies - one per AssetType for resource metadata exports - Output: none, CAI execute exports as an asynchonous task delivered in a Google Cloud Storage bucket - Automatic retrying: yes
Package dumpinventory request Cloud Asset Inventory to perform an export - Triggered by: Cloud Scheduler Job, through PubSub messages - Instances: - one for all IAM bindings policies - one per AssetType for resource metadata exports - Output: none, CAI execute exports as an asynchonous task delivered in a Google Cloud Storage bucket - Automatic retrying: yes
Package getgroupsettings retreives one group settings from `Groups Settings API` - Triggered by: PubSub messages in GCI groups topic - Instances: Only one - Output: PubSub messages to a dedicated topic formated like Cloud Asset Inventory feed messages - Cardinality: one-one, one output message for each triggering event
Package getgroupsettings retreives one group settings from `Groups Settings API` - Triggered by: PubSub messages in GCI groups topic - Instances: Only one - Output: PubSub messages to a dedicated topic formated like Cloud Asset Inventory feed messages - Cardinality: one-one, one output message for each triggering event

Jump to

Keyboard shortcuts

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